美文网首页iOS
解析 NSHTTPURLResponse 中的信息 - iOS

解析 NSHTTPURLResponse 中的信息 - iOS

作者: survivorsfyh | 来源:发表于2020-06-08 19:15 被阅读0次

    项目中调用接口发起请求是最常见的一种操作之一,那么接口异常后如何从 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,种类被划分没有那么详细


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

    相关文章

      网友评论

        本文标题:解析 NSHTTPURLResponse 中的信息 - iOS

        本文链接:https://www.haomeiwen.com/subject/wqhbzhtx.html