【发布时间】: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