【问题标题】:AFNetworking (iOS) - response is not NSDictionaryAFNetworking (iOS) - 响应不是 NSDictionary
【发布时间】:2012-09-05 21:41:37
【问题描述】:

我知道我的请求格式正确,但我从 Web 服务获得的响应不是 NSDictionary。

  1. 为什么不是 NSDictionary?
  2. 如何判断“responseObject”是什么类型的对象?

    AFHTTPRequestOperation *operation = [[AFParseAPIClient sharedClient]  HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            NSLog(@"Response for %@: %@", className, responseObject);
            [self writeJSONResponse:responseObject toDiskForClassWithName:className];
        }else{
            //NSLog(@"Response NOT NSDictionary: %@", [responseObject class]);
            NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
            NSLog(@"str: %@", str);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Request for class %@ failed with error: %@", className, error);
    }];
    

这是我得到的:

//Response NOT NSDictionary: <7b227265 73756c74 73223a5b 7b22636f ...>

编辑:这是我得到的:

str: {"results":[{"desc":"My description.","device_iPad":true,"device_iPhone":true,"createdAt":"2012-09-05T18:36:11.431Z","updatedAt":"2012-09-05T22:00:52.199Z"}]}

【问题讨论】:

  • [responseObject class] 会告诉你它是什么。会不会只是一个字符串?
  • 这回答了我的问题的第 2 部分:__NSCFData。但我应该得到一个 NSDictionary...
  • 可能服务返回错误。试试 NSString 方法- (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding 并打印你的字符串,看看它包含什么。
  • 不知道该怎么做。你能举个例子来处理上面的 responseObject 吗?
  • 我不完全确定这会奏效,只是抛出一些想法:p .. 试试NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]

标签: ios web-services afnetworking


【解决方案1】:

只需在创建操作后添加这行代码

operation.responseSerializer = [AFJSONResponseSerializer 序列化器];

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,但使用 AFHTTPClient 的子类执行请求。我得到的是 __NSCFData 而不是 __NSCFDictionary。我用这个解决了它:

    [self setDefaultHeader:@"Accept" value:@"application/json"];
    

    self 是我的 AFHTTPClient 子类。

    【讨论】:

      【解决方案3】:

      这对您有用吗?来自 AFNetworking github 项目

      NSURL *url = [NSURL URLWithString:@"https://gowalla.com/users/mattt.json"];
      NSURLRequest *request = [NSURLRequest requestWithURL:url];
      
      AFJSONRequestOperation *operation = [AFJSONRequestOperationJSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
      NSLog(@"Name: %@ %@", [JSON valueForKeyPath:@"first_name"], [JSON valueForKeyPath:@"last_name"]);
      } failure:nil];
      
      [operation start];
      

      试一试,很简单。 *如果它仍然是一个数组,请执行[[JSON objectAtIndex:0] objectForKey@"desc /*(or whatever)*/"]

      【讨论】:

      • 如何在“results”数组中获取字典?
      • [responseObject valueForKey:@"results"] , [responseObject valueForKeyPath:@"results"], [responseObject objectAtIndex:0] 都报错
      • 您是否使用了我的回答中的方法,您能否发布生成的 JSON 对象,您遇到了什么样的错误?
      • 我有 AFNetworking,但我明白了:使用未声明的标识符 AFJSONRequestOperationJSONRequestOperationWithRequest
      • @soleil 分开 AFJSONRequestOperationJSONRequestOperationWithRequestAFJSONRequestOperation JSONRequestOperationWithRequest
      【解决方案4】:

      使用 AFJSONRequestOperation 和 AFNetworking 返回 JSON 响应。使用 AFHTTPRequestOperation 返回 NSData,您可以将其转换(转换)为 NSDictionary 并使用以下命令打印:

      NSLog(@"Response: %@", [[NSString alloc] initWithData:(NSData *)json encoding:NSUTF8StringEncoding]);

      我的建议是只使用 AFJSONRequestOperation!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-12-08
        • 1970-01-01
        • 1970-01-01
        • 2015-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多