AFNetworking无法解析JSON
2015-05-29 00:00
337 查看
摘要: AFNetworking无法解析JSON以前用的好端端的接口,今天访问居然出错了,但是再用浏览器测试,发现可以正常返回数据,甚是奇怪啊。下面是错误信息:获取服务器响应出错 error=Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=0x7fdfd8729680 {com.alamofire.serialization.response.error.response= { URL: http://172.16.1.31:7001/itom/getwork } { status code: 200, headers { "Cache-Control" = "no-cache"; "Content-Type" = "text/html;charset=UTF-8"; Date = "Mon, 24 Nov 2014 03:13:16 GMT"; "Transfer-Encoding" = Identity; "X-Powered-By" = "Servlet/2.5 JSP/2.1";} }, NSErrorFailingURLKey=http://172.16.1.31:7001/itom/getwork, com.alamofire.serialization.response.error.data=<5b7b2273 74617475 73223a22 73756363 65737322 2c226d73 67223a22 e799bbe9 9986e688 90e58a9f 227d5d>, NSLocalizedDescription=Request failed: unacceptable content-type: text/html}下面是百度出来的答案:I also encountered the same problem. This means that your server is sending
"text/html"instead of the already supported types. After a little search, my solution was to add
"text/html"to
acceptableContentTypesset in
AFURLResponseSerializationclass. Just search for "acceptableContentTypes" and add
@"text/html"to the set manually. Of course, the ideal solution will be to change the tpe from the server, but for that you will hade to talk with the server team.I hope this helps you. Best regards and less bugs as possible in the code.
op.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html”];
对应到自己的项目里面,我用的是AFNetworking这套网络请求包,需要改的是:
AFURLResponseSerialization.m文件
223行:self.acceptableContentTypes = [NSSetsetWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript", nil];加上蓝色部分,其实就是添加一种服务器返回的数据格式。另一种解决方法:AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", nil];加上蓝色部分即可!也就是为AFN允许接受的content-type。
相关文章推荐
- XML 与 JSON 优劣对比
- newtonsoft.json解析天气数据出错解决方法
- vbs 解析json jsonp的方法
- jquery JSON的解析方式示例介绍
- c#版json数据解析示例分享
- ASP JSON类文件的使用方法
- C#实现对Json字符串处理实例
- C#实现Json转Unicode的方法
- php解析json数据实例
- PHP6 先修班 JSON实例代码
- 关于JSON以及JSON在PHP中的应用技巧
- .Net中的json操作类用法分析
- js Object2String方便查看js对象内容
- C#中的DataSet、string、DataTable、对象转换成Json的实现代码
- Json实现异步请求提交评论无需跳转其他页面
- javascript格式化json显示实例分析
- jquery处理json数据实例分析
- jQuery向后台传入json格式数据的方法
- jquery中ajax调用json数据的使用说明
- jQuery遍历json中多个map的方法