【问题标题】:Issue with retrieving JSON with AFNetworking使用 AFNetworking 检索 JSON 的问题
【发布时间】:2012-02-06 10:51:10
【问题描述】:

我正在尝试使用 AFNetworking 检索一些 JSON 数据。

我的服务器组件 (PHP/ZendFramework) 提供以下响应:

>curl -i http://test.local:8080/public/appapi/testaction/format/json
HTTP/1.1 200 OK
Date: Mon, 06 Feb 2012 10:37:20 GMT
Server: Apache
X-Powered-By: PHP/5.3.8
Content-Length: 35
Content-Type: application/json
{"entries":"test","abcxxx":"test2"}

我尝试使用以下代码来检索此 json 数据:

NSString *baseurl = @"http://test.local:8080";

NSString *path = @"/public/appapi/testaction/format/json";

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:baseurl]];
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];
[client setAuthorizationHeaderWithUsername:@"myusername" password:@"mypassword"];

[client getPath:path parameters:nil success:^(__unused AFHTTPRequestOperation *operation, id JSON) {

    //NSLog(@"sjson: %@", [JSON valueForKeyPath:@"entries"]);
    NSLog(@"sjson: %@", JSON);        
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error Code: %i - %@",[error code], [error localizedDescription]);
}];

在模拟器中运行时,控制台显示以下输出:

2012-02-06 11:22:39.800 App[3642:15803] sjson: <6a736f6e 466c6963 6b724170 69287b22 6d657468 6f64223a 7b225f63 6f6e7465 6e74223a 22666c69 636b722e 74657374 2e656368 6f227d2c 2022666f 726d6174 223a7b22 5f636f6e 74656e74 223a226a 736f6e22 7d2c2022 6170695f 6b657922 3a7b225f 636f6e74 656e7422 3a223336 32306666 32343265 37323336 34633135 32373031 31353438 62393335 3066227d 2c202273 74617422 3a226f6b 227d29>

任何关于我做错的提示都将不胜感激:) 我尝试访问 Twitter API 并且效果很好......

【问题讨论】:

  • 你如何解决这个问题?有数据吗?

标签: json afnetworking


【解决方案1】:

这很令人困惑,我一直试图弄清楚如何更好地正确记录/设计,但您需要将请求的 Accept 标头设置为 application/json

注册AFJSONRequestOperation 表示“任何通过的请求,如果是 JSON 请求,我会查看并处理”。您暗示请求是 JSON 的方式是设置适当的 Accept 标头,或添加 .json 作为扩展名。现在,请求正在通过操作类列表,但它只捕获AFHTTPRequestOperation,它的响应对象类型为NSData(您在日志中看到的内容)。

很抱歉造成混乱!

【讨论】:

  • 谢谢马特!这真的很有帮助 - 现在它就像一个魅力!
  • 如果您无权访问您的服务器来更改响应标头,您可以尝试 Ben H 提出的解决方案进一步向下: id payload = [NSJSONSerialization JSONObjectWithData:JSON options:NSJSONWritingPrettyPrinted error:nil] ;它对我有用。
  • 请参考here你应该注册类并设置正确的接受头。
【解决方案2】:

为了让我的应用发送和接收 JSON,我必须添加以下 3 行:

 httpClient.parameterEncoding = AFJSONParameterEncoding;
 [httpClient setDefaultHeader:@"Accept" value:@"application/json"];
 [httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];

【讨论】:

  • 添加这一行 [httpClient setDefaultHeader:@"Accept" value:@"application/json"]; 是给我“请求资源的适当表示 --- 在此服务器上找不到”。还有其他的吗?
  • 听起来好像服务器没有提供 JSON。
【解决方案3】:

如果您像我一样使用自定义接受标头,AFNetworking 将无法识别您正在请求 json,就像 mattt 解释的那样。解决此问题的一种方法是将数据简单地转换为完成块中的 json 对象:

^(AFHTTPRequestOperation *operation, id JSON)
{
   id payload = [NSJSONSerialization JSONObjectWithData:JSON options:NSJSONWritingPrettyPrinted error:nil];

   NSLog(@"Fetched: %@", payload);
}

【讨论】:

  • 我无法更改服务器响应标头,因为我无权访问它。服务器以“text/html”而不是“application/json”响应。你的建议就像一个魅力。谢谢!!!
  • same here.. 但是我发现使用我的 rest 客户端的所有类都必须调用这个 json 反序列化代码有点令人沮丧。我希望我能把所有这些配置内容都集中到我的REST singleton 类
【解决方案4】:

我遇到了同样的问题,但是在我添加了 @Tylerc230 答案中的 Accept 标头后,请求开始失败,因为接收到的格式 "text/html" 不是预期的内容类型,所以我在中添加了 @"text/html" acceptableContentTypes AFJSONRequestOperation.m 的函数。

【讨论】:

    猜你喜欢
    • 2013-01-05
    • 1970-01-01
    • 2015-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 2021-07-21
    • 1970-01-01
    相关资源
    最近更新 更多