【问题标题】:receiving the error response with AFHTTPClient calls通过 AFHTTPClient 调用接收错误响应
【发布时间】:2012-06-07 20:49:15
【问题描述】:

按照文档中的建议,我将 AFHTTPClient 实现为单例类,并在帖子中使用 JSON 数据调用它,并接收 JSON 数据作为回报:

[[BMNetworkCalls sharedInstance] postPath:theURL parameters:theDict success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"my return is: %@", [responseObject valueForKeyPath:@"Result"]);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {            
    NSLog(@"error in network call: %@", [error localizedDescription]);
}];

一切都很好,但是如果我收到一个错误,(“HTTPRequestOperation 中的错误:(200-299) 中的预期状态代码,得到 400”),我实际上 想要 读取 responseObject这里也是(这是我使用的 API 告诉我我造成了哪类错误的方式)。

我可以使用 AFJSONRequestOperation 做到这一点:

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSLog(@"my return is: %@", [JSON valueForKeyPath:@"Result"]);

} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
    NSLog(@"exception code: %@", [JSON valueForKeyPath:@"ExceptionCode"]);
    NSLog(@"exception message: %@", [JSON valueForKeyPath:@"ExceptionMessage"]);
}];
[operation start];

我如何(并且我可以?)使用 AFHTTPClient 来做到这一点?

【问题讨论】:

  • 你应该为每个 API 调用在 BMNetworkCalls 中编写一个自定义方法

标签: json afnetworking


【解决方案1】:

operation 变量包含您需要的一切:

[[BMNetworkCalls sharedInstance] postPath:theURL parameters:theDict success:^(AFHTTPRequestOperation *operation, id responseObject) {
  // ...
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    if ([operation isKindOfClass:[AFJSONRequestOperation class]]) {
      id JSON = [(AFJSONRequestOperation *)operation responseJSON];
      NSLog(@"JSON: %@", JSON)
    }
}];

【讨论】:

  • 现在你告诉我..(笑)。请注意,即使没有必要,我在下面的回答也是有效的。
【解决方案2】:

所有功劳归于@phix23,他为我指明了正确的方向!

这是我在子类 AFHTTPClient 中编写的自定义方法,它允许我在收到 400 错误后查看 JSON 响应:

- (void) myPostPath:(NSString *)path
        parameters:(NSDictionary *)parameters
           success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, id JSON))success 
           failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON))failure
{
    NSURLRequest *request = [self requestWithMethod:@"POST" path:path parameters:parameters];   
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:success failure:failure];
    [self enqueueHTTPRequestOperation:operation];
}

我叫它:

[[BMNetworkCalls sharedInstance] myPostPath:theURL parameters:theDict success:^(NSURLRequest *request, NSHTTPURLResponse *response, id responseObject) {
    NSLog(@"my return is: %@", [responseObject valueForKeyPath:@"Result"]);

} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {          
    NSLog(@"exception code: %@", [JSON valueForKeyPath:@"ExceptionCode"]);
}];

【讨论】:

  • 这是不必要的。您可以访问通常在失败回调块中传递的操作变量的 responseJSON 属性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-07
  • 1970-01-01
  • 2021-06-15
  • 1970-01-01
  • 2016-11-20
相关资源
最近更新 更多