【问题标题】:Error with AFNetworking for JSON Parsing用于 JSON 解析的 AFNetworking 错误
【发布时间】:2012-05-19 19:36:13
【问题描述】:

我有以下 JSON 解析代码:

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"; NSLocalizedDescription = "预期的内容类型 {(\n \"text/json\",\n \"application/json\",\n \"text/javascript\"\n)},得到 text/html";

有人可以帮我吗?

【问题讨论】:

    标签: ios json afnetworking


    【解决方案1】:
    [AFJSONRequestOperation addAcceptableContentTypes:@"text/plain"]
    

    上述内容已从 AFNetworking 2.x 中弃用。相反,您可以在 AFHTTPRequestOperation 的实例上调用以下内容,如下所示

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

    manager 是您的 AFHTTPRequestOperation 实例。

    来源:https://github.com/AFNetworking/AFNetworking/issues/1381

    【讨论】:

    • 另外,请记住使用 ?dl=1 而不是 ?dl=0 以便使用 text/plain 内容类型来下载 json 文件。
    【解决方案2】:

    在我的错误日志中,它打印“got text/html”。所以只需添加

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

    有效。

    【讨论】:

      【解决方案3】:

      因为您提供的链接没有热链接文件。它链接到 HTML 页面以下载文件。尝试在浏览器中访问那里...

      试试这个链接:https://dl.dropbox.com/s/qz16qyi3julygl9/facebook.json?dl=1 虽然不保证它会起作用。许多公司不赞成以这种方式直接链接到文件。

      【讨论】:

      • 我是你告诉我的链接。但我又遇到了同样的错误。我认为这个错误可能与 NSLocalizedDescription = "Expected content type {(\n \"text/json\",\n \"application/json\",\n \"text/javascript\"\n)}, got text /清楚的”;谢谢。
      • 那你需要想办法直接链接到它。您可能无法使用 DropBox。但是,这是您问题的根源。您可能需要将其移至另一台服务器,或使用 API。
      • ahhhh 我发现 dropbox 返回 json 文件的内容类型 --> text/plain 但 AFJSONRequestOperator 支持 "text/json"、"application/json"、"text/javascript" 如何表示内容输入 text/json ?
      • yippy,我已经解决了这个问题。进入 AFJSONRequestOperation.m 并编辑方法 -> + (NSSet *)acceptableContentTypes;并将 mime type -> @"text/plain" 添加到 NSSet
      • 您可以设置单个操作的可接受的内容类型包括text/plain。或者,如果您使用的是 AFNetworking 1.0RC1,您可以使用 [AFJSONRequestOperation addAcceptableContentTypes:@"text/plain"]
      猜你喜欢
      • 2013-06-24
      • 1970-01-01
      • 1970-01-01
      • 2017-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多