1

我需要使用 AFNetworking 在 UITableView 上加载 50 多个 youtube 视频。

我是否必须在 viewdidload 中多次使用 AFJSONRequestOperation 来下载数据?

这是我尝试过的:

NSString *urlAsString = @"http://gdata.youtube.com/feeds/api/playlists/PL7CF5B0AC3B1EB1D5?v=2&alt=jsonc&max-results=50";

NSString *urlAsString2 = @"http://gdata.youtube.com/feeds/api/playlists/PL7CF5B0AC3B1EB1D5?v=2&alt=jsonc&max-results=50&start-index=51";
NSURL *url = [NSURL URLWithString:urlAsString];
NSURL *url2 = [NSURL URLWithString:urlAsString2];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLRequest *request2 = [NSURLRequest requestWithURL:url2];

//现在我不知道该怎么做。

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    self.videoMetaData = [JSON valueForKeyPath:@"data.items.video"];
    self.allThumbnails = [JSON valueForKeyPath:@"data.items.video.thumbnail"];
    [self.tableView reloadData]; 
    // NSLog(@" video Meta Data %@", self.videoMetaData);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
    NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];
[operation start];
4

1 回答 1

0

为什么不直接创建一个方法并将 URL 传递给它呢?

-(void)loadVideoFromURL:(NSURL *)url {

    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    // setup AFNetworking stuff
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    // call delegate or processing method on success
    [self someDelegateMethod:(NSDictionary *)JSON];

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
    NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
    }];


    [operation start];

}
于 2013-02-12T01:25:07.567 回答