0

我正在从 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];
        }
    }];
}    

请帮我

提前致谢

4

1 回答 1

0

您的代码是正确的,但是结果中没有键 @"gallery" 的对象。看起来关键是@“数据”。因此,将您的代码更改为

self.items = [res objectForKey:@"data"]
于 2012-11-05T23:41:55.207 回答