Replacement for AFJSONRequestOperation in AFNetworking 2.x
Asked Answered
C

2

22

I am making a basic iPhone app with HTML Requests, by following this tutorial.

The tutorial has me using AFJSONRequestOperation in AFNetworking. The trouble is, I'm using AFNetworking version 2, which no longer has AFJSONRequestOperation.

So, of course, this code (from about half-way down the tutorial, under the heading "Querying the iTunes Store Search API") doesn't compile:

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];

My question is, what do I replace AFJSONRequestOperation with so that I can keep working with AFNetworking 2.x? I've googled this and found that no one else seems to be asking this question.

Christophany answered 22/1, 2014 at 21:18 Comment(0)
P
30

Could you use AFHTTPSessionManger? So something like

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

Another alternative could be to use AFHTTPRequestOperation and again set the responseSerializer to [AFJSONResponseSerializer serializer]. So something like

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
}];
Pragmatic answered 22/1, 2014 at 21:23 Comment(6)
BTW, the default responseSerializer is AFJSONResponseSerializer, so this line is redundant.Cuellar
@aahrens: Do AFHTTPSessionManager or AFHTTPRequestOperation prefer?Chaetopod
@AaronBrager doesn't seem to be the case on version 2.6.1 (late 2015)Mentality
@MaxMacLeod It is the default for both the session manager and the request operation manager: github.com/AFNetworking/AFNetworking/blob/…Cuellar
@AaronBrager then it's not working. Knock out the operation.responseSerializer = [AFJSONResponseSerializer serializer]; line above and take a look at the responseObject in the debugger. The type of responseObject differs when that line is removedMentality
@MaxMacLeod That default doesn't apply if you're creating a request operation by hand, vs. using a session / operation manager. IE, the first code sample in this answer doesn't need to set it, but the second one does.Cuellar
C
7

From NSHipster's article on AFNetworking 2:

One of the breakthroughs of AFNetworking 2.0's new architecture is use of serializers for creating requests and parsing responses. The flexible design of serializers allows for more business logic to be transferred over to the networking layer, and for previously built-in default behavior to be easily customized.

In AFNetworking 2, serializers (the objects that turn HTTP data into usable Objective C objects) are now separate objects from the request operation object.

AFJSONRequestOperation, etc. therefore no longer exist.

From the AFJSONResponseSerializer docs:

AFJSONResponseSerializer is a subclass of AFHTTPResponseSerializer that validates and decodes JSON responses.

There are a few ways to hit the API you mentioned. Here's one:

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];
Cuellar answered 22/1, 2014 at 21:36 Comment(1)
Thanks for the links, the explanation and the example!Christophany

© 2022 - 2024 — McMap. All rights reserved.