AFNetworking代码从1.3迁移到2.0

时间:2015-07-01 16:44:36

标签: objective-c afnetworking afnetworking-2 titanium-modules

我是IOS开发的新手。我正试图用AFNetworking 2.0重写这段代码。如何使用响应来获取成功或失败回调的状态代码和标题?这是用于AFNetworking的钛模块。

               AFJSONRequestOperation * operation = 
       [AFJSONRequestOperation JSONRequestOperationWithRequest: request 
        success: ^ (NSURLRequest * request, NSHTTPURLResponse * response, id JSON) {
             if (success) {
                 id response_body = JSON;
                 id status_code = [NSNull null];
                 id response_headers = [NSNull null];
                 id reason = [NSNull null];

                 if (!response_body)
                     response_body = [NSNull null];

                 if (response) {
                     status_code = [NSNumber numberWithInteger: response.statusCode];
                     response_headers = response.allHeaderFields;
                 }

                 [self _fireEventToListener: @"complete"
                     withObject: NSDictionaryOfVariableBindings(response_body, status_code, response_headers, reason) 
                     listener: success thisObject: nil
                 ];
                 [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

             }
         }
         failure: ^ (NSURLRequest * request, NSHTTPURLResponse * response, NSError * error, id JSON) {
             if (failed) {
                 id response_body = JSON;
                 id status_code = [NSNull null];
                 id response_headers = [NSNull null];
                 id reason = [NSNull null];

                 if (!response_body)
                     response_body = [NSNull null];

                 if (response) {
                     status_code = [NSNumber numberWithInteger: response.statusCode];
                     response_headers = response.allHeaderFields;
                 }

                 if (error && error.localizedDescription) {
                     reason = error.localizedDescription;
                 }

                 [self _fireEventToListener: @"complete"
                     withObject: NSDictionaryOfVariableBindings(response_body, status_code, response_headers, reason) 
                     listener: failed thisObject: nil
                 ];
                 [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
             }
         }
     ];

1 个答案:

答案 0 :(得分:0)

从简短的一瞥代码开始,看起来你有点误解的是块语法。

success区块的顶部,您有if (success) {代码 您的代码中没有任何内容声明 success 变量。传递到该成功块的唯一变量是request, response, JSON

你在failure区块中发生了同样的事情。

如果你对它们进行排序,你应该会看到一些进展。