最近我看到很多AFNet的教程,我就在想怎么那么多人不明白怎么用AFNet。很多人回答的问题都是一样的怎么还有人不会用。我估计很多人都遇到了这个错误
我运行了 这段代码
AFHTTPRequestOperationManager * manager = [ AFHTTPRequestOperationManager manager ];
[manager GET:@"http://211.154.151.249:8866/ogPortal/getCarType.do"parameters:Nilsuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"%@",responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error===%@",error);
}];
这是返回的错误提示
AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x8a9b9b0> { URL: http://211.154.151.249:8866/ogPortal/getCarType.do } { status code: 200, headers {
"Content-Language" = "zh-HANS";
"Content-Type" = "text/html;charset=UTF-8";
Date = "Tue, 29 Apr 2014 02:40:01 GMT";
Server = "Apache-Coyote/1.1";
"Set-Cookie" = "JSESSIONID=CE41FCD6855EB887488C368B2952B798; Path=/ogPortal/; HttpOnly";
"Transfer-Encoding" = Identity;
} }, NSLocalizedDescription=Request failed: unacceptable content-type: text/html}
这段疑似是说 你这个 东西是text/html的 我现在不认识这个格式的 没有办法返回。
此时怎么办呢?
首先 我们全局搜索 self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",nil];
然后我们加上他不认识的格式 self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html"nil];
此时编译成功了吧!!
此处代码太多 少截取点啦~
014-04-29 10:45:21.067 AFNetWorkingNowText[3083:70b] {
msgCode = 0;
result = (
{
child = (
{
child = (
{
id = 954;
ischild = 1;
name = "2013\U6b3e 6.0L Coupe";
}
);
id = 126;
ischild = 0;
name = "A \U963f\U65af\U987f\U00b7\U9a6c\U4e01DB9";
},
{
child = (
{
id = 955;
ischild = 1;
name = "2007\U6b3e 6.0 Manual Coupe";
},
{
id = 956;
ischild = 1;
name = "2009\U6b3e 6.0 Touchtronic Coupe";
},
有人说 什么 改源代码??? 以后错了 怎么办~! 好吧 你要是不想改动源代码 此处也可以这么写
加上这么一句话 :( manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];)
AFHTTPRequestOperationManager * manager = [ AFHTTPRequestOperationManager manager ];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[manager GET:@"http://211.154.151.249:8866/ogPortal/getCarType.do"parameters:Nilsuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"%@",responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error===%@",error);
}];
然后运行 成功 ~! 不用谢小编,小编的名字叫雷锋~!
在进行网络请求时出现-1016 是因为只支持
text/json,application/json,text/javascript
你可以添加text/html
一劳永逸的方法是 在
AFURLResponseSerialization.h
里面搜索
self.acceptableContentTypes
然后 在里面 添加
@"text/html",@"text/plain"
这样就可以解决-1016的错误了
但是随之而来的是3840错误
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x9152780 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
你会发现出现此错误
怎么办呢
添加如下语句 就可以解决问题了
manger.requestSerializer= [AFHTTPRequestSerializerserializer];
manger.responseSerializer= [AFHTTPResponseSerializerserializer];