用于JSONparsing的AFNetworking错误
我有以下JSONparsing代码:
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];
但我有请求失败,并显示以下错误消息:
NSErrorFailingURLKey =“ https://www.dropbox.com/s/qz16qyi3julygl9/facebook.json ”; {\ n \“text / json \”,\ n \“application / json \”,\ n \“text / javascript \”\ n)},得到text / html“;
有人可以帮我吗?
在我的错误日志中,它打印“ 获得文本/ HTML ”。 所以只需添加
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]]
有用。
[AFJSONRequestOperation addAcceptableContentTypes:@"text/plain"]
以上内容已从AFNetworking 2.x中弃用。 相反,您可以按如下方式在AFHTTPRequestOperation的实例上调用以下内容
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
经理是AFHTTPRequestOperation的实例。
来源: https : //github.com/AFNetworking/AFNetworking/issues/1381
由于您提供的链接不能热链接文件。 它链接到一个HTML页面来下载文件。 尝试在浏览器中去那里…
试试这个链接,而不是: https : //dl.dropbox.com/s/qz16qyi3julygl9/facebook.json?dl = 1不保证它会工作。 很多公司都以这种方式直接链接到文件。