作為iOS攻城獅都知道,網(wǎng)絡(luò)請(qǐng)求有一個(gè)著名的框架,那就是AFNetWorking,當(dāng)發(fā)起網(wǎng)絡(luò)請(qǐng)求時(shí),我遇到了一個(gè)這樣bug
請(qǐng)求失敗--Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/plain" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x608000036220> { URL: https://api.weibo.com/oauth2/access_token } { status code: 200, headers {
"Cache-Control" = "no-cache";
"Content-Length" = 111;
"Content-Type" = "text/plain;charset=UTF-8";
Date = "Sun, 16 Jul 2017 05:45:50 GMT";
Expires = "Thu, 01 Jan 1970 00:00:00 GMT";
Pragma = "No-cache";
Server = "nginx/1.6.1";
} },
分析
unacceptable content-type: text/plain 不接受內(nèi)容類型,換句話來(lái)說(shuō)AFN不支持解析這種格式,
那怎么解決的呢?AFNetworking為什么能夠解析服務(wù)器返回的東西呢?
因?yàn)閙anager有一個(gè)responseSerializer屬性.它只設(shè)置了一些固定的解析格式.其中不包含text/plain這種數(shù)據(jù)的格式.因?yàn)榻馕鰣?bào)錯(cuò)了.
我們來(lái)看一下AFNetworking解析格式的底層:
self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", nil];
沒(méi)有后臺(tái)提供的格式:text/plain,
那么,我們自己去修改,現(xiàn)在我發(fā)個(gè)gif圖看具體如何操作

打印結(jié)果如下
f1326ddff76840101624cc777012c819&from=844b&vit=fps https://m.baidu.com/?code=f1326ddff76840101624cc777012c819&from=844b&vit=fps
2017-07-16 14:56:11.045 weibo[6532:292589] 請(qǐng)求成功--{
"access_token" = "2.00TP4yAG0HVdgt7874226d93SnuPeD";
"expires_in" = 129834;
"remind_in" = 129834;
uid = 5511186043;
}