dongxian3574 2014-10-18 21:04
浏览 73

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

I am checking to see if a user exists in my database, so i send the user's fbid and name to the PHP server and check my database to see if that user exists. However I keep getting NULL when i get the returned array that is suppose to hold the user.

in my console it says:

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: <>

Code:

#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条回答 默认 最新

  • dongxing9219 2014-10-18 21:20
    关注
    @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);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加