AFNetworking 2.0:NSLocalizedDescription =请求失败:不可接受的内容类型:text/html [英] AFNetworking 2.0: NSLocalizedDescription=Request failed: unacceptable content-type: text/html
问题描述
我试图从Apple主页上获取 html.
I tried to GET html from Apple homepage.
AFHTTPRequestOperationManager *manager
= [AFHTTPRequestOperationManager manager];
[manager GET:@"http://www.apple.com/jp" parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"HTML: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
此操作失败,并显示以下错误消息:
This failed with error message as below:
2014-02-07 14:54:22.922 Tests[1833:70b] Error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=0x8a32650 {NSErrorFailingURLKey=http://www.apple.com/jp/, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xea2e1a0> { URL: http://www.apple.com/jp/ } { status code: 200, headers {
"Accept-Ranges" = bytes;
"Cache-Control" = "max-age=237";
Connection = "keep-alive";
"Content-Encoding" = gzip;
"Content-Length" = 4402;
"Content-Type" = "text/html; charset=UTF-8";
Date = "Fri, 07 Feb 2014 05:52:51 GMT";
Expires = "Fri, 07 Feb 2014 05:56:48 GMT";
Server = Apache;
Vary = "Accept-Encoding";
Via = "1.0 proxy1.screen.co.jp (squid)";
"X-Cache" = "MISS from proxy1.screen.co.jp";
"X-Cache-Lookup" = "HIT from proxy1.screen.co.jp:8080";
} }, NSLocalizedDescription=Request failed: unacceptable content-type: text/html}
我找到了消息Request failed: unacceptable content-type: text/html
,所以我知道我必须让text/html可以接受.然后,我添加了以下行:manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
.
I found the message Request failed: unacceptable content-type: text/html
so I understand I have to let text/html acceptable. Then, I added the line: manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
.
AFHTTPRequestOperationManager *manager
= [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes
= [NSSet setWithObject:@"text/html"]; // ###### ADDED LINE ######
[manager GET:@"http://www.apple.com/jp" parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"HTML: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
但这也失败了.
2014-02-07 14:55:48.927 Tests[1873:70b]
Error: 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=0xfa7b870 {NSDebugDescription=
JSON text did not start with
array or object and option to allow fragments not set.}
谢谢您的帮助.
推荐答案
问题是AFHTTPRequestOperationManager
默认为AFJSONResponseSerializer
的responseSerializer
.通过更改acceptableContentTypes
,您可以更改将接受的内容类型,但不会更改AFJSONResponseSerializer
仍会在响应中寻找JSON的事实.
The problem is that AFHTTPRequestOperationManager
defaults to a responseSerializer
of AFJSONResponseSerializer
. By changing acceptableContentTypes
, you are changing which content types will be accepted, but you're not changing the fact that AFJSONResponseSerializer
will still look for JSON in the response.
您可能只想将responseSerializer
设置为AFHTTPResponseSerializer
,以便它不再尝试解析JSON(并将自动接受text/html
):
You might want to just set the responseSerializer
to AFHTTPResponseSerializer
, so that it will no longer try to parse the JSON (and will automatically accept text/html
):
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
这篇关于AFNetworking 2.0:NSLocalizedDescription =请求失败:不可接受的内容类型:text/html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!