ios - JSON 解析在 IOS 中获取错误 NSErrorFailingURLKey

标签 ios objective-c iphone json afnetworking-3

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

NSErrorFailingURLKey= http://wptrafficanalyzer.in/p/demo1/first.php/countries/ , NSLocalizedDescription=请求失败: Not Acceptable 内容类型: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 内容类型是 Application/JSON,但在 IOS 中我得到 header 内容类型 text/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];
}

最佳答案

添加这段代码:

 manager.responseSerializer = [AFHTTPResponseSerializer serializer];

在此之后:

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

这里有很好的解释:Upload an sqlite file

Your server page is returning text/html, but AFURLSessionManager is expecting JSON.

编辑:

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

关于ios - JSON 解析在 IOS 中获取错误 NSErrorFailingURLKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34806053/

相关文章:

iOS 自定义 View 或导航栏 Controller

ios - 在 iOS 中本地化字符串 : default (fallback) language?

objective-c - 在模型更改时自动更新 NSOutlineView,无需 NSTreeController

iphone - 如何从默认的 iOS 相机应用程序向我的应用程序发送照片?

iphone - 在屏幕外捕获 MKMapView 屏幕截图

ios - 用于视网膜显示的带有 UIImage 的 UIColor 模式

iphone - iOS 如何从类方法访问 self ?

iOS:如何在 Constants.h 中转换字符串中的类型?

objective-c - 如何在 Objective-C 中获取用户的语言环境?

iphone - iPhone 应用程序的演练/工具提示/演示