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];


是否成功了,成功了吧!可是新问题出现了  编码问题  假设server返回a 的话   你收到的 是<61>

这样  怎么能行呢?

当你用浏览器 去请求时 发现 响应头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];


原文地址:https://www.cnblogs.com/wgwyanfs/p/6774591.html