JSON解析在IOS中得到错误NSErrorFailingURLKey

Posted

技术标签:

【中文标题】JSON解析在IOS中得到错误NSErrorFailingURLKey【英文标题】:JSON Parsing getting Error NSErrorFailingURLKey in IOS 【发布时间】:2016-01-15 07:18:35 【问题描述】:

我正在尝试在第一个日志中打印 JSON 响应。但是每当我尝试从这个 URL 和这个 URL 获得响应时。但是每当我提出请求时,我都会收到这个错误。

NSErrorFailingURLKey=http://wptrafficanalyzer.in/p/demo1/first.php/countries/, NSLocalizedDescription=请求失败:不可接受的内容类型:text/html, com.alamofire.serialization.response.error.data

这是我的日志文件

  2016-01-15 12:19:25.618 JSOnParsingWithActorList[3153:58905] Error: Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x78e25230>  URL: http://wptrafficanalyzer.in/p/demo1/first.php/countries/   status code: 200, headers 
    "Cache-Control" = "max-age=3600";
    Connection = "Keep-Alive";
    "Content-Encoding" = gzip;
    "Content-Length" = 490;
    "Content-Type" = "text/html";
    Date = "Fri, 15 Jan 2016 06:27:55 GMT";
    Expires = "Fri, 15 Jan 2016 07:27:55 GMT";
    "Keep-Alive" = "timeout=5, max=100";
    Server = "Apache/2.2.22 (Ubuntu)";
    Vary = "Accept-Encoding";
    "X-Powered-By" = "PHP/5.3.10-1ubuntu3.21";
 , NSErrorFailingURLKey=http://wptrafficanalyzer.in/p/demo1/first.php/countries/, NSLocalizedDescription=Request failed: unacceptable content-type: text/html, com.alamofire.serialization.response.error.data=<7b22636f 756e7472 69657322 3a5b7b22 636f756e 7472796e 616d6522 3a22496e 64696122 2c22666c 6167223a 22687474 703a5c2f 5c2f7770 74726166 66696361 6e616c79 7a65722e 696e5c2f 705c2f64 656d6f31 5c2f696e 6469612e 706e6722 2c226c61 6e677561 6765223a 2248696e 6469222c 22636170 6974616c 223a224e 65772044 656c6869 222c2263 75727265 6e637922 3a7b2263 6f646522 3a22494e 52222c22 63757272 656e6379 6e616d65 223a2252 75706565 227d7d2c 7b22636f 756e7472 796e616d 65223a22 50616b69 7374616e 222c2266 6c616722 3a226874 74703a5c 2f5c2f77 70747261 66666963 616e616c 797a6572 2e696e5c 2f705c2f 64656d6f 315c2f70 616b6973 74616e2e 706e6722 2c226c61 6e677561 6765223a 22557264 75222c22 63617069 74616c22 3a224973 6c616d61 62616422 2c226375 7272656e 6379223a 7b22636f 6465223a 22504b52 222c2263 75727265 6e63796e 616d6522 3a225061 6b697374 616e6920 52757065 65227d7d 2c7b2263 6f756e74 72796e61 6d65223a 22537269 204c616e 6b61222c 22666c61 67223a22 68747470 3a5c2f5c 2f777074 72616666 6963616e 616c797a 65722e69 6e5c2f70 5c2f6465 6d6f315c 2f737269 6c616e6b 612e706e 67222c22 6c616e67 75616765 223a2253 696e6861 6c61222c 22636170 6974616c 223a2253 7269204a 61796177 61726465 6e617075 7261204b 6f747465 222c2263 75727265 6e637922 3a7b2263 6f646522 3a22534b 52222c22 63757272 656e6379 6e616d65 223a2253 7269204c 616e6b61 6e205275 70656522 7d7d2c7b 22636f75 6e747279 6e616d65 223a2243 68696e61 222c2266 6c616722 3a226874 74703a5c 2f5c2f77 70747261 66666963 616e616c

我正在使用AFNetworking 进行 URL 调用。我在 android 中检查此 url。此 URL 内容类型是应用程序/JSON,但在 ios 中,我正在获取标题内容类型文本/html。从此URL 我收到来自 JSON 格式 URL 的成功响应。请帮助解决此问题。

这是我的 ViewController.m 文件

 - (void)viewDidLoad 
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

    NSURL *URL = [NSURL URLWithString:@"http://wptrafficanalyzer.in/p/demo1/first.php/countries/"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];

    NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) 
        if (error) 
            NSLog(@"Error: %@", error);
         else 
            NSLog(@"%@ %@",response);
        
    ];
    [dataTask resume];

【问题讨论】:

【参考方案1】:

添加此代码:

 manager.responseSerializer = [AFHTTPResponseSerializer serializer];

之后:

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

这里解释得很好:Upload an sqlite file

您的服务器页面正在返回 text/html,但 AFURLSessionManager 需要 JSON。

编辑:

由于上述解决方案没有成功,我发现此链接包含此错误的所有可能解决方案:Request failed: unacceptable content-type: text/html

【讨论】:

我只是在我的项目中只写了上面提到的代码而已。 还有这个:manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"]; 还有这个:manager.responseSerializer.acceptableContentTypes = nil; 检查我编辑的链接答案。在那里,您将找到解决此问题的多种可能解决方案。

以上是关于JSON解析在IOS中得到错误NSErrorFailingURLKey的主要内容,如果未能解决你的问题,请参考以下文章

在 iOS 中解析 JSON 时遇到一些错误?

在 iOS 错误中解析 JSON 图像 url

iOS:在 PHP 服务文件中使用 return 而不是 echo 时出现 JSON 解析错误

无法在 iOS 中解析推送通知 json?

Json 解析错误:在 Swift iOS 中使用未声明的类型“Foundation”

在iOS中解析JSON时是不是可以检索描述以及状态码