最近在做項目使用AFNetworking第三方庫時總是遇到Request failed: unacceptable content-type: text/html" UserInfo=0x7c05169 問題。所以就記錄一下
網上的解決方法是:
對應到自己的項目裏面,我用的是AFNetworking這套網絡請求包,需要改的是:
AFURLResponseSerialization.m文件
223行:
self.acceptableContentTypes = [NSSetsetWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript", nil];
加上藍色部分,其實就是添加一種服務器返回的數據格式。
試了一下不適用,已改就會發生程序崩潰,所以我發現大多數情況都不是上面的方法能解覺得。
下面是我的錯誤代碼:
NSString *mypath=[NSString stringWithFormat:@"%@%@",YANZURL,texFiled1.text];
AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
manager.responseSerializer=[AFJSONResponseSerializer serializer];
[manager GET:mypath parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
NSLog(@"%@ %@",dic[@"msg"],mypath);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"請求失敗%@",error);
}];
這個錯誤就在:
manager.responseSerializer=[AFJSONResponseSerializer serializer];
原來是AFJSONResponseSerializer用錯了,應該是 AFHTTPResponseSerializer
所以我總結了一下,這個問題大多數都是block代碼塊前面部分用錯API了。