美文网首页
AFNetworking - AFURLResponseSeri

AFNetworking - AFURLResponseSeri

作者: ASkyWatcher | 来源:发表于2020-06-12 00:24 被阅读0次

    AFURLResponseSerialization 的作用是对请求返回的数据进行序列化

    它有一个序列化的基类AFHTTPResponseSerializer,有六个不同的序列化的类继承于它

    • AFJSONResponseSerializer JSON格式数据响应
    • AFXMLParserResponseSerializer iOS端XML数据解析响应
    • AFXMLDocumentResponseSerializerMAC OS端XML数据解析响应
    • AFPropertyListResponseSerializerplist格式数据解析响应
    • AFImageResponseSerializer图片数据解析响应
    • AFCompoundResponseSerializer复合式数据解析响应

    在序列化之前,有一个必要的工作,那就是校验响应是否有效,在基类AFHTTPResponseSerializer里面提供了一个核心方法,主要是是校验响应内容的类型和状态码

    - (BOOL)validateResponse:(NSHTTPURLResponse *)response
                        data:(NSData *)data
                       error:(NSError * __autoreleasing *)error
    {
        BOOL responseIsValid = YES;
        NSError *validationError = nil;
    
        if (response && [response isKindOfClass:[NSHTTPURLResponse class]]) {
            //如果返回的内容类型是不支持的类型,抛出错误
            if (self.acceptableContentTypes && ![self.acceptableContentTypes containsObject:[response MIMEType]] &&
                !([response MIMEType] == nil && [data length] == 0)) {
    
                if ([data length] > 0 && [response URL]) {
                    NSMutableDictionary *mutableUserInfo = [@{
                                                              NSLocalizedDescriptionKey: [NSString stringWithFormat:NSLocalizedStringFromTable(@"Request failed: unacceptable content-type: %@", @"AFNetworking", nil), [response MIMEType]],
                                                              NSURLErrorFailingURLErrorKey:[response URL],
                                                              AFNetworkingOperationFailingURLResponseErrorKey: response,
                                                            } mutableCopy];
                    if (data) {
                        mutableUserInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] = data;
                    }
    
                    validationError = AFErrorWithUnderlyingError([NSError errorWithDomain:AFURLResponseSerializationErrorDomain code:NSURLErrorCannotDecodeContentData userInfo:mutableUserInfo], validationError);
                }
    
                responseIsValid = NO;
            }
            //如果返回的状态码不是200~299,抛出错误
            if (self.acceptableStatusCodes && ![self.acceptableStatusCodes containsIndex:(NSUInteger)response.statusCode] && [response URL]) {
                NSMutableDictionary *mutableUserInfo = [@{
                                                   NSLocalizedDescriptionKey: [NSString stringWithFormat:NSLocalizedStringFromTable(@"Request failed: %@ (%ld)", @"AFNetworking", nil), [NSHTTPURLResponse localizedStringForStatusCode:response.statusCode], (long)response.statusCode],
                                                   NSURLErrorFailingURLErrorKey:[response URL],
                                                   AFNetworkingOperationFailingURLResponseErrorKey: response,
                                           } mutableCopy];
    
                if (data) {
                    mutableUserInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] = data;
                }
    
                validationError = AFErrorWithUnderlyingError([NSError errorWithDomain:AFURLResponseSerializationErrorDomain code:NSURLErrorBadServerResponse userInfo:mutableUserInfo], validationError);
    
                responseIsValid = NO;
            }
        }
    
        if (error && !responseIsValid) {
            *error = validationError;
        }
    
        return responseIsValid;
    }
    

    其他都是序列化操作,还有初始化方法,实现NSSecureCoding和NSCopying协议,这些都是大同小异,值得一提的是一个图片加载的分类,主要作用是对图片的解压,这里可以仔细看下bang's blog,里面做了比较详细解释

    相关文章

      网友评论

          本文标题:AFNetworking - AFURLResponseSeri

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