AFNetworking - occasionally get “unacceptable content type: application/json” on http response

六月ゝ 毕业季﹏ 提交于 2020-01-04 02:28:07

问题


I can't figure out why this message is being thrown by AFNetworking 2.0. My understanding is that application/json is the default serialization scheme, so if the server returns JSON with content-type:application/json, why would AFNetworking throw this error?

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>}

Commented out URL's with X's in the code block. This only happens sometimes, and it causes the request to fail. If I issue another request, it succeeds.

I'm using an AFHTTPSessionManager singleton to issue all requests.


回答1:


Try this:

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


来源:https://stackoverflow.com/questions/32014588/afnetworking-occasionally-get-unacceptable-content-type-application-json-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!