AFNetWorking2.0 post 出现code=-1016错误怎么解决

Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable con

在使用AFNetworking 2.0  的时候本来一切很顺畅,但是中途遇到几个比较坑的地方

这里分享一下爬坑经历,忘读者不能速爬坑!

在发送请求后,NSURLSessionDataTask一直报错:

Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html"

经过一番网上排查,网上有人说是AF2.0本身的问题,解析格式不全,所以需要在AF的源文件AFURLResponseSerialization.m中修改代码就能解决:

修改文件223行处:

[objc] view plaincopy

self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", nil nil];

为:

[objc] view plaincopy

self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil nil];

即可!笔者试过确实可以,但是AF还在持续更新的类库,不宜随意修改,特别是在用了CocoaPods之后,如果之后更新库,此类错误又会重复出现,随后笔者发现acceptableContentTypes是一个开放的属性,既然这样,就证明acceptableContentTypes可以在外部被修改,所以可以在

初始化HttpClient单利的时候改变这一值:

[objc] view plaincopy

- (instancetype)initWithBaseURL:(NSURL *)url {

if (self = [super initWithBaseURL:url]) {

self.responseSerializer = [AFJSONResponseSerializer serializer];

self.requestSerializer.timeoutInterval = TimeoutInterval;

self.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil nil];

[self setHTTPHeader];  // 可在此处设置Http头信息

}

return self;

}

备注:这里与你响应的网址的类型有关,也就是说,你请求的网址为@"text/html"。一般不会出现这种情况,这主要是后台太水,后台数据生成的有问题。


2.网络提示中文

在info.plist中增加Localization native development region String类型 值为China

你可能感兴趣的:(AFNetWorking2.0 post 出现code=-1016错误怎么解决)