调用 AFNetworking 响应失败块而不是成功块

Posted

技术标签:

【中文标题】调用 AFNetworking 响应失败块而不是成功块【英文标题】:AFNetworking response failure block is called instead of success block 【发布时间】:2015-05-14 12:35:37 【问题描述】:

当我得到状态码 200 时,正在调用 AFNetworking 响应 failure 块。如何改为调用 success

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://128.199.94.58/test/bt/client_token.php" parameters:nil
     success:^(AFHTTPRequestOperation *operation, id responseObject) 
         self.clientToken = responseObject[@"customerID"];
         NSLog(@"Client Token received.");
     
     failure:^(AFHTTPRequestOperation *operation, NSError *error) 
         // Handle failure communicating with your server
         NSLog(@"Client Token request failed.%@",operation.responseString);
         NSLog(@"error code %ld",(long)[operation.response statusCode]);
     ];

【问题讨论】:

【参考方案1】:

查看error 的值。它会告诉你连接失败的原因。在这种情况下,“失败”与状态码无关。返回“404”仍然是“成功”。失败意味着您无法完成操作。

【讨论】:

感谢您指出... web 服务的内容类型错误为 text/html...。现在更改为 application/json【参考方案2】:

如下使用acceptableStatusCodes

 AFHTTPRequestOperationManager *manager =  [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.requestSerializer = [TimeoutAFJSONRequestSerializer serializer];
NSMutableIndexSet* codes = [[NSMutableIndexSet alloc] init];
[codes addIndex: 200];
manager.responseSerializer.acceptableStatusCodes = codes;

[manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
 


  failure:^(AFHTTPRequestOperation *operation, NSError *error)
 
 ];

【讨论】:

【参考方案3】:

我运行这段代码,它可以找到。

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:@"http://128.199.94.58/test/bt/client_token.php" parameters:nil
     success:^(AFHTTPRequestOperation *operation, id responseObject) 
         NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseObject
                                                              options:kNilOptions
                                                                error:nil];
         self.clientToken = json[@"customerID"];
         NSLog(@"Client Token received.");
     
     failure:^(AFHTTPRequestOperation *operation, NSError *error) 
         // Handle failure communicating with your server
         NSLog(@"Client Token request failed.%@",operation.responseString);
         NSLog(@"error code %ld",(long)[operation.response statusCode]);
     ];

回复是:

json:

    customerID = "eyJ2ZXJzaW9uIjoyLCJhdXRob3JpemF0aW9uRmluZ2VycHJpbnQiOiJhMjg2OGVjY2FmZjNjMTQ0M2Y4MTg2MjQ4NDFhZDIyZGM3MWFhOTQ0MmFiMTY2NWVlNWY1YjJkODdiOTVhYzBjfGNyZWF0ZWRfYXQ9MjAxNS0wNS0xNFQxMzoyMDowNi45NjE2NDQxNzArMDAwMFx1MDAyNm1lcmNoYW50X2lkPXpxZDlkcGpmZmRzazd4bnlcdTAwMjZwdWJsaWNfa2V5PWRoeTdqeGt6Z3Y4d3dkcGoiLCJjb25maWdVcmwiOiJodHRwczovL2FwaS5zYW5kYm94LmJyYWludHJlZWdhdGV3YXkuY29tOjQ0My9tZXJjaGFudHMvenFkOWRwamZmZHNrN3hueS9jbGllbnRfYXBpL3YxL2NvbmZpZ3VyYXRpb24iLCJjaGFsbGVuZ2VzIjpbImN2diJdLCJlbnZpcm9ubWVudCI6InNhbmRib3giLCJjbGllbnRBcGlVcmwiOiJodHRwczovL2FwaS5zYW5kYm94LmJyYWludHJlZWdhdGV3YXkuY29tOjQ0My9tZXJjaGFudHMvenFkOWRwamZmZHNrN3hueS9jbGllbnRfYXBpIiwiYXNzZXRzVXJsIjoiaHR0cHM6Ly9hc3NldHMuYnJhaW50cmVlZ2F0ZXdheS5jb20iLCJhdXRoVXJsIjoiaHR0cHM6Ly9hdXRoLnZlbm1vLnNhbmRib3guYnJhaW50cmVlZ2F0ZXdheS5jb20iLCJhbmFseXRpY3MiOnsidXJsIjoiaHR0cHM6Ly9jbGllbnQtYW5hbHl0aWNzLnNhbmRib3guYnJhaW50cmVlZ2F0ZXdheS5jb20ifSwidGhyZWVEU2VjdXJlRW5hYmxlZCI6ZmFsc2UsInBheXBhbEVuYWJsZWQiOnRydWUsInBheXBhbCI6eyJkaXNwbGF5TmFtZSI6InVzYyIsImNsaWVudElkIjpudWxsLCJwcml2YWN5VXJsIjoiaHR0cDovL2V4YW1wbGUuY29tL3BwIiwidXNlckFncmVlbWVudFVybCI6Imh0dHA6Ly9leGFtcGxlLmNvbS90b3MiLCJiYXNlVXJsIjoiaHR0cHM6Ly9hc3NldHMuYnJhaW50cmVlZ2F0ZXdheS5jb20iLCJhc3NldHNVcmwiOiJodHRwczovL2NoZWNrb3V0LnBheXBhbC5jb20iLCJkaXJlY3RCYXNlVXJsIjpudWxsLCJhbGxvd0h0dHAiOnRydWUsImVudmlyb25tZW50Tm9OZXR3b3JrIjp0cnVlLCJlbnZpcm9ubWVudCI6Im9mZmxpbmUiLCJ1bnZldHRlZE1lcmNoYW50IjpmYWxzZSwiYnJhaW50cmVlQ2xpZW50SWQiOiJtYXN0ZXJjbGllbnQzIiwibWVyY2hhbnRBY2NvdW50SWQiOiI2ejl3eGtkanlyNnQzbmg1IiwiY3VycmVuY3lJc29Db2RlIjoiVVNEIn0sImNvaW5iYXNlRW5hYmxlZCI6ZmFsc2UsIm1lcmNoYW50SWQiOiJ6cWQ5ZHBqZmZkc2s3eG55IiwidmVubW8iOiJvZmYifQ==";

它可能对你有用。

【讨论】:

这导致应用程序崩溃...感谢您的尝试【参考方案4】:

如果您检查失败块中的错误,它会清楚地显示invalid content type。需要按如下方式设置管理器的内容类型

manager.requestSerializer = [AFJSONRequestSerializer serializer];

【讨论】:

是的,但是这个解决方案使我的应用程序崩溃,所以在服务器端进行了更改...谢谢【参考方案5】:

试试这个

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

并在成功块中

 success:^(AFHTTPRequestOperation *operation, id responseObject) 
     NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:manager.responseData options:kNilOptions error:nil];
     self.clientToken = dic[@"customerID"];
     NSLog(@"Client Token received.");
 

【讨论】:

你能告诉我你的答案和 Inder Kumar Rathore 的答案有什么区别吗?? 他说 AFJSONRequestSerializer,我建议使用 AFHTTPResponseSerializer。更多编辑以获得结果

以上是关于调用 AFNetworking 响应失败块而不是成功块的主要内容,如果未能解决你的问题,请参考以下文章

AFnetworking 不断返回响应 Code=-1011 “请求失败:客户端错误 (422)

在 AFNetworking 2 的失败块中访问 JSON 响应

以 JSON 字符串数组形式获取 AFNetworking 响应的结果

使用 iOS 中的 AFNetworking,确定失败响应是服务器错误还是连接错误的最佳方法是啥?

为啥这个 API 在 AFNetworking 中将其响应作为“失败”返回,而它正是它应该是的并且状态码是 200?

AFNetworking (iOS) - 响应不是 NSDictionary