我使用AFNetworking在youtube视频中填充UITableView。 YouTube API仅允许每个请求获得最多50个结果。因此,我必须使用多个网址才能获得超过50个结果。
我创建了一个方法,它将对给定的URL执行AFNetworkings AFJSONRequestOperation。
我认为在收到JSON数据之前就已经创建了UITable。在创建Method之前,一切都运行良好。
这是我第一次创建方法。在过去的几天里,我一直试图在UITable上加载超过50个YouTube视频。请看一下我的代码。
这是我的代码,您也可以从
下载整个项目QQViewController.m
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//[super viewDidLoad];
NSString *urlAsString = @"http://gdata.youtube.com/feeds/api/playlists/PL7CF5B0AC3B1EB1D5?v=2&alt=jsonc&max-results=50";
// I am not sure how i am supposed to populate the uitableview with the second link :(
NSString *urlAsString2 = @"http://gdata.youtube.com/feeds/api/playlists/PL7CF5B0AC3B1EB1D5?v=2&alt=jsonc&max-results=50&start-index=51";
self.myurl = [NSURL URLWithString:urlAsString];
[self getJSONfromURL:self.myurl];
[self.tableView reloadData];
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.videoMetaData = [self.myJSON valueForKeyPath:@"items.video"];
NSLog(@" QQVC video Meta Data %@", self.videoMetaData);
self.allThumbnails = [self.myJSON valueForKeyPath:@"data.items.video.thumbnail"];
// The table need to be reloaded or else we will get an empty table.
[self.tableView reloadData]; // Must Reload
// NSLog(@" video Meta Data %@", self.videoMetaData);
}
//这是方法
-(void)getJSONfromURL:(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.myJSON = (NSArray *)JSON];
self.myJSON = [JSON valueForKey:@"data"];
[self.tableView reloadData];
//NSLog(@" in get JSon method %@", self.myJSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];
[operation start];
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
答案 0 :(得分:1)
getJSONfromURL:方法中存在几个问题。主要问题是你将self.myJSON定义为[JSON valueForKey:@“data”],然后将self.allThumbnails定义为[self.myJSON valueForKeyPath:@“data.items.video.thumbnail”] - 你用过的再次“数据”,所以self.thumbnails是null。这似乎工作正常:
-(void)getJSONfromURL:(NSURL *)url {
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// setup AFNetworking stuff
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
self.myJSON = [JSON valueForKey:@"data"];
self.allThumbnails = [self.myJSON valueForKeyPath:@"items.video.thumbnail"];
self.videoMetaData = [self.myJSON valueForKeyPath:@"items.video"];
[self.tableView reloadData];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];
[operation start];
}
如果您想从多个网址加载日期,可以这样做。我添加了一个计数器来跟踪URL字符串数组的进度。每次下载后都会重新加载该表,因此在显示某些数据之前,不必等待整个事情完成。
@interface QWViewController ()
@property (strong,nonatomic) NSArray *urlStrings;
@end
@implementation QWViewController {
int counter;
}
-(void)viewDidLoad {
[super viewDidLoad];
counter = 0;
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";
self.urlStrings = @[urlAsString,urlAsString2];
self.allThumbnails = [NSMutableArray array];
self.videoMetaData = [NSMutableArray array];
[self getJSONFromURL:self.urlStrings[0]];
}
-(void)getJSONFromURL:(NSString *) urlString {
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
self.myJSON = [JSON valueForKey:@"data"];
[self.allThumbnails addObjectsFromArray:[self.myJSON valueForKeyPath:@"items.video.thumbnail"]];
[self.videoMetaData addObjectsFromArray:[self.myJSON valueForKeyPath:@"items.video"]];
[self.tableView reloadData];
counter += 1;
if (counter < self.urlStrings.count) [self getJSONFromURL:self.urlStrings[counter]];
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];
[operation start];
}
答案 1 :(得分:0)
将重新加载的tableview调用移至此处。
-(void)getJSONfromURL:(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.myJSON = (NSArray *)JSON];
self.myJSON = [JSON valueForKey:@"data"];
[self.tableView reloadData];
//NSLog(@" in get JSon method %@", self.myJSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];
[operation start];
}