iOS 使用AFNetworking遇到异常 Request failed: unacceptable content-type: text/html

Posted ⬆️小马哥⬆️

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS 使用AFNetworking遇到异常 Request failed: unacceptable content-type: text/html相关的知识,希望对你有一定的参考价值。

错误日志是:

Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7fc8b970eb70> { URL: http://www.aifish.top/register.php?userid=123&password=123 } { status code: 200, headers {

    Connection = "keep-alive";

    "Content-Encoding" = gzip;

    "Content-Type" = "text/html";

    Date = "Mon, 29 Aug 2016 03:13:37 GMT";

    Server = Tengine;

    "Transfer-Encoding" = Identity;

    Vary = "Accept-Encoding, Accept-Encoding";

} }, NSErrorFailingURLKey=http://www.aifish.top/register.php?userid=123&password=123, com.alamofire.serialization.response.error.data=<7b226661 696c5f6d 7367223a 7b227374 72696e67 223a2264 61746120 65786973 74227d2c 22756964 5f666169 6c223a7b 22737472 696e6722 3a223132 33227d7d>, NSLocalizedDescription=Request failed: unacceptable content-type: text/html}

通常我用

NSMutableSet *set = [NSMutableSet setWithSet:manager.responseSerializer.acceptableContentTypes];
        [set addObject:@"text/html"];

 就可以解决了, 不知道这次怎么不行,  只要自己打印下就知道到底有没有加上了.

NSLog(@"%@", manager.responseSerializer.acceptableContentTypes);

然后在AF的AFJSONResponseSerializer.m中,228行左右
把 这句: self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", nil];
修改为:
self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
打印一下contentTypes就知道加没加上了.??

以上是关于iOS 使用AFNetworking遇到异常 Request failed: unacceptable content-type: text/html的主要内容,如果未能解决你的问题,请参考以下文章

iOS开发过程中,遇到解析的json数据为<null>,进行赋值时会导致崩溃,使用AFNetworking可以这样解决。

ios AFNetworking---cookie(session)保持登陆会话状态

iOS 8 扩展的 AFNetworking 后台会话配置

库未加载@rpath/AFNetworking iOS

连接 JSON AFnetworking 2.0

ios json 解析与 afnetworking