AFNetworking AFHTTPClient AFJSONRequestOperation not using JSON Operation
I followed this Screencast... http://nsscreencast.com/episodes/6-afnetworking
My singleton AFHTTPClient code is...
+ (MyClient *)sharedInstance
{
static dispatch_once_t once;
static MyClient *myClient;
dispatch_once(&once, ^ { myClient = [[MyClient alloc]
initWithBaseURL:[NSURL URLWithString:MyBaseURL]];});
return myClient;
}
- (id)initWithBaseURL:(NSURL *)url
{
self = [super initWithBaseURL:url];
if (self) {
// these are not actual values but I am setting default headers.
[self setDefaultHeader:@"sdfg" value:@"4"];
[self setDefaultHeader:@"std" value:@"3"];
[self setDefaultHeader:@"reg" value:@"5"];
[self setDefaultHeader:@"yu" value:@"1"];
[self setDefaultHeader:@"xv" value:@"3"];
[self setDefaultHeader:@"hmm" value:@"5"];
[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
}
return self;
}
Then I'm executing it like...
[[MyClient sharedInstance] getPath:@"blah.php" parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSMutableArray *stats = [NSMutableArray array];
// it crashes on the next line because responseObject is NSData
for (NSDictionary *dictionary in responseObject) {
CCStatistic *stat = [[CCStatistic alloc]
initWithDictionary:dictionary];
[stats addObject:stat];
}
self.stats = stats;
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error retrieving!");
NSLog(@"%@", error);
}];
It all works fine. I've intercepted it with Charles and it is sending the
correct request and receiving the correct JSON except the operation is not
a JSON operation.
So the responseObject is NSData not the JSON object that I was expecting.
An I missing any config to use the JSON operation?
No comments:
Post a Comment