美文网首页iOS学习IOS 黑科技
iOS 使用AFNetworking遇到异常 Request f

iOS 使用AFNetworking遇到异常 Request f

作者: wzf_taker | 来源:发表于2016-05-09 18:00 被阅读16991次

iOS 使用AFNetworking遇到错误 Request failed: unacceptable content-type: text/html
错误日志:
Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7f999be478d0> { URL: myUrlXXXXXX } { status code: 200, headers {
** "Cache-Control" = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";**
** Connection = "keep-alive";**
** "Content-Encoding" = gzip;**
** "Content-Type" = "text/html; charset=utf-8";**
** Date = "Tue, 24 Nov 2015 09:06:04 GMT";**
** Expires = "Thu, 19 Nov 1981 08:52:00 GMT";**
** Pragma = "no-cache";**
** Server = "nginx/1.1.19";**
** "Set-Cookie" = "PHPSESSID=qdvtek1k91oeva2u8fats39l93; path=/";**
** "Transfer-Encoding" = Identity;**
** "X-Powered-By" = "PHP/5.3.10-1ubuntu3.21";**
} }, NSErrorFailingURLKey=http:URL: myUrlXXXXXX, com.alamofire.serialization.response.error.data=<7b226572 726f725f 636f6465 223a302c 22657272 6f725f6d 7367223a 22222c22 75706c6f 61645f75 726c223a 22687474 703a5c2f 5c2f7777 772e7467 7370792e 636f6d5c 2f6c6f6f 70657273 5c2f7365 72766572 5c2f6269 6e5c2f63 7573746f 6d65725c 2f75706c 6f61645c 2f373332 31746f75 7869616e 672e6a70 67227d>, NSLocalizedDescription=Request failed: unacceptable content-type: text/html}


原因:
不可接受的内容类型 “text/html
解决方案:
AFJSONResponseSerializer.m中,222行左右
把 这句: self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", nil];
修改为:
self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];

在进行网络请求时出现-1016 是因为只支持
text/json,application/json,text/javascript
你可以添加text/html
一劳永逸的方法是 在
AFURLResponseSerialization.h
里面搜索
self.acceptableContentTypes
然后 在里面 添加
@"text/html",@"text/plain"
这样就可以解决-1016的错误了
但是随之而来的是3840错误
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x9152780 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
你会发现出现此错误
怎么办呢
添加如下语句 就可以解决问题了
manger.requestSerializer= [AFHTTPRequestSerializerserializer];

相关文章

网友评论

  • 至恒之狐:不知道你们遇到过请求导致后台路由发生了变化,导致返回了一个界面,本来是一组json数据。就算按照作者的方法反而适得其反,当然能获取页面的html代码,但是Android能返回json数据。
  • 4072b043db49:看到16年的问题,原谅我潜水这么多年,不得不登录吐槽了。
    传回来的json是text/html,是不可接受格式,那么就是说:
    1、你要么让AFNetworking接受传回值是text/html格式;
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
    2、要么就把text/html这种json序列化(serializer)成二进制格式(NSData),然后再解析出来,就是用NSData当中间层。
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    拿到responseObject后,如果外层是字典:
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];

    这里并没有审核机制,所以这里“大神”很多,这种问题,读读英文提示,想想谢希仁的《计算机网络》第6章 应用层,第248页,很好理解。

    (这里是广告时间):我写了一个自己的个人技术网站,筹备很久了,才开始写,记录了从0起步到现在,如果能找到我的话,你就能访问到,这里就不再说了。以免真的变成广告。
    摩卡奇:这个才是正解吧, 为什么改afn里面的东西呢. 本来数据给的text/html就用AFHTTPResponseSerializer解就可以了
  • _君莫笑_:你说的方法不好使还是报错
    Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
  • 神的旨意:AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain", nil];
    我的是增加了@"text/plain"
    增加manager.requestSerializer = [AFHTTPRequestSerializer serializer]; 这个是有用的
    Juice007:@神的旨意 正解
    c82b4b9b9cb3:@神的旨意 大神这样写可以吗?
  • 修_远:请求数据失败,失败原因:
    Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
    悖论13:manager.responseSerializer= [AFHTTPResponseSerializer serializer];
    _君莫笑_:这个问题最后怎么解决了?
    c82b4b9b9cb3:@道陌人 请问最后怎么解决了?
  • 修_远:还是不行啊
  • 2d8bf2f2c677:感谢,用这个方法解决了!
  • 96e4766b589a:你解决了么
  • 我是要成为大神的男人:是的,你最后怎么解决的呢。
  • 95e764559ca9: manger.requestSerializer= [AFHTTPRequestSerializerserializer];好像不对啊。我试了还是报这样的错误
    a8304ea8f57e:楼主,你解决了这个问题吗?
    96e4766b589a:@秋雨寒路人 你解决了码

本文标题:iOS 使用AFNetworking遇到异常 Request f

本文链接:https://www.haomeiwen.com/subject/tcuerttx.html