2013-12-09 47 views
1

我使用的是YouTube數據api v3,但我有一個奇怪的問題。我明白,對於分頁,我需要在發送後續請求時使用nextPageTokem,但我的問題是,我沒有在響應中獲得nextPageToken。我的代碼如下。Youtube Data API v3 java搜索與分頁問題

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

我錯過了什麼?我需要設置一些東西來獲取響應中的標題嗎?

在此先感謝您的回答

回答

3

這是因爲您設置字段只返回「項目」。如果你只想返回items和nextpageToken,你可以將它設置爲「items,nextPageToken」

+0

謝謝@Ibrahim Ulukaya,幫助。有沒有這些東西的任何文件。 v3的文檔看起來很稀少,除了一些樣本和javadoc。 – Karthik

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

//使用遞歸函數的示例。打印檢索詞「小狗」的所有結果。

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