最近我看到不少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];