我关注了这个截屏视频... http://nsscreencast.com/episodes/6-afnetworking
我的单例AFHTTPClient
代码是...
+ (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;
}
然后我正在执行它就像......
[[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);
}];
一切正常。我已经用 Charles 拦截了它,它正在发送正确的请求并接收正确的 JSON,但该操作不是 JSON 操作。
所以这responseObject
不是我NSData
所JSON object
期待的。
我错过了使用 JSON 操作的任何配置?