如何在JSON因AFJSONRequestOperation失败时获取responseString

时间:2013-08-02 19:20:04

标签: ios objective-c afnetworking nshttpurlresponse

当服务器无法发送有效的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 :)时,我发现写入失败有三个参数(但实际上有四个,第四个总是为零)。在这种情况下,有没有简单的方法将响应作为字符串获取?

1 个答案:

答案 0 :(得分:0)

如果responseStringnil,则表示您未从服务器接收任何数据,或者数据无法用于创建有效的NSString对象。

根据Cocoa error 3840的声音,它对应于NSJSONSerialization错误,我的猜测是服务器确实发回了空响应。