我執行AFHTTPClient作爲一個單獨的類,如文檔建議,並呼籲與在後JSON數據,並接收返回JSON數據:接收與AFHTTPClient錯誤響應呼籲
[[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」),我實際上想要在這裏讀取響應對象(這是我使用的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呢?
你應該在BMNetworkCalls中寫一個自定義方法對於每個API調用 – Felix