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

本文解决使用AFNetworking2.4.1时遇到的请求失败问题,通过修改代码中可接收的Content-Type,成功处理文本内容类型响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:@"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);
    }];

使用AFNetworking2.4.1时遇到以下问题 :

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=<NSHTTPURLResponse: 0x8f15f30> { URL: 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]; 


更多参考 :

What is the correct JSON content type?



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值