最近我看到不少AFNet的教程,我就在想怎么那么多人不明白怎么用AFNet。不少人回答的问题都是同样的怎么还有人不会用。我估计不少人都遇到了这个错误javascript
我运行了 这段代码html
AFHTTPRequestOperationManager * manager = [ AFHTTPRequestOperationManager manager ];java
[manager GET:@"http://211.154.151.249:8866/ogPortal/getCarType.do"parameters:Nilsuccess:^(AFHTTPRequestOperation *operation, id responseObject)json
{网络
NSLog(@"%@",responseObject);app
}spa
failure:^(AFHTTPRequestOperation *operation, NSError *error) {code
NSLog(@"error===%@",error);htm
}];教程
这是返回的错误提示
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];