【问题标题】:AFNetworking returns a _NSCFData object instead of JSONAFNetworking 返回一个 _NSCFData 对象而不是 JSON
【发布时间】:2012-08-05 07:45:26
【问题描述】:

我正在向 AFHTTPClient 的实例发送以下消息。我希望成功块被发送一个 Foundation 对象(字典),但调试器显示 JSON 是一个 _NSCFData 对象。 This question on SO 声明我需要将 Accept 标头设置为“application/json”。好吧,我正在这样做,但 AFNetworking 仍然没有解码响应正文中的 JSON。如果我自己使用 NSJSONSerialization 解码 json,我会得到一个 NSDictionary,如我所料。我做错了什么?

[client setDefaultHeader:@"Accept" value:@"application/json"];
[client postPath:@"/app/open_connection/"
  parameters:params
     success:^(AFHTTPRequestOperation *operation, id JSON) {
         NSLog(@"successful login! %@", [JSON valueForKeyPath:@"status"]);
     }
     failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"error opening connection");
         NSAlert *alert = [NSAlert alertWithError:error];
         [alert runModal];
     }
];

注意:我正在使用 Django 在 Python 中对服务器进行编程。响应的内容类型是'application/json'

【问题讨论】:

  • 尝试添加[client registerHTTPOperationClass:[AFJSONRequestOperation class]];

标签: afnetworking


【解决方案1】:

当您使用 AFHTTPClient 和 JSON API 时,您通常需要设置所有这三个设置:

httpClient.parameterEncoding = AFJSONParameterEncoding;
[httpClient setDefaultHeader:@"Accept" value:@"application/json"];
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];

现在,当您使用客户端发出请求时,它会知道将响应解析为 JSON。

[httpClient postPath:@"/app/open_connection/"
          parameters:params
             success:^(AFHTTPRequestOperation *operation, id response) {
                 NSLog(@"JSON! %@", response);
             }
             failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             }];

这也是我发现的一个技巧。在NSError 对象中,您可以解析它并检索错误消息(如果 HTTP 响应有 JSON 错误消息):

failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSDictionary *JSON =
    [NSJSONSerialization JSONObjectWithData: [error.localizedRecoverySuggestion dataUsingEncoding:NSUTF8StringEncoding]
                                    options: NSJSONReadingMutableContainers
                                      error:nil];
           failureCallback(JSON[@"message"]);
}

【讨论】:

  • 无法弄清楚为什么 AFHTTPClient 返回 NSCFData - 结果是“application/json”没有设置。非常感谢!
【解决方案2】:

试试这个...我认为您的客户端设置可能有问题。

NSMutableURLRequest *request = [client requestWithMethod:@"POST" path:@"/app/open_connection/" parameters:params];

AFJSONRequestOperation *operation =
[AFJSONRequestOperation JSONRequestOperationWithRequest:request 
    success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        NSLog(@"successful login! %@", [JSON valueForKeyPath:@"status"]);
    }
    failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"error opening connection");
        NSAlert *alert = [NSAlert alertWithError:error];
        [alert runModal];
}];
[operation start];

【讨论】:

  • 可以的,应该是我的AFHTTPClient的配置有问题。
  • 对我来说,问题是内容被返回为 text/html。起初,我以为我可以简单地修改 AcceptableContentTypes 以添加 text/html,但这仍然会导致 NSCF 数据,因为显然它需要采用适当的 mime 类型才能使内容能够正确解析为 JSON 而不是一个大 blob文本。
猜你喜欢
  • 2013-12-29
  • 2013-07-07
  • 2017-09-22
  • 1970-01-01
  • 2021-01-11
  • 1970-01-01
  • 2015-09-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多