user3587834
user3587834

Reputation: 13

AFNetworking iOS - way to see actual data in NSLog after GET request is complete

I have a simple call to an API method below called queryJobAndMoveWithName that makes GET request (ex. http://www.imdb.com/xml/find?json=1&nr=1&nm=on&q=jennifer).

However when I look at the log responseJSON gets rendered as:

<57652068 6f706520 746f206c 61756e63 68205353 4c206163 63657373 20746f20 616c6c20 70616765 73206f6e 20494d44 6220696e 20746865 20766572 79206e65 61722066 75747572 652e0d0a 0d0a5468 616e6b73 20666f72 20796f75 72207061 7469656e 63652e> other than {XXX:XXX, XXX:XXX}

- (void)queryJobAndMoveWithName:(NSString *)name completionHandler:(void (^)(NSDictionary *resultsJSON, NSError *error))completionHandler {
    
    NSString *url = [NSString stringWithFormat:@"%@%@?json=1&nr=1&nm=on&q=%@", kAPIHost, kSearchPath, name];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/javascript" ,@"text/html", @"text/plain", @"text/json", nil];
    
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseJSON) {
        NSLog(@"JSON: %@", responseJSON);
        completionHandler(responseJSON, nil);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
        completionHandler(nil, error);
    }];
}

Here is a method that calls the API method from view controller

- (IBAction)searchButtonTapped:(id)sender {
    NSLog(@"%@",self.searchTextField.text);
    NSString *queryName = self.searchTextField.text;
    NSArray *fullName = [queryName componentsSeparatedByString:@" "];
    NSString *formattedFullName = [fullName componentsJoinedByString:@"+"];
    
    FPAImdbApi *imdbApi = [[FPAImdbApi alloc] init];
    [imdbApi queryJobAndMoveWithName:formattedFullName completionHandler:^(NSDictionary *resultsJSON, NSError *error) {
        if (error) {
            NSLog(@"error: %@", error);
        } else if (resultsJSON) {
            NSLog(@"result json: \n %@", resultsJSON);
        } else {
            NSLog(@"nothing found");
        }

    }];
    
}

So my question is how do I view the contents of the resulting JSON in logs other than the form of memory?

Upvotes: 0

Views: 215

Answers (1)

rckoenes
rckoenes

Reputation: 69469

You have not told the manager that the response is in JSON, here you set the response serializer to HTTP:

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

Chang it to JSON:

manager.responseSerializer = [AFJSONResponseSerializer serializer];

In the error block you could do something like this to print out the response from the server:

if (error) {
    NSHTTPURLResponse *respone = error.userInfo[AFNetworkingOperationFailingURLResponseErrorKey];
    NSData *errorData = error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey];
    NSString *responseString = [[NSString alloc] initWithData:errorData encoding:NSUTF8StringEncoding];
    NSLog(@"Response: %@", responseString);
}

Upvotes: 0

Related Questions