网络请求的错误:AFNetworking在发送请求时出现的错误
Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/plain" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7ff512723940> { URL: https://api.weibo.com/oauth2/access_token } { status code: 200, headers {
"Cache-Control" = "no-cache";
"Content-Length" = 117;
"Content-Type" = "text/plain;charset=UTF-8";
Date = "Thu, 09 Feb 2017 05:59:36 GMT";
Expires = "Thu, 01 Jan 1970 00:00:00 GMT";
Pragma = "No-cache";
Server = "nginx/1.6.1";
"api-server-ip" = "172.15.138.68";
} }, NSErrorFailingURLKey=https://api.weibo.com/oauth2/access_token,
NSLocalizedDescription=Request failed: unacceptable content-type:
text/plain}
这种错误是由于AFNetworking不支持text/plain导致的,在自定义的网络工具类中添加即可
//单例
static let shareInstance:NetworkingTools? = {
//https://api.weibo.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&response_type=code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI
//创建基础URL
let url = NSURL.init(string: "")
//创建单例
let instance = NetworkingTools.init(baseURL: url, sessionConfiguration: NSURLSessionConfiguration.defaultSessionConfiguration())
//instance.responseSerializer.acceptableContentTypes =
instance.responseSerializer.acceptableContentTypes = Set(arrayLiteral: ''text/plain'',"application/json","text/json","text/javascript")
return instance
}()
搜索text/json,就会跳转AFN框架,可以看到AFN支持的类型如下:
self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", nil];
可以看到它并不支持text/plain,需要手动进行添加