在故障块AFNetworking 3.0上获取responseObject [英] get responseObject on failure block AFNetworking 3.0

查看:170
本文介绍了在故障块AFNetworking 3.0上获取responseObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从AFNetworking 3.x的故障块中获取响应字符串,

how can I get the response string from failure block in AFNetworking 3.x,

在2.x版本中,实现方法是:

In the 2.x version the way to do it was:

[manager GET:path parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *dictionary_FetchResult = responseObject;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSDictionary *dictionary_FetchResult = operation.responseObject;
}];

但是在3.x版本中,返回块的参数没有任何操作,如下所示:

but in the 3.x version there is no operation in the returning block's parameter as shown below:

[manager POST:path parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    NSDictionary *dictionary_FetchResult = responseObject;
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    NSLog(@"Error: %@", error);
}];

所以我希望有人能做到这一点.

so I was hoping if someone was able to achieve that.

推荐答案

只需在您的故障块中执行此操作:-

Just do this in your failure block:-

 NSString* errResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];
NSLog(@"%@",errResponse);

对于Swift:-

var errResponse: String = String(data: (error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] as! NSData), encoding: NSUTF8StringEncoding)
NSLog("%@", errResponse)

已为Swift 4.1更新

var errResponse: String = String(data: (error._userInfo![AFNetworkingOperationFailingURLResponseDataErrorKey] as! Data), encoding: String.Encoding.utf8)!
print(errResponse)

这篇关于在故障块AFNetworking 3.0上获取responseObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆