首页 > 解决方案 > AFNetworking 迁移 - 替代 HTTPRequestOperationWithRequest

问题描述

我正在将一个旧项目迁移到目标 c 中的最新 AFNetworking pod。我不确定如何在最新的 AFNetworking 中管理添加操作,也没有找到任何相关文章。

+ (void)uploadXMLDocumentWithUrl:(NSString*)serviceURL
                     xmlFileData:(NSData *)xmlData
                withSuccessBlock:(void (^)(id response, NSDictionary* headers))successBlock
                  withErrorBlock:(void (^)(NSError* error, NSDictionary* headers, id responseObject))errorBlock
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:serviceURL]];
    [request setHTTPBody: xmlData];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"application/xml" forHTTPHeaderField:@"Accept"];
    
    
    
    NSOperation *operation = [manager HTTPRequestOperationWithRequest:request success:^(NSURLSessionTask *operation, id responseObject) {
        
        
        NSDictionary* headersCollection = [[(NSDictionary*)operation valueForKey:@"response"] valueForKey:@"allHeaderFields"];
        NSMutableDictionary* headers = [headersCollection mutableCopy];
        headers[@"statusCode"] = [NSNumber numberWithInteger:operation.response.statusCode];
        
        successBlock(responseObject, headers);
        
    }
                                                              failure:^(AFHTTPRequestOperation* operation, NSError* error) {
        
        NSDictionary* headersCollection = [[(NSDictionary*)operation valueForKey:@"response"] valueForKey:@"allHeaderFields"];
        NSMutableDictionary* headers = [headersCollection mutableCopy];
        headers[@"statusCode"] = [NSNumber numberWithInteger:operation.response.statusCode];
        
        errorBlock(error, headers, operation.responseObject);
        
    }];
    [manager.operationQueue addOperation:operation];
    
    
}

我知道AFHTTPRequestOperationManager已迁移到,AFHTTPSessionManager但我不了解如何迁移HTTPRequestOperationWithRequest:request。非常感谢任何帮助。谢谢

标签: iosobjective-cafnetworking

解决方案


推荐阅读