【问题标题】:AFNetworking code migration from 1.3 to 2.0AFNetworking 代码从 1.3 迁移到 2.0
【发布时间】:2015-07-01 16:44:35
【问题描述】:

我是 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;
             }
         }
     ];

【问题讨论】:

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


    【解决方案1】:

    粗略浏览一下代码,您似乎对块语法稍有误解。

    success 块的顶部,您有if (success) { 代码。
    您的代码中从未有过 success 变量的声明。传递到该成功块的唯一变量是 request, response, JSON

    failure 块中也发生了同样的事情。

    如果你把这些整理出来,你应该会看到一些进展。

    【讨论】:

    • 以上目前正在使用 afnetworking 1.3 版本的代码。那些成功和失败的变量被定义为kroll回调。上面代码中没有。
    • 我找不到使用 AFHTTPRequestOperation 编写相同代码的方法,因为 AFJSONRequestOperation 在 afnetworking 2.0 中不可用。AFJSONRequestOperation 仅允许操作和 id 作为参数。我需要响应读取状态代码和标头。 @MultiColourPixel
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    • 2014-01-14
    • 2015-05-08
    • 2014-07-31
    • 1970-01-01
    • 2015-10-31
    相关资源
    最近更新 更多