Error with AFNetworking for JSON Parsing

2020-07-23 06:11发布

问题:

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.