duanri1985 2014-11-28 09:47
浏览 124

我的JSONObjectWithData为null

I have a problem with my JSON who send me a null value.

I think it's not a problem with my webservice in PhP because my colleague who is a expert in PhP doesn't see any error

Just below my Obj-C code

dispatch_queue_t downloadQueue = dispatch_queue_create("downloader", NULL);

        dispatch_async(downloadQueue, ^{
            NSData *result = self.envoyerLaDemande;
            dispatch_async(dispatch_get_main_queue(), ^{

                id strResult=nil;

                strResult = [NSJSONSerialization JSONObjectWithData:result options:0 error:nil];

                NSLog(@"strResult: %@", strResult);

                NSString* boolOk=[strResult objectForKey:@"result"];
                NSLog(@"BoolOk: %@", boolOk);

And there is the PHP script

$data = array();

if(condition == 1)
{
$data["result"] = "ok";
}
else
{
$data["result"] = "error";
}
$data["result"] = "coucou";
}
echo json_encode($data);

And the methode envoyerLaDemande

-(NSData*)envoyerLaDemande{


NSURL *url = [NSURL URLWithString:@"http://mywebsite.com.com/App/V2/mailTo.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

NSString* requestFields;


requestFields = [NSString stringWithFormat:@"agence=%@&", nomAgence];

request.HTTPBody = requestData;
request.HTTPMethod = @"POST";

NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (error == nil && response.statusCode == 200) {
} else {
}
return responseData;

}
  • 写回答

1条回答 默认 最新

  • duanchu7271 2014-11-28 14:16
    关注

    The problem is that you're doing two dispacth_async.

    In the first one, you're telling the OS "Perform this code in the background", this will start loading the request in the background. However, immediately following that code you're telling the OS to perform a piece of code on the main thread. This piece of code accesses the result variable which hasn't been set yet because it's still being loaded in the background.

    If you insist on using these dispatches, simply removing the dispatch back to the main queue should work. However, consider looking into NSURLConnectionDataDelegate or at the very least use the method sendAsynchronousRequest:queue:completionHandler on your NSURLConnection.

    This code should work in your current scenario.

    dispatch_queue_t downloadQueue = dispatch_queue_create("downloader", NULL);
    
    dispatch_async(downloadQueue, ^{
        NSData *result = self.envoyerLaDemande;
        id strResult=nil;
        strResult = [NSJSONSerialization JSONObjectWithData:result options:0 error:nil];
        NSLog(@"strResult: %@", strResult);
        NSString* boolOk=[strResult objectForKey:@"result"];
        NSLog(@"BoolOk: %@", boolOk);
    }
    

    EDIT:

    Instead of simply ignoring the error and response objects, use them to diagnose the problem:

    NSHTTPURLResponse *response = nil;
    NSError *error = nil;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    if (error == nil && response.statusCode == 200) {
        return responseData;
    } else {
        NSLog(@"Error. Status code: %d Error message: %@", response.statusCode, error.localizedDescription);
        return nil;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP