ios AFNetworking 2.0 - “不可接受的内容类型:文本/纯文本”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20807926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AFNetworking 2.0 - "unacceptable content-type: text/plain"
提问by TomorrowPlusX
I'm using AFNetworking 2.0 to read JSON from a service I'm building (on localhost
for now) in Node. Pretty normal stuff.
我正在使用 AFNetworking 2.0 从我在 Node.js 中构建(localhost
现在)的服务中读取 JSON 。很正常的东西。
Node is sending JSON like so:
节点发送 JSON 如下:
res.setHeader('Content-Type','application/json');
res.end( JSON.stringify(...));
My iOS first-pass code is attempting to read that data like so:
我的 iOS 第一遍代码正在尝试像这样读取数据:
typedef void(^NextBlock)();
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager GET:self.newestTimestampURL.absoluteString
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
//NSDictionary *response =
NSLog(@"got %@", responseObject );
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"fail %@", error );
}];
This is the error I'm getting:
这是我得到的错误:
Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/plain" UserInfo=0xb783e30 {NSErrorFailingURLKey=http://localhost:3000/api/v1/log/newest, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xb656a70> { URL: http://localhost:3000/api/v1/log/newest } { status code: 200, headers {
Connection = "keep-alive";
ContentType = "application/json";
Date = "Fri, 27 Dec 2013 20:58:50 GMT";
"Transfer-Encoding" = Identity;
} }, NSLocalizedDescription=Request failed: unacceptable content-type: text/plain}
I can curl (-i) the url http://localhost:3000/api/v1/log/newest
and get the data I'm expecting, and it's application/json as expected. If I load that data in my web browser, I get JSON as expected per dev tools inspector.
我可以卷曲 (-i) urlhttp://localhost:3000/api/v1/log/newest
并获取我期望的数据,它是预期的 application/json。如果我在 Web 浏览器中加载该数据,我会按照开发工具检查器的预期获得 JSON。
But using AFNetworking, I get this mysterious "unacceptable content-type: text/plain" error. Any idea?
但是使用 AFNetworking,我得到了这个神秘的“不可接受的内容类型:文本/纯文本”错误。任何的想法?
NOTE: I've never used AFNetworking before, so I'm probably using it incorrectly.
注意:我以前从未使用过 AFNetworking,所以我可能没有正确使用它。
回答by KIO
It seems that the server is sending "text/html", and this type is not supported by default. Add @"text/html" for "acceptableContentTypes"
好像是服务器在发送“text/html”,默认不支持这种类型。为“acceptableContentTypes”添加@“text/html”
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
回答by shebelaw
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
work for me.
为我工作。
回答by AaronTKD
Work for me.
为我工作。
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
回答by ABS
I have followed the answer posted by @KIO but it did not work in my scenario.
我遵循了@KIO 发布的答案,但它在我的场景中不起作用。
After going with many answers i was able to find solution which worked for me.
在回答了很多答案之后,我找到了对我有用的解决方案。
AFHTTPRequestOperationManager *operation = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:url];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
AFJSONResponseSerializer *jsonResponseSerializer = [AFJSONResponseSerializer serializer];
NSMutableSet *jsonAcceptableContentTypes = [NSMutableSet setWithSet:jsonResponseSerializer.acceptableContentTypes];
[jsonAcceptableContentTypes addObject:@"text/plain"];
jsonResponseSerializer.acceptableContentTypes = jsonAcceptableContentTypes;
operation.responseSerializer = jsonResponseSerializer;
回答by rsc
Just use:
只需使用:
SWIFT:
迅速:
manager.responseSerializer.acceptableContentTypes = NSSet(object: "text/plain") as Set<NSObject>
OBJ-C:
OBJ-C:
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
回答by Lê Tr??ng Giang
NSURL *url = [NSURL URLWithString:RSS_WORLD_NEWS];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
// Make sure to set the responseSerializer correctly
operation.responseSerializer = [AFXMLParserResponseSerializer serializer];
operation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/rss+xml"];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSXMLParser *XMLParser = (NSXMLParser *)responseObject;
[XMLParser setShouldProcessNamespaces:YES];
XMLParser.delegate = self;
[XMLParser parse];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[MBProgressHUD hideHUDForView:self.view animated:YES];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving contents"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
}];
[operation start];
work for me
为我工作
回答by Sourabh Sharma
Updated Solution for Swift.
Swift 的更新解决方案。
If you are using AFNetworking in Swift. Then this solution might help you. It will accept most of the content types.
如果您在 Swift 中使用 AFNetworking。那么这个解决方案可能会帮助你。它将接受大多数内容类型。
let manager=AFHTTPRequestOperationManager()
manager.responseSerializer = AFJSONResponseSerializer(readingOptions: NSJSONReadingOptions.AllowFragments) as AFJSONResponseSerializer
manager.requestSerializer = AFJSONRequestSerializer() as AFJSONRequestSerializer
manager.responseSerializer.acceptableContentTypes = NSSet(objects:"application/json", "text/html", "text/plain", "text/json", "text/javascript", "audio/wav") as Set<NSObject>