我在我自己封裝的AFNetworking頭里面已經添加了types@"text/html" 但是一直繼續(xù)報錯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遥缕,說明請求成功了趣效,我在后臺同事那里也顯示他那邊獲取到數(shù)據(jù)返回了,但是我這邊就是拿不到谎痢,我第一想法是他那邊傳給我的json數(shù)據(jù)格式錯誤了崇渗,但是除了這個接口以外,前面的18個接口都是正常的赛糟,就是最后的這2個接口是錯誤的睁搭。
我馬上想到用原生的試試,我用了原生的能成功
但是為什么這個AFN的不行呢谆沃,我已經在我寫的分類里面添加了類方法的type類型了钝凶,為甚還是錯誤的呢,我在多篇文章中唁影,終于找到了這個:
全局搜索文件AFURLResponseSerialization.m
文件228行 給這個AFNetworking添加請求類型
self.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html", @"text/json", @"text/javascript", nil];
為此,我把第三方的AFN從pod中刪除了 加載本地的文件里面掂名。
中午回來据沈,做了測試,發(fā)現(xiàn)這個接口的錯誤從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];
獲取到的參數(shù)為_NSInlineData格式
需要轉為JSon的字典格式
NSData * data = responseObject;
NSDictionary * dict =[NSJSONSerialization JSONObjectWithData:data options:NSUTF8StringEncoding error:nil];
*********************************perfect*********************************
以上為本人原創(chuàng)饺蔑,希望對大家有所幫助锌介。不足之處,敬請斧正猾警!
轉載請?zhí)砑映鎏帯?/p>