当服务器无法发送有效的JSON响应(fe.php echos某些临时变量或出错)时,我很难得到responseString
。我正在使用AFJSONRequestOperation
这样的AFNetwoking
:
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id responseObject) {
NSLog(@"object: %@", responseObject);
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id responseString) {
NSLog(@"failure: %@", responseString);
}
];
responseString
失败的情况始终为nil
。当我试图阅读文档(http://cocoadocs.org/docsets/AFNetworking/1.3.1/Classes/AFJSONRequestOperation.html#//api/name/JSONRequestOperationWithRequest:success:failure :)时,我发现写入失败有三个参数(但实际上有四个,第四个总是为零)。在这种情况下,有没有简单的方法将响应作为字符串获取?
答案 0 :(得分:0)
如果responseString
为nil
,则表示您未从服务器接收任何数据,或者数据无法用于创建有效的NSString
对象。
根据Cocoa error 3840
的声音,它对应于NSJSONSerialization
错误,我的猜测是服务器确实发回了空响应。