在使用AFNetworking的时候可能会碰到下面的错误:
{ status code: 200, headers {
"Content-Length" = 14;
"Content-Type" = "text/plain;charset=utf-8";
Date = "Thu, 22 May 2014 10:37:50 GMT";
Server = "Apache-Coyote/1.1";
"Set-Cookie" ="JSESSIONID=C0DFED60A154557F8386E62AB2A066CE; Path=/FHJRDT";
} }, NSLocalizedDescription=Request failed:unacceptable content-type: text/plain}
此时需要修改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];
-----------------------分界线-----------------------------
以上方法虽然可以实现,但以本人最近的只是来看实在是下下策。
更好的办法是这样:
遇到这个问题的人很有可能是代码中没有添加一段话:manager.responseSerializer = [AFHTTPResponseSerializer serializer];
示例代码如下:
NSString *urlStr =@"http://xxxx";
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer]; //很重要,去掉就容易遇到错误,暂时还未了解更加详细的原因
[manager GET:urlStr parameters:@{@"username":@"xxx",@"password":@"xxx"}success:^(AFHTTPRequestOperation *operation,idresponseObject) {
// 解析成功
NSLog(@"JSON: %@", responseObject);
}failure:^(AFHTTPRequestOperation *operation,NSError *error) {
// 解析失败
NSLog(@"error:++++%@",error.localizedDescription);
}];}