来自php的返回数据为null,iOS

时间:2014-10-18 21:04:47

标签: php ios mysql objective-c

我正在检查我的数据库中是否存在用户,因此我将用户的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];

    }
}

1 个答案:

答案 0 :(得分:0)

@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);
}