Youtube Data API v3 java搜索与分页问题

时间:2013-12-09 03:51:16

标签: java youtube-api google-api-java-client

我正在使用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);
  }

我错过了什么?我是否需要设置一些内容来获取响应中的标题?

提前感谢您的回答

2 个答案:

答案 0 :(得分:3)

这是因为您将字段设置为仅返回“项目”。如果您只想返回item和nextpageToken,可以将其设置为“items,nextPageToken”

答案 1 :(得分:0)

// 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);
 }

//使用递归函数的示例。打印搜索字词的所有结果&#34; puppy&#34;。

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);