AFNetworking 3.0.4更新使用

aric 8年前發布 | 29K 次閱讀 Objective-C開發

來自: http://my.oschina.net/u/2252300/blog/615951


最近更新了AFNetworking的版本,用了最新的3.0.4

發現較之前使用的2.5版本有較大的區別

在文件目錄結構上3.0.4去除了NSURLConnection這個文件夾和里面的

AFHTTPRequestOperation.h

AFHTTPRequestOperation.m

AFHTTPRequestOperationManager.h

AFHTTPRequestOperationManager.m

AFURLConnectionOperation.h

AFURLConnectionOperation.m

這6個文件


在之前的2.5版本中我們常使用的方法是:

    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    mgr.requestSerializer = [AFHTTPRequestSerializer serializer];
    mgr.responseSerializer = [AFJSONResponseSerializer serializer];
                        
    NSDictionary *params = @{ @"PPID":[NSString stringWithFormat:@"%ld", (long)ppId],
                              @"UserID":userId,
                            };
    
    [mgr POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary* jsonRsp = (NSDictionary *)responseObject;
        if (isSuccessRsp(jsonRsp))
        {
            success(jsonRsp);
        }
        else
        {
            failure(errorMsgRsp(jsonRsp));
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(error.description);
    }];

- (AFHTTPRequestOperation *)POST:(NSString *)URLString
                  headParameters:(id)headParameters
                  bodyParameters:(id)bodyParameters
                         success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                         failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
    [self setHeadParameters:headParameters];
    
    NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"POST" URLString:[[NSURL URLWithString:URLString] absoluteString] parameters:bodyParameters error:nil];
    AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
    
    [self.operationQueue addOperation:operation];
    
    return operation;
}

同時也不需要手動寫隊列


現3.0.4使用方法更加簡單

- (void)POST:(NSString *)URLString
           headParameters:(id)headParameters
           bodyParameters:(id)bodyParameters
                  success:(void (^)( id responseObject))success
                  failure:(void (^)( NSError *error))failure
{
    NSURL *url= [NSURL URLWithString:URLString];
    if (url == NULL)
    {
        return;
    }
    
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    
    if (headParameters)
    {
        [self setHeadParameters:headParameters sessionMgr:sessionManager];
    }

    [sessionManager POST:[url absoluteString] parameters:bodyParameters progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
}

 本文由用戶 aric 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!