解析 NSHTTPURLResponse 中的数据 - iOS

项目中调用接口发起请求是最常见的一种操作之一,那么接口异常后如何从 failure 的 NSError 中提取所需信息,详见如下:
注:以下内容介绍基于 AFNetwork 展开,其它请求 api 暂未尝试,仅供参考

 

请求异常 NSError 中数据

如下 Status Code 以 502 为例,500、503、504 均已试验 

(lldb) po error
Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad gateway (502)" 
UserInfo={NSLocalizedDescription=Request failed: bad gateway (502), 
NSErrorFailingURLKey=http://xxxxxxx接口地址, 
com.alamofire.serialization.response.error.data={
length = 23, 
bytes = 0x7b22737461747573223a226e6f2074726164654e4f227d}, 
com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x283ecdee0> { 
URL: http://xxxxxxx接口地址 } 
{ Status Code: 502, Headers {
    "Access-Control-Allow-Credentials" =     (
        true
    );
    "Access-Control-Allow-Headers" =     (
        "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With"
    );
    "Access-Control-Allow-Methods" =     (
        "GET, POST, PUT, DELETE, OPTIONS"
    );
    "Access-Control-Allow-Origin" =     (
        "*"
    );
    "Access-Control-Max-Age" =     (
        3600
    );
    "Content-Type" =     (
        "application/json;charset=UTF-8"
    );
    Date =     (
        "Wed, 03 Jun 2020 06:52:10 GMT"
    );
    "Transfer-Encoding" =     (
        Identity
    );
} }}

 

解析 NSError 中数据

首先,根据控制台中 po 输出的异常信息和 AFURLResponseSerialization 中所提供的如下定义可以来获取不同的异常数据源

NSString * const AFURLResponseSerializationErrorDomain = @"com.alamofire.error.serialization.response";
NSString * const AFNetworkingOperationFailingURLResponseErrorKey = @"com.alamofire.serialization.response.error.response";
NSString * const AFNetworkingOperationFailingURLResponseDataErrorKey = @"com.alamofire.serialization.response.error.data";

其次,对照如上提供的规范可以针对 NSError 提取对应的数据信息,例如

NSLog(@"%@", error.userInfo[AFNetworkingOperationFailingURLResponseErrorKey]);

再其次,根据控制台中的数据信息格式类型的不同,对照响应的接收格式类型进行处理即可;

NSData *errorData = error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey];
NSHTTPURLResponse *response = error.userInfo[AFNetworkingOperationFailingURLResponseErrorKey];

最后,需要哪些参数字段对应取值,随后针对不同的异常状态进行具体业务场景处理即可。

NSData *errorData = error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey];
if (errorData) {
    NSError *errorObj = nil;
    NSDictionary *serializedData = [NSJSONSerialization JSONObjectWithData:errorData
                                                                   options:kNilOptions
                                                                     error:&errorObj];
    errorMsg = [NSString stringWithFormat:@"%@", [serializedData objectForKey:@"detail"]];
    if (errorObj) {
        NSLog(@"YH Pay [POST] Request error:\n%@", errorObj);
    }
        NSLog(@"YH Pay [POST] Request\nURL: %@\nError:\n%@", apiString, serializedData);
}

NSHTTPURLResponse *response = error.userInfo[AFNetworkingOperationFailingURLResponseErrorKey];
NSLog(@"%@", response.URL);
NSLog(@"%ld", (long)response.statusCode);
NSLog(@"%@", response.allHeaderFields);

 

NSError

// 错误状态吗 iOS-sdk里面的 NSURLError.h 文件
typedef NS_ENUM (NSUInteger, AFNetworkErrortype) {
    AFNetworkErrorType_TimedOut = NSURLErrorTimedOut,
    AFNetworkErrortype_UnURL = NSURLErrorUnsupportedURL,
    AFNetworkErrorType_NoNetwork = NSURLErrorNotConnectedToInternet,
    AFNetworkErrortype_404Failed = NSURLErrorBadServerResponse,
    
    AFNetworkErrorType_3840Failed = 3840,
};

若不单独做如上处理只监听 NSError 的 code,种类被划分没有那么详细


以上便是此次分享的全部内容,希望能对大家有所帮助!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值