在AFNetworking 2.x中替换AFJSONRequestOperation

Jam*_*unn 22 objective-c ios afnetworking afjsonrequestoperation afnetworking-2

按照本教程,我正在制作一个带有HTML请求的基本iPhone应用程序.

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

所以,当然,这个代码(从教程的大约一半,在" 查询iTunes Store搜索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];
Run Code Online (Sandbox Code Playgroud)

我的问题是,我该如何替换AFJSONRequestOperation以便我可以继续使用AFNetworking 2.x?我用谷歌搜索了这一点,发现似乎没有其他人在问这个问题.

aah*_*ens 30

你能用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
     }];
Run Code Online (Sandbox Code Playgroud)

另一种选择可能是使用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
}];
Run Code Online (Sandbox Code Playgroud)

  • 顺便说一句,默认的`responseSerializer`是`AFJSONResponseSerializer`,所以这一行是多余的. (4认同)

Aar*_*ger 7

来自NSHipster的关于AFNetworking 2的文章:

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

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

AFJSONRequestOperation等因此不再存在.

来自AFJSONResponseSerializer文档:

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];
Run Code Online (Sandbox Code Playgroud)