Aluminum
Aluminum

Reputation: 2992

How to parse a response obtained with AFNetworking 1.0 using JSON format

I'm having a problem with AFNetworking.

Currently I can send a POST request in JSON format [AFJSONParameterEncoding] using NSDictionary to a server and it correctly replies, the problem is that the server replies with a JSON formatted response too, response I'm able to convert to NSString using:

[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
// responseObject is the server response

The problem is I'm not able to transform the response in any other format, other than NSString like in the code posted before. How can that be possible? I would like to convert the response to JSON format so I can read a precise value, the value associated with the key "isInformative"

Here's my code so far:

NSDictionary *requestBody = [NSDictionary dictionaryWithObjectsAndKeys:
                                     @"value1", @"key1",
                                     @"value2", @"key2",
                                     nil];

NSDictionary *requestHead = @{
                              @"RequestHead": requestBody
                             };

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://XXX.XXX.XXX.XXX:XXXX"]];
[httpClient setParameterEncoding:AFJSONParameterEncoding];
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];

NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"
                                                        path:@"/public-mobile-sdk/"
                                                  parameters:requestHead];

AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *requestOperation, id responseObject) {
    // Here I can convert the responseObject to NSSTring correctly
    NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);

    } failure:^(AFHTTPRequestOperation *requestOperation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

[requestOperation start];

Note - I can't update the AFNetworking version bundled in the Xcode project since it's not my own, so sadly I must stick and use version 1.X

Upvotes: 0

Views: 686

Answers (1)

Aluminum
Aluminum

Reputation: 2992

This solved my problem:

AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:jailbreakRequest];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *requestOperation, id responseObject) {
    //Place this line of code below to create a NSDictionary from the async server response
    NSDictionary *jsonList = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
    } failure:^(AFHTTPRequestOperation *requestOperation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

[requestOperation start];

Thanks anyway :-)

Upvotes: 1

Related Questions