Using YouTube Data Api in Android(在 Android 中使用 YouTube 数据 API)
问题描述
我正在尝试在我的 Android 应用中使用 YouTube Data Api.当我尝试将它作为 java 项目运行时,它运行良好,没有任何错误.但是当我尝试在我的 android 应用程序中做同样的事情时,应用程序强制关闭.这是 logcat 和我的代码的输出.
Am trying to use YouTube Data Api in my android app. When i try to run it as a java project, it runs fine without any errors. But when i try to do the same thing in my android application , the application force closes. Here is the output from logcat and my code.
LOGCAT 的输出
01-05 00:13:17.198: E/AndroidRuntime(627): FATAL EXCEPTION: main
01-05 00:13:17.198: E/AndroidRuntime(627): java.lang.ExceptionInInitializerError
01-05 00:13:17.198: E/AndroidRuntime(627): at com.example.allinonedata.YouTubeManager.retrieveVideos(YouTubeManager.java:29)
...
01-05 00:13:17.198: E/AndroidRuntime(627): Caused by: java.lang.NoClassDefFoundError: com.google.gdata.data.media.MediaSource
这里是代码mainactivity.java
HERE IS THE CODE mainactivity.java
package com.example.allinonedata;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
String clientID = "JavaCodeGeeks";
String textQuery = "nexus 4";
int maxResults = 1;
boolean filter = true;
int timeout = 2000;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
YouTubeManager ym = new YouTubeManager(clientID);
try{
List<YouTubeVideo> videos = ym.retrieveVideos(textQuery, maxResults, filter, timeout);
for (YouTubeVideo youtubeVideo : videos) {
System.out.println(youtubeVideo.getWebPlayerUrl());
System.out.println("Thumbnails");
for (String thumbnail : youtubeVideo.getThumbnails()) {
System.out.println(" " + thumbnail);
}
System.out.println(youtubeVideo.getEmbeddedWebPlayerUrl());
System.out.println("**************************************************");
}
}
catch(Exception e)
{
}
}
}
youtubemanager.java
youtubemanager.java
package com.example.allinonedata;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
import com.google.gdata.client.youtube.YouTubeQuery;
import com.google.gdata.client.youtube.YouTubeService;
import com.google.gdata.data.media.mediarss.MediaThumbnail;
import com.google.gdata.data.youtube.VideoEntry;
import com.google.gdata.data.youtube.VideoFeed;
import com.google.gdata.data.youtube.YouTubeMediaContent;
import com.google.gdata.data.youtube.YouTubeMediaGroup;
public class YouTubeManager {
private static final String YOUTUBE_URL = "http://gdata.youtube.com/feeds/api/videos";
private static final String YOUTUBE_EMBEDDED_URL = "http://www.youtube.com/v/";
private String clientID;
public YouTubeManager(String clientID) {
this.clientID = clientID;
}
public List<YouTubeVideo> retrieveVideos(String textQuery, int maxResults,
boolean filter, int timeout) throws Exception {
YouTubeService service = new YouTubeService(clientID);
service.setConnectTimeout(timeout); // millis
YouTubeQuery query = new YouTubeQuery(new URL(YOUTUBE_URL));
query.setOrderBy(YouTubeQuery.OrderBy.RELEVANCE);
query.setFullTextQuery(textQuery);
query.setSafeSearch(YouTubeQuery.SafeSearch.NONE);
query.setMaxResults(maxResults);
VideoFeed videoFeed = service.query(query, VideoFeed.class);
List<VideoEntry> videos = videoFeed.getEntries();
return convertVideos(videos);
}
private List<YouTubeVideo> convertVideos(List<VideoEntry> videos) {
List<YouTubeVideo> youtubeVideosList = new LinkedList<YouTubeVideo>();
for (VideoEntry videoEntry : videos) {
YouTubeVideo ytv = new YouTubeVideo();
YouTubeMediaGroup mediaGroup = videoEntry.getMediaGroup();
String webPlayerUrl = mediaGroup.getPlayer().getUrl();
ytv.setWebPlayerUrl(webPlayerUrl);
String query = "?v=";
int index = webPlayerUrl.indexOf(query);
String embeddedWebPlayerUrl = webPlayerUrl.substring(index+query.length());
embeddedWebPlayerUrl = YOUTUBE_EMBEDDED_URL + embeddedWebPlayerUrl;
ytv.setEmbeddedWebPlayerUrl(embeddedWebPlayerUrl);
List<String> thumbnails = new LinkedList<String>();
for (MediaThumbnail mediaThumbnail : mediaGroup.getThumbnails()) {
thumbnails.add(mediaThumbnail.getUrl());
}
ytv.setThumbnails(thumbnails);
List<YouTubeMedia> medias = new LinkedList<YouTubeMedia>();
for (YouTubeMediaContent mediaContent : mediaGroup.getYouTubeContents()) {
medias.add(new YouTubeMedia(mediaContent.getUrl(), mediaContent.getType()));
}
ytv.setMedias(medias);
youtubeVideosList.add(ytv);
}
return youtubeVideosList;
}
}
推荐答案
google-api-java-client(它确实支持 Android,不像 gdata-client)以前不支持 Youtube Data API,但是v3 的发布现已纠正;这个客户端现在有很好的 Youtube 支持,应该可以为你提供你需要的东西.有关详细信息和代码示例,请参见此处:
The google-api-java-client (which does have Android support, unlike the gdata-client) didn't previously support the Youtube Data API, but with the release of v3 this is now rectified; there is good Youtube support now with this client that should provide you what you need. See here for details and code samples:
https://developers.google.com/api-客户端库/java/apis/youtube/v3
还有这里的入门指南
https://developers.google.com/youtube/v3/getting-started
这篇关于在 Android 中使用 YouTube 数据 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Android 中使用 YouTube 数据 API
基础教程推荐
- iOS4 创建后台定时器 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
