【问题标题】:Replacement for AFJSONRequestOperation in AFNetworking 2.x在 AFNetworking 2.x 中替换 AFJSONRequestOperation
【发布时间】:2014-02-13 04:11:56
【问题描述】:

我正在制作一个带有 HTML 请求的基本 iPhone 应用程序,遵循 this tutorial.

本教程让我在 AFNetworking 中使用 AFJSONRequestOperation。问题是,我使用的是 AFNetworking 版本 2,它不再具有 AFJSONRequestOperation。

所以,当然,这段代码(大约在教程的一半,在标题“查询 iTunes Store Search API”下)无法编译:

NSURL *url = [[NSURL alloc]
    initWithString:
    @"http://itunes.apple.com/search?term=harry&country=us&entity=movie"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
AFJSONRequestOperation *operation =
    [AFJSONRequestOperation JSONRequestOperationWithRequest:request
    success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        NSLog(@"%@", JSON);
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response,
        NSError *error, id JSON) {
            NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
    }];
[operation start];

我的问题是,我应该用什么替换 AFJSONRequestOperation 才能继续使用 AFNetworking 2.x?我用谷歌搜索了一下,发现似乎没有其他人在问这个问题。

【问题讨论】:

    标签: ios objective-c afnetworking afnetworking-2 afjsonrequestoperation


    【解决方案1】:

    你能用 AFHTTPSessionManger 吗?所以像

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager GET:[url absoluteString]
      parameters:nil
         success:^(NSURLSessionDataTask *task, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
         }
         failure:^(NSURLSessionDataTask *task, NSError *error) {
            // Handle failure
         }];
    

    另一种选择是使用AFHTTPRequestOperation 并再次将responseSerializer 设置为[AFJSONResponseSerializer serializer]。所以像

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] 
                                                initWithRequest:request];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation
                                                            , id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // Handle error
    }];
    

    【讨论】:

    • 顺便说一句,默认responseSerializerAFJSONResponseSerializer,所以这一行是多余的。
    • @aahrens: 更喜欢AFHTTPSessionManager 还是AFHTTPRequestOperation
    • @AaronBrager 在 2.6.1 版(2015 年末)上似乎并非如此
    • @MaxMacLeod 是会话管理器和请求操作管理器的默认值:github.com/AFNetworking/AFNetworking/blob/…
    • @MaxMacLeod 如果您手动创建请求操作,而不是使用会话/操作管理器,则该默认值不适用。 IE,这个答案中的第一个代码示例不需要设置它,但是第二个需要。
    【解决方案2】:

    来自NSHipster's article on AFNetworking 2

    AFNetworking 2.0 新架构的突破之一是使用序列化程序来创建请求和解析响应。序列化程序的灵活设计允许将更多业务逻辑转移到网络层,并且可以轻松自定义以前内置的默认行为。

    在 AFNetworking 2 中,序列化程序(将 HTTP 数据转换为可用的 Objective C 对象的对象)现在是与请求操作对象分开的对象。

    AFJSONRequestOperation 等因此不再存在。

    来自the AFJSONResponseSerializer docs

    AFJSONResponseSerializerAFHTTPResponseSerializer 的子类,用于验证和解码 JSON 响应。

    有几种方法可以访问您提到的 API。这是一个:

    NSURL *url = [[NSURL alloc] initWithString:@"http://itunes.apple.com/search?term=harry&country=us&entity=movie"];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"success: %@", operation.responseString);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error: %@",  operation.responseString);
    }];
    
    [operation start];
    

    【讨论】:

    • 感谢您的链接、解释和示例!
    猜你喜欢
    • 1970-01-01
    • 2013-08-30
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    • 1970-01-01
    • 1970-01-01
    • 2012-04-07
    • 1970-01-01
    相关资源
    最近更新 更多