AFNetworking:失败时如何获取响应字符串?
AFNetworking: how to get the response string when failed?
我正在使用
- (AFHTTPRequestOperation *)GET:(NSString *)URLString
parameters:(id)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
与服务器通信,效果很好。
但有时服务器会出错,它的响应不是json格式。我的搭档要我显示错误字符串,但由于它进入了失败块,我很难得到原始响应字符串。
有人可以帮忙吗?
在失败块中,添加这些代码:
NSDictionary *userInfo = [error userInfo];
NSString *message = [userInfo objectForKey:@"message"];
我觉得message正是你所需要的,你可以使用NSLog来显示信息。
要获取错误代码(响应代码),您可以使用 [operation.response statusCode];
错误详情应该在这里
id responseObject = error.userInfo[kErrorResponseObjectKey];
参考自:http://www.splinter.com.au/2014/09/10/afnetworking-error-bodies/
您可以在成功和失败块中记录 operation.responseString
行以获取服务器的响应字符串。
如果网络服务出现某种错误并提供无效 json 或响应,则在失败块中记录 [error description]
。
作为参考,请检查下面的代码片段。
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setTimeoutInterval:120];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
[manager POST:[[NSString stringWithFormat:@"%@",URL]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"Response: %@", operation.responseString);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"Error: %@ \n %@", operation.responseString,[error description]);
}];
我正在使用
- (AFHTTPRequestOperation *)GET:(NSString *)URLString
parameters:(id)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
与服务器通信,效果很好。
但有时服务器会出错,它的响应不是json格式。我的搭档要我显示错误字符串,但由于它进入了失败块,我很难得到原始响应字符串。
有人可以帮忙吗?
在失败块中,添加这些代码:
NSDictionary *userInfo = [error userInfo];
NSString *message = [userInfo objectForKey:@"message"];
我觉得message正是你所需要的,你可以使用NSLog来显示信息。
要获取错误代码(响应代码),您可以使用 [operation.response statusCode];
错误详情应该在这里
id responseObject = error.userInfo[kErrorResponseObjectKey];
参考自:http://www.splinter.com.au/2014/09/10/afnetworking-error-bodies/
您可以在成功和失败块中记录 operation.responseString
行以获取服务器的响应字符串。
如果网络服务出现某种错误并提供无效 json 或响应,则在失败块中记录 [error description]
。
作为参考,请检查下面的代码片段。
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setTimeoutInterval:120];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
[manager POST:[[NSString stringWithFormat:@"%@",URL]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"Response: %@", operation.responseString);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"Error: %@ \n %@", operation.responseString,[error description]);
}];