简体   繁体   English

AFNetworking 2.0 - “不可接受的内容类型:text / plain”

[英]AFNetworking 2.0 - “unacceptable content-type: text/plain”

I'm using AFNetworking 2.0 to read JSON from a service I'm building (on localhost for now) in Node. 我正在使用AFNetworking 2.0从我正在构建的服务中读取JSON(现在在localhost上)。 Pretty normal stuff. 很正常的东西。

Node is sending JSON like so: Node正在发送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. 我可以卷曲(-i)url http://localhost:3000/api/v1/log/newest并获取我期望的数据,并且它是application / json,如预期的那样。 If I load that data in my web browser, I get JSON as expected per dev tools inspector. 如果我在我的Web浏览器中加载该数据,我会按照预期的工具检查器获得JSON。

But using AFNetworking, I get this mysterious "unacceptable content-type: text/plain" error. 但是使用AFNetworking,我得到了这个神秘的“不可接受的内容类型:text / plain”错误。 Any idea? 任何想法?

NOTE: I've never used AFNetworking before, so I'm probably using it incorrectly. 注意:我之前从未使用AFNetworking,所以我可能错误地使用它。

It seems that the server is sending "text/html", and this type is not supported by default. 似乎服务器正在发送“text / html”,默认情况下不支持此类型。 Add @"text/html" for "acceptableContentTypes" 为“acceptableContentTypes”添加@“text / html”

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

为我工作。

为我工作。

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];

Just use: 只需使用:

SWIFT: 迅速:

manager.responseSerializer.acceptableContentTypes = NSSet(object: "text/plain") as Set<NSObject>

OBJ-C: OBJ-C:

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];

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;
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 为我工作

Updated Solution for Swift. 更新了Swift的解决方案。

If you are using AFNetworking in Swift. 如果你在Swift中使用AFNetworking。 Then this solution might help you. 那么这个解决方案可能对你有帮 It will accept most of the content types. 它将接受大多数内容类型。

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 请求失败:不可接受的内容类型:使用AFNetworking 2.0的文本/纯文本 - Request failed: unacceptable content-type: text/plain using AFNetworking 2.0 AFNetworking 请求失败:不可接受的内容类型:文本/纯文本, - AFNetworking Request failed: unacceptable content-type: text/plain, 使用AFNetworking 2.0的“请求失败:不可接受的内容类型:text / html” - “Request failed: unacceptable content-type: text/html” using AFNetworking 2.0 请求失败:不可接受的内容类型:使用AFNetworking 2.0的text / html - Request failed: unacceptable content-type: text/html using AFNetworking 2.0 AFNetworking请求失败:不可接受的内容类型:text / html - AFNetworking Request failed: unacceptable content-type: text/html 具有文本/纯文本响应的API失败,显示“内容类型不可接受” - API with text/plain response failed with 'unacceptable content-type' 请求失败:不可接受的内容类型:文本/纯文字-AFNetwork POST - Request failed: unacceptable content-type: text/plain - AFNetwork POST 使用AFNetworking 2.0时,“不可接受的内容类型:application / rss + xml” - “unacceptable content-type: application/rss+xml” when using AFNetworking 2.0 请求失败:不可接受的内容类型:使用AFNetworking 2.5的图像/ jpg - Request failed: unacceptable content-type: image/jpg using AFNetworking 2.5 使用AFNetworking解析Content-Type text / html - Parsing Content-Type text/html with AFNetworking
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM