Error with AFNetworking for JSON Parsing

让人想犯罪 __ 提交于 2020-01-01 14:23:07

问题


I have the following code for JSON Parsing:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.dropbox.com/s/qz16qyi3julygl9/facebook.json"]];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

    NSLog(@"Request Success %@",[JSON class]);

} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
    NSLog(@"Request Failure Because %@",[error userInfo]);
}];

[operation start];

but I have Request Failure with the following error message:

NSErrorFailingURLKey = "https://www.dropbox.com/s/qz16qyi3julygl9/facebook.json"; NSLocalizedDescription = "Expected content type {(\n \"text/json\",\n \"application/json\",\n \"text/javascript\"\n)}, got text/html";

can somebody help me?


回答1:


In my errorlog it prints "got text/html". So just add

[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]]

It works.




回答2:


[AFJSONRequestOperation addAcceptableContentTypes:@"text/plain"]

The above is deprecated from AFNetworking 2.x. Instead you can call the following on the instance of the AFHTTPRequestOperation as follows

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

Where manager is your instance of AFHTTPRequestOperation.

Source: https://github.com/AFNetworking/AFNetworking/issues/1381




回答3:


Because the link you provide doesn't hotlink the file. It links to an HTML page to download the file. Try going there in a browser...

Try this link instead: https://dl.dropbox.com/s/qz16qyi3julygl9/facebook.json?dl=1 No guarantees it will work though. A lot of companies frown on directly linking to files in this way.



来源:https://stackoverflow.com/questions/10547405/error-with-afnetworking-for-json-parsing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!