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

一盞燈, 一片昏黃; 一簡書, 一杯淡茶。 守著那一份淡定, 品讀屬于自己的寂寞。 保持淡定, 才能欣賞到最美麗的風(fēng)景! 保持淡定, 人生從此不再寂寞

  • 使用AFNetworking進(jìn)行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=<NSHTTPURLResponse: 0x8f15f30> { 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]; 
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容