我正在从 Web 服务获取数据,但它在 URL 请求时崩溃了,请参阅下面的代码
- (void)loadDataSource
{
// Request
NSString *URLPath = [NSString stringWithFormat:@"http://imgur.com/gallery.json"];
NSURL *URL = [NSURL URLWithString:URLPath];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
**CRASHED HERE** [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
if (!error && responseCode == 200) {
id res = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
if (res && [res isKindOfClass:[NSDictionary class]]) {
self.items = [res objectForKey:@"gallery"];
[self dataSourceDidLoad];
} else {
[self dataSourceDidError];
}
} else {
[self dataSourceDidError];
}
}];
}
请帮我
提前致谢