AFNetworking - 偶尔获得"不可接受的内容类型:application / json"在http响应

时间:2015-08-14 16:23:39

标签: objective-c iphone afnetworking

我无法弄清楚为什么AFNetworking 2.0会抛出此消息。我的理解是application / json是默认的序列化方案,所以如果服务器返回带有content-type:application / json的JSON,为什么AFNetworking会抛出这个错误?

Failure with messages Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: application/json" UserInfo=0x17e2ed60 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x17e84100> { URL: http://XXX/XXX/XXX } { status code: 200, headers {
        Connection = "keep-alive";
        "Content-Type" = "application/json;charset=UTF-8";
        Date = "Fri, 14 Aug 2015 16:16:52 GMT";
        Server = "Apache-Coyote/1.1";
        "Transfer-Encoding" = Identity;
        "X-Application-Context" = application;
    } }, NSErrorFailingURLKey=http://XXX/XXX/XXX, NSLocalizedDescription=Request failed: unacceptable content-type: application/json, com.alamofire.serialization.response.error.data=<7b227374 61747573 223a3230 302c2263 6f646522 3a225636 594d227d>}

在代码块中注明了带有X&#s的URL。这有时只会发生,并导致请求失败。如果我发出另一个请求,它会成功。

我使用AFHTTPSessionManager单例来发出所有请求。

1 个答案:

答案 0 :(得分:8)

试试这个:

sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];