【发布时间】:2013-08-02 19:20:04
【问题描述】:
当服务器未能发送有效的 JSON 响应时,我很难获得 responseString(fe. php 回显一些临时变量或出现问题)。我正在使用来自AFNetwoking 的AFJSONRequestOperation,如下所示:
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:) 时,我发现有人写到失败会得到三个参数(但实际上是四个,第四个始终为零)。在这种情况下,有什么简单的方法可以将响应作为字符串获取?
【问题讨论】:
-
失败时你得到什么反应。
-
响应对象指示的内容长度是多少?
-
2013-08-02 21:06:12.250 app[44769:c07] responseCode:200,错误:操作无法完成。 (Cocoa 错误 3840。)(null)如何从响应中获取内容长度?
-
也许有点跑题了,但是当我遇到这种情况时,我总是使用 cURL 来测试服务器请求并在终端中打印输出。这有助于我捕捉可能的回声或通知。
标签: ios objective-c afnetworking nshttpurlresponse