AFNetworking-偶尔会收到“不可接受的内容类型:application / json”在http响应 [英] AFNetworking - occasionally get "unacceptable content type: application/json" on http response

查看:264
本文介绍了AFNetworking-偶尔会收到“不可接受的内容类型:application / json”在http响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么AFNetworking 2.0会抛出此消息。我的理解是application / json是默认的序列化方案,因此如果服务器返回内容类型为:application / json的JSON,为什么AFNetworking会引发此错误?

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

在代码块中注释了带有X的URL。仅在某些情况下会发生这种情况,并导致请求失败。如果我发出另一个请求,它将成功。

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.

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

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

推荐答案

尝试以下操作:

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

这篇关于AFNetworking-偶尔会收到“不可接受的内容类型:application / json”在http响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆