美文网首页ios
基于AFNetworking 3.0的快速封装

基于AFNetworking 3.0的快速封装

作者: Django_Monstar | 来源:发表于2016-05-17 11:10 被阅读1395次
    AFNetworking 3.0与2.x之前的版本有重大的改变就是废弃了基于NSURLConnection封装的AFHTTPRequestOperationManager, AFHTTPRequestOperation,AFURLConnectionOperation 三个类.

    如果你不小心升级了AFN到3.x,AFHTTPRequestOperationManager相关的方法及无法使用,取而代之的是使用AFHTTPSessionManager,至于为何这样做,简而言之就是NSURLSession 在iOS9+后支持HTTP /2协议,该协议数据传输速度更快更安全。代码如下:

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //AFN的网络请求由manager发起, manager的request和resopnse设置要根据服务器的接受和返回的数据进行设置;
    /*
      1.manager默认提交请求的数据是二进制的,返回格式是JSON 如果提交数据是JSON的,需要将请求格式设置为AFJSONRequestSerializer
    如: manager.responseSerializer = [AFJSONResponseSerializer serializer];
     2.一般的请求格式
    AFHTTPRequestSerializer 二进制格式 
    AFJSONRequestSerializer JSON
    3.返回格式
    AFHTTPResponseSerializer 二进制格式 
    AFJSONResponseSerializer JSON
    返回格式的acceptableContentTypes根据实际修改
    */
    
        
    
    

    GET

     AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
        manager.responseSerializer = [AFJSONResponseSerializer serializer];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
        manager.requestSerializer=[AFJSONRequestSerializer serializer];
        [manager GET:getURL parameters:requestDictionary progress:nil success:^(NSURLSessionDataTask *  task, id   responseObject) {
          
        } failure:^(NSURLSessionDataTask *  task, NSError *  error) {
          
        }];
    

    POST

        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer = [AFJSONResponseSerializer serializer];//申明返回的结果是json类型
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];//如果报接受类型不一致请替换一致text/html或别的
        manager.requestSerializer=[AFJSONRequestSerializer serializer];//申明请求的数据是json类型
        [manager POST:URL parameters:requestDictionary progress:nil success:^(NSURLSessionDataTask *  task, id   responseObject) {
    
        } failure:^(NSURLSessionDataTask *  task, NSError *  error) {
            
        }];
    

    Upload

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer = [AFJSONResponseSerializer serializer];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
        manager.requestSerializer=[AFJSONRequestSerializer serializer];
        [manager POST:URL parameters:requestDictionary constructingBodyWithBlock:^(id<AFMultipartFormData>formData) {
          [formData appendPartWithFileData:imageData name:@"file" fileName:@"img.png" mimeType:@"image/jpeg"];
        } progress:nil success:^(NSURLSessionDataTask *  task, id   responseObject) {
          
        } failure:^(NSURLSessionDataTask * task, NSError *  error) {
            
        }];
    

    相关文章

      网友评论

        本文标题:基于AFNetworking 3.0的快速封装

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