iOS 使用AFNetworking遇到异常 Request failed: unacceptable content-type: text/html

时间:2021-6-3 作者:qvyue

iOS 使用AFNetworking遇到错误 Request failed: unacceptable content-type: text/html
错误日志:
Error Domain=com.alamofire.error.serialization.response Code=-1016 “Request failed: unacceptable content-type: text/html” UserInfo={com.alamofire.serialization.response.error.response= { URL: myUrlXXXXXX } { status code: 200, headers {
** “Cache-Control” = “no-store, no-cache, must-revalidate, post-check=0, pre-check=0”;**
** Connection = “keep-alive”;**
** “Content-Encoding” = gzip;**
** “Content-Type” = “text/html; charset=utf-8”;**
** Date = “Tue, 24 Nov 2015 09:06:04 GMT”;**
** Expires = “Thu, 19 Nov 1981 08:52:00 GMT”;**
** Pragma = “no-cache”;**
** Server = “nginx/1.1.19”;**
** “Set-Cookie” = “PHPSESSID=qdvtek1k91oeva2u8fats39l93; path=/”;**
** “Transfer-Encoding” = Identity;**
** “X-Powered-By” = “PHP/5.3.10-1ubuntu3.21”;**
} }, NSErrorFailingURLKey=http:URL: myUrlXXXXXX, com.alamofire.serialization.response.error.data=, NSLocalizedDescription=Request failed: unacceptable content-type: text/html}


原因:
不可接受的内容类型 “text/html
解决方案:
AFJSONResponseSerializer.m中,222行左右
把 这句: self.acceptableContentTypes = [NSSet setWithObjects:@”application/json”, @”text/json”, @”text/javascript”, nil];
修改为:
self.acceptableContentTypes = [NSSet setWithObjects:@”application/json”, @”text/json”, @”text/javascript”,@”text/html”, nil];

在进行网络请求时出现-1016 是因为只支持
text/json,application/json,text/javascript
你可以添加text/html
一劳永逸的方法是 在
AFURLResponseSerialization.h
里面搜索
self.acceptableContentTypes
然后 在里面 添加
@”text/html”,@”text/plain”
这样就可以解决-1016的错误了
但是随之而来的是3840错误
Error Domain=NSCocoaErrorDomain Code=3840 “The operation couldn’t be completed. (Cocoa error 3840.)” (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x9152780 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
你会发现出现此错误
怎么办呢
添加如下语句 就可以解决问题了
manger.requestSerializer= [AFHTTPRequestSerializerserializer];

声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:qvyue@qq.com 进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。