Php 我的JSONObjectWithData为空
我的JSON向我发送空值时出现问题 我认为我的PhP Web服务没有问题,因为我的同事是PhP专家,他没有看到任何错误 就在我的Obj-C代码下面Php 我的JSONObjectWithData为空,php,ios,objective-c,iphone,json,Php,Ios,Objective C,Iphone,Json,我的JSON向我发送空值时出现问题 我认为我的PhP Web服务没有问题,因为我的同事是PhP专家,他没有看到任何错误 就在我的Obj-C代码下面 dispatch_queue_t downloadQueue = dispatch_queue_create("downloader", NULL); dispatch_async(downloadQueue, ^{ NSData *result = self.envoyerLaDemande;
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);
还有PHP脚本
$data = array();
if(condition == 1)
{
$data["result"] = "ok";
}
else
{
$data["result"] = "error";
}
$data["result"] = "coucou";
}
echo json_encode($data);
还有methode特使Lademande
-(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;
}
问题是您正在执行两个
dispach\u async
在第一个示例中,您告诉操作系统“在后台执行此代码”,这将开始在后台加载请求。然而,紧接着这个代码,你告诉操作系统在主线程上执行一段代码。这段代码访问尚未设置的结果
变量,因为它仍在后台加载
如果坚持使用这些调度,只需将调度移回主队列即可。但是,考虑查看或至少在您的代码> NSURLCONNECT/<代码>上使用该方法。< /P>
此代码应该适用于您当前的场景
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);
}
编辑:
不要简单地忽略错误
和响应
对象,而是使用它们来诊断问题:
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;
}
self.envoyerLaDemande
看起来像什么?@Batman请提供更多信息。@Rick self.envoyerLaDemande是我调用Web服务时的方法,我编辑我的post@learner我已经编辑了我的post@Batman使用chrome add-on postman确认您的服务正常工作,并使用NSURLConnectionDataDelegate方法(didReceiveData,didFailWithError),通过断点查看流程。感谢您的回答,但它不起作用,问题仍然是sameIs结果也为零?如果是,则问题可能是与服务器的通信。是的,结果也为零。与服务器的通信?除非我希望Web服务执行的操作是好的,但它不会向我发送return值查看sendSynchronousRequest的文档。它说如果无法创建连接或下载失败,将返回nil。
。因此下载时存在一些问题。我建议查看错误
和响应
对象,而不是简单地忽略它们。我输入了您编写的代码,但我不知道没有任何关于错误的nslog,它只会向我发送响应数据;)