AFNetworking - 解决办法

来源:互联网 发布:手机屏幕色彩校正软件 编辑:程序博客网 时间:2024/05/18 01:16
在进行网络请求时出现-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];manger.responseSerializer = [AFHTTPResponseSerializerserializer];是否成功了,成功了吧!但是新问题出现了  编码问题  如果服务器返回a 的话   你收到的 是<61>这样  怎么能行呢?当你用浏览器 去请求时 发现 响应头Content-Type: text/html;charset=UTF-8 是这样的但是afNetwork 请求是Content-Type:text/plain;charset=ISO-8859-1 是这样的  不一致了吧为什么 pc浏览器 访问的 和用afNetwork 访问的 不一致呢? 不了解 什么情况?接着发现  其实 添加 如下二句 即可  也不用去修改AFURLResponseSerialization.h 里面的东西manger.requestSerializer = [AFHTTPRequestSerializerserializer];manger.responseSerializer = [AFHTTPResponseSerializerserializer];把 收到的responseObject 转换一下 编码 就OK了NSData *doubi = responseObject;NSString *shabi =  [[NSString alloc]initWithData:doubi encoding:NSUTF8StringEncoding];
0 0