我正在開發一款Android應用程序,需要通過關鍵字搜索YouTube的視頻。我已經使用YouTube數據API與下面的代碼:如何在Android中使用YouTube數據API搜索視頻
try {
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, new HttpRequestInitializer() {
public void initialize(HttpRequest request) throws IOException {
}
}).setApplicationName("YoutubeQoE").build();
// Define the API request for retrieving search results.
YouTube.Search.List search = youtube.search().list("id,snippet");
// Set your developer key from the Google Cloud Console for
// non-authenticated requests. See:
// https://cloud.google.com/console
search.setKey(DeveloperKey.DEVELOPER_KEY);
search.setQ("dogs");
// Restrict the search results to only include videos. See:
// https://developers.google.com/youtube/v3/docs/search/list#type
search.setType("video");
// To increase efficiency, only retrieve the fields that the
// application uses.
//search.setFields("items(id/kind,id/videoId,snippet/title,snippet/thumbnails/default/url)");
search.setMaxResults(25);
SearchListResponse searchResponse = search.execute();
List<SearchResult> lista = searchResponse.getItems();
} catch (GoogleJsonResponseException e) {
System.err.println("There was a service error: " + e.getDetails().getCode() + " : "
+ e.getDetails().getMessage());
} catch (IOException e) {
System.err.println("There was an IO error: " + e.getCause() + " : " + e.getMessage());
} catch (Throwable t) {
t.printStackTrace();
}
對於德Developer_Key開發我已經使用在谷歌開發者控制檯公共API訪問。
但是,當我執行該程序沒有在線路中的問題:
SearchListResponse searchResponse = search.execute();
在Android的manifest.xml我擁有這些權限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
我會很感激,如果有人可以幫助我
你得到的錯誤是什麼? –
親愛的Josesvm,我正在做同樣的嘗試。但是我無法獲得這個搜索java類文件的支持庫。所以請分享jar文件或jar文件的位置。 –