iOS—修改AFNetworking源文件可接收text/plain的方法
iOS—修改AFNetworking源文件可接收text/plain的方法 在使用AFNetworking的时候可能会碰到下面的错误: { status code: 200, headers { "Content-Length" = 14; "Content-Type" = "text/plain;charse
在使用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 = [AFHTTPResponseSerializerserializer];
示例代码如下:
NSString *urlStr =@"http://xxxx";
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManagermanager];
manager.responseSerializer = [AFHTTPResponseSerializerserializer]; //很重要,去掉就容易遇到错误,暂时还未了解更加详细的原因
[managerGET:urlStr parameters:@{@"username":@"xxx",@"password":@"xxx"}success:^(AFHTTPRequestOperation *operation,id responseObject) {
// 解析成功
NSLog(@"JSON: %@", responseObject);
}failure:^(AFHTTPRequestOperation *operation,NSError *error) {
// 解析失败
NSLog(@"error:++++%@",error.localizedDescription);
}];}
更多推荐
所有评论(0)