AFNetwork 2.0在请求时报错code=-1016 和 3840

在进行网络请求时出现-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 = [AFHTTPRequestSerializer serializer];
manger.responseSerializer = [AFHTTPResponseSerializer serializer];

是否成功了,成功了吧!但是新问题出现了,当你用浏览器去请求时发现响应头Content-Type: text/html;charset=UTF-8 是这样的,但是afNetwork请求是Content-Type:text/plain;charset=ISO-8859-1 是这样的 不一致了吧,为什么 pc浏览器 访问的 和用afNetwork 访问的不一致呢? 不了解什么情况?接着发现其实添加如下二句即可,也不用去修改AFURLResponseSerialization.h 里面的东西

manger.requestSerializer = [AFHTTPRequestSerializer serializer];
manger.responseSerializer = [AFHTTPResponseSerializer serializer];
把收到的responseObject 转换一下 编码 就OK了

NSData *doubi = responseObject;
NSString *shabi = [[NSStringalloc]initWithData:doubi encoding:NSUTF8StringEncoding];

Leave a Reply