我在我自己封装的AFNetworking头里面已经添加了types@"text/html" 但是一直继续报错Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/plain" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x61800023fbe0> { URL: http://172.16.0.104:85/Handlers/Phone/PhoneHandler.ashx?visit=GetPatrolRoom } { status code: 200, headers {
"Cache-Control" = private;
"Content-Length" = 343;
"Content-Type" = "text/plain; charset=utf-8";
Date = "Thu, 20 Apr 2017 03:12:32 GMT";
Server = "Microsoft-IIS/7.5";
"X-AspNet-Version" = "4.0.30319";
"X-Powered-By" = "ASP.NET";
} }, NSErrorFailingURLKey
这里code200,说明请求成功了,我在后台同事那里也显示他那边获取到数据返回了,但是我这边就是拿不到,我第一想法是他那边传给我的json数据格式错误了,但是除了这个接口以外,前面的18个接口都是正常的,就是最后的这2个接口是错误的。
我马上想到用原生的试试,我用了原生的能成功
但是为什么这个AFN的不行呢,我已经在我写的分类里面添加了类方法的type类型了,为甚还是错误的呢,我在多篇文章中,终于找到了这个:
全局搜索文件AFURLResponseSerialization.m
文件228行 给这个AFNetworking添加请求类型
self.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html", @"text/json", @"text/javascript", nil];
为此,我把第三方的AFN从pod中删除了 加载本地的文件里面。
中午回来,做了测试,发现这个接口的错误从4个变成1个了
[0] (null) @"NSDebugDescription" : @"JSON text did not start with array or object and option to allow fragments not set."
在对应的这个接口请求的manager添加一句代码
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
获取到的参数为_NSInlineData格式
需要转为JSon的字典格式
NSData * data = responseObject;
NSDictionary * dict =[NSJSONSerialization JSONObjectWithData:data options:NSUTF8StringEncoding error:nil];
*********************************perfect*********************************
以上为本人原创,希望对大家有所帮助。不足之处,敬请斧正!
转载请添加出处。