Reputation: 13085
I know my request is formatted properly, but the response I get from the web service is not an NSDictionary.
How can I tell what kind of object "responseObject" is?
AFHTTPRequestOperation *operation = [[AFParseAPIClient sharedClient] HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
if ([responseObject isKindOfClass:[NSDictionary class]]) {
NSLog(@"Response for %@: %@", className, responseObject);
[self writeJSONResponse:responseObject toDiskForClassWithName:className];
}else{
//NSLog(@"Response NOT NSDictionary: %@", [responseObject class]);
NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"str: %@", str);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Request for class %@ failed with error: %@", className, error);
}];
Here's what I get:
//Response NOT NSDictionary: <7b227265 73756c74 73223a5b 7b22636f ...>
EDIT: here's what I get back:
str: {"results":[{"desc":"My description.","device_iPad":true,"device_iPhone":true,"createdAt":"2012-09-05T18:36:11.431Z","updatedAt":"2012-09-05T22:00:52.199Z"}]}
Upvotes: 0
Views: 5368
Reputation: 1588
just add this line of code after creating your operation
operation.responseSerializer = [AFJSONResponseSerializer serializer];
Upvotes: 4
Reputation: 597
Use AFJSONRequestOperation and AFNetworking returns a JSON response. Using AFHTTPRequestOperation returns NSData which you can convert (cast) to NSDictionary and print using the following:
NSLog(@"Response: %@", [[NSString alloc] initWithData:(NSData *)json encoding:NSUTF8StringEncoding]);
My advice is to just use AFJSONRequestOperation!
Upvotes: 0
Reputation: 31
I ran into the same problem but performing requests with a subclass of AFHTTPClient. I was getting a __NSCFData instead of a __NSCFDictionary. I solved it with this:
[self setDefaultHeader:@"Accept" value:@"application/json"];
self is my AFHTTPClient subclass.
Upvotes: 2
Reputation: 1727
Would this be useful to you? It's from AFNetworking github project
NSURL *url = [NSURL URLWithString:@"https://gowalla.com/users/mattt.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperationJSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"Name: %@ %@", [JSON valueForKeyPath:@"first_name"], [JSON valueForKeyPath:@"last_name"]);
} failure:nil];
[operation start];
Give it a try, it's pretty straightforward. *If it's still an array do [[JSON objectAtIndex:0] objectForKey@"desc /*(or whatever)*/"]
Upvotes: 1