我使用的是youtube数据api v3,但我遇到了一个奇怪的问题。我确实理解,对于分页,我需要在发送后续请求时使用响应中的nextPageTokem,但我的问题是,我在响应中没有得到nextPageToken。我的代码如下。
youtube = new YouTube.Builder(HTTP_TRANSPORT, JSON_FACTORY, new HttpRequestInitializer() {
public void initialize(HttpRequest request) throws IOException {}
}).setApplicationName("DMT").build();
String queryTerm = "<my movie>";
YouTube.Search.List search = youtube.search().list("id,snippet");
String apiKey = properties.getProperty("youtube.apikey");
search.setQ(queryTerm);
search.setVideoDuration("long");
search.setType("video");
search.setFields("items(*)");
SearchListResponse searchResponse = search.execute();
System.out.println(searchResponse.toPrettyString());
System.out.println(searchResponse.getNextPageToken());
List<SearchResult> searchResultList = searchResponse.getItems();
if (searchResultList != null) {
System.out.println(searchResponse.getPageInfo());
prettyPrint(searchResultList.iterator(), queryTerm);
}
我错过了什么?我需要设置一些东西来获得响应中的标头吗?
提前感谢您的回答
这是因为您将字段设置为只返回"items"。如果您只想返回items和nextpageToken,您可以将其设置为"items,nextpageToken"
// Recursive function to print an entire feed.
public static void printEntireVideoFeed(YouTubeService service,
VideoFeed videoFeed, boolean detailed) throws MalformedURLException,
IOException, ServiceException {
do {
printVideoFeed(videoFeed, detailed);
if(videoFeed.getNextLink() != null) {
videoFeed = service.getFeed(new URL(videoFeed.getNextLink().getHref()),
VideoFeed.class);
}
else {
videoFeed = null;
}
}
while(videoFeed != null);
}
//递归函数的示例用法。打印搜索词"小狗"的所有结果。
YouTubeQuery query =
new YouTubeQuery(new URL("http://gdata.youtube.com/feeds/api/videos"));
query.setFullTextQuery("puppy");
VideoFeed videoFeed = service.query(query, VideoFeed.class);
printEntireVideoFeed(service, videoFeed, false);