iOS中,修改AFNetworking源文件可接收text/plain的方法”Request failed: unacceptable content-type: text/plain”

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

一盏灯, 一片昏黄; 一简书, 一杯淡茶。 守着那一份淡定, 品读属于自己的寂寞。 保持淡定, 才能欣赏到最美丽的风景! 保持淡定, 人生从此不再寂寞

iOS中,修改AFNetworking源文件可接收text/plain的方法
  • 使用AFNetworking进行post请求
 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:@"[http://www.xiushuang.com/client/index.php?s=/Portal/p_banner/appinfo/comxiushuanglol_339_xs_android/index.json](http://www.xiushuang.com/client/index.php?s=/Portal/p_banner/appinfo/comxiushuanglol_339_xs_android/index.json)" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject){
        NSLog(@"JSON : %@",responseObject);
    }failure:^(AFHTTPRequestOperation *operation, NSError *error){
        NSLog(@"Error : %@",error);
    }];

使用AFNetworking3.0时遇到以下问题 :

Error : Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/plain" UserInfo=0x8f85590 {com.alamofire.serialization.response.error.response= { URL: [http://www.xiushuang.com/client/index.php?s=/Portal/p_banner/appinfo/comxiushuanglol_339_xs_android/index.json](http://www.xiushuang.com/client/index.php?s=/Portal/p_banner/appinfo/comxiushuanglol_339_xs_android/index.json) } { status code: 200, headers {
    "Cache-Control" = "maxage=3600";
    Connection = "keep-alive";
    "Content-Encoding" = gzip;
    "Content-Type" = "text/plain; charset=utf-8";
    Date = "Tue, 07 Oct 2014 06:29:12 GMT";
    Expires = "Tue, 07 Oct 2014 07:29:12 GMT";
    "Last-Modified" = "Tue, 07 Oct 2014 06:26:13 GMT";
    Server = "nginx/1.2.9";
    "Transfer-Encoding" = Identity;
    Vary = "Accept-Encoding";
} }

此时需要修改AFNetworking可接收的Content-Type,前往AFNetworking源代码目录找到AFURLResponseSerialization.m文件将里面的代码:

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

修改为:

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

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