[英]Returned data from php is null, iOS
我正在检查数据库中是否存在用户,因此我将用户的fbid和名称发送到PHP服务器,并检查数据库是否存在该用户。 但是,当我得到假定可容纳用户的返回数组时,我总是得到NULL。
在我的控制台中它说:
2014-10-18 13:53:17.385 KandiTAG[1206:60b] checkUser has been called.
2014-10-18 13:53:17.453 KandiTAG[1206:60b] mutableData: <>
码:
#pragma mark - NSURLConnection Delegate
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[mutableData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[mutableData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
//if we get any connection error manage it here
//for example use alert view to say no internet connection
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSMutableArray *data = [NSJSONSerialization JSONObjectWithData:mutableData options:kNilOptions error:nil];
NSLog(@"mutableData: %@", mutableData);
NSLog(@"response JSON: %@", data);
}
-(void)checkUser {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
FBid = [defaults stringForKey:@"FBID"];
userName = [defaults stringForKey:@"NAME"];
usr = [NSString stringWithFormat:@"user"];
pw = [NSString stringWithFormat:@"password"];
NSLog(@"checkUser has been called");
NSString *requestString = [NSString stringWithFormat:@"user=%@&pw=%@&fbid=%@&username=%@", usr, pw, FBid, userName];
NSData *requestData = [requestString dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"example.com/login.php?"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/..." forHTTPHeaderField:@"content-type"];
[request setHTTPBody:requestData];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection)
{
mutableData = [[NSMutableData alloc] init];
}
}
@interface yourVC : UIViewController<NSURLConnectionDelegate>
{
NSMutableData *_responseData;
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
FBid = [defaults stringForKey:@"FBID"];
userName = [defaults stringForKey:@"NAME"];
usr = [NSString stringWithFormat:@"user"];
pw = [NSString stringWithFormat:@"password"];
NSString *requestString = [NSString stringWithFormat:@"user=%@&pw=%@&fbid=%@&username=%@", usr, pw, FBid, userName];
NSData *requestData = [requestString dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"example.com/login.php?"]];
// check this directly in a browser to see if it even returns data
// are you missing the http:// in your actual example?
// make sure the server responds to a GET for this
NSLog(@"%@%@",@"http://example.com/login.php?",requestString);
[request setHTTPMethod:@"POST"];
[request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:requestData];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
_responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[_responseData appendData:data];
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse*)cachedResponse {
return nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"%@", _responseData);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.