简体   繁体   English

请求失败:不可接受的内容类型:使用AFNetworking 2.0的文本/纯文本

[英]Request failed: unacceptable content-type: text/plain using AFNetworking 2.0

I've a trouble with requests using AFNetworiking 2. 我对使用AFNetworiking 2的请求感到麻烦。

I have my AFHTTPSessionManager custom sharedInstance in my sharedInstance class method 我的sharedInstance类方法中有我的AFHTTPSessionManager自定义sharedInstance

sharedInstance.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:3];
sharedInstance.responseSerializer.acceptableContentTypes = [sharedInstance.responseSerializer.acceptableContentTypes setByAddingObject:@"text/plain"];
sharedInstance.responseSerializer.acceptableContentTypes = [sharedInstance.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

This is because my server responds with text/plain (only on errors - on success it responses with jsons) 这是因为我的服务器以文本/纯文本响应(仅在错误时显示-成功时,它使用jsons响应)

But even that when I send a POST method, I get this error: 但是即使这样,当我发送POST方法时,也会收到此错误:

failure: 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=0x175e6970 {NSDebugDescription=JSON text did not start with array or object
and option to allow fragments not set., NSUnderlyingError=0x175e59b0 "Request failed:
internal server error (500)"}

I've read questions: this and this and some more stuff in the web but there is no good answer. 我读过的问题: 这个这个和一些更多的东西在网络,但没有很好的答案。 Any ideas? 有任何想法吗?

Try something like this: 尝试这样的事情:

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:url]];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.requestSerializer = [AFJSONRequestSerializer serializer];

使用迅捷尝试一下

manager.responseSerializer.acceptableContentTypes = nil

Internal server error (500) basically means your server is not able to successfully handle this request, but goes in error. 内部服务器错误(500)基本上意味着您的服务器无法成功处理此请求,但出现错误。 (You may want to expose here portion of server code to get more help). (您可能希望在此处公开服务器代码的一部分以获得更多帮助)。 From your question, I understand that in case of error, the server answers in text/plain. 从您的问题中得知,如果发生错误,服务器将以文本/纯文本形式回答。 I suppose that your code raises this error (JSON text did not start with array...) because you are trying to parse the response as JSON (expected when no error occurs) even in the case of a 500 error. 我想您的代码会引发此错误(JSON文本不是以数组开头...),因为即使在发生500错误的情况下,您仍试图将响应解析为JSON(如果没有错误发生,则是预期的)。

You may want to share the rest of your client code. 您可能要共享其余的客户代码。 Correcting the server side issue would help, but would not lead to a proper client code logic for the error cases. 纠正服务器端问题将有所帮助,但不会为错误情况导致正确的客户端代码逻辑。

If you need to access Http error code, have a look here : is there an easy way to get the http status code in the failure block from AFHTTPClient? 如果您需要访问Http错误代码,请在此处查看: 是否有一种简便的方法可以从AFHTTPClient的故障块中获取http状态代码?

Possible similar to your issue here : AFNetworking 2.0: NSLocalizedDescription=Request failed: unacceptable content-type: text/html 可能与您的问题类似: AFNetworking 2.0:NSLocalizedDescription =请求失败:不可接受的内容类型:text / html

For OC, I solved the problem by this way 对于OC,我通过这种方式解决了问题

AFHTTPSessionManager* manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = nil;

Hope this will help. 希望这会有所帮助。

Just went through something similar yesterday! 昨天经历了类似的事情!

Here's what I did. 这就是我所做的。

Go to your afnetworking class, then AFURLResponseSerializer.m . 转到您的afnetworking类,然后转到AFURLResponseSerializer.m。

Find this method, then make sure it has NSJSONReadingAllowFragments. 找到此方法,然后确保它具有NSJSONReadingAllowFragments。

+ (instancetype)serializer {
    return [self serializerWithReadingOptions:(NSJSONReadingOptions)NSJSONReadingAllowFragments];
}

Try running it again, you may get another error, Invalid value around character 0. 尝试再次运行它,您可能会收到另一个错误,字符0周围的值无效。

So yeah, your server is bull, it's giving garbage json. 是的,您的服务器很牛,它正在提供垃圾json。

You can check it though! 您可以检查它!

So in your failure possibility put: 因此,在发生故障的可能性中:

NSLog("%@", operation.responseString);

Most servers say in the reply whether it was a success or not, mine does anyway. 大多数服务器在答复中都说成功与否,无论如何都是我的。

So search the responseString for Success like below. 因此,如下搜索搜索成功的responseString。

(in failure:) (失败:)

 if ([operation.responseString containsString:@"SUCCESS"]) {                 
//true success
} else {              
NSLog(@"Error: %@", error);
//true failure
}

Surprisingly this question does not have a complete answer, while @blackbox is very close. 令人惊讶的是,这个问题没有完整的答案,而@blackbox非常接近。

First of all, as @blackbox said, as server responses with 500 Internal Server Error , probably displayed as text/html or text/plain by default, it's not accepted by AFNetworking 2 's AFJSONResponseSerializer ; 首先,正如@blackbox所说,服务器响应为500内部服务器错误 ,默认情况下可能显示为text/htmltext/plainAFNetworking 2AFJSONResponseSerializer不接受它; it accepts MIME type equals to JSON only (eg application/json ). 它接受仅等于JSON的MIME类型(例如application/json )。

Therefore, to tackle this, in failure block, you can detect such error and relevant display error message. 因此,要解决此问题,可以在failure块中检测到此类错误以及相关的显示错误消息。

暂无
暂无

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

相关问题 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 2.0 - “不可接受的内容类型:text / plain” - AFNetworking 2.0 - “unacceptable content-type: text/plain” AFNetworking请求失败:不可接受的内容类型:text / html - AFNetworking Request failed: unacceptable content-type: text/html 请求失败:不可接受的内容类型:文本/纯文字-AFNetwork POST - Request failed: unacceptable content-type: text/plain - AFNetwork POST 具有文本/纯文本响应的API失败,显示“内容类型不可接受” - API with text/plain response failed with 'unacceptable content-type' 请求失败:不可接受的内容类型:使用AFNetworking 2.5的图像/ jpg - Request failed: unacceptable content-type: image/jpg using AFNetworking 2.5 使用AFNetworking 2.0时,“不可接受的内容类型:application / rss + xml” - “unacceptable content-type: application/rss+xml” when using AFNetworking 2.0 Swift项目中的AFNetorking-“错误:请求失败:内容类型不可接受:text / html” - AFNetorking in Swift project — “Error: Request failed: unacceptable content-type: text/html”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM