使用YouTube数据API

使用YouTube数据API

本文介绍了在Android中使用YouTube数据API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想使用YouTube数据API在我的Andr​​oid应用程序。当我尝试运行它作为一个Java项目,它运行良好,没有任何错误。但是,当我尝试做同样的事情在我的Andr​​oid应用程序,该应用程序的力量关闭。下面是从logcat的输出和我的code。

输出logcat的

  01-05 00:13:17.198:E / AndroidRuntime(627):致命异常:主要
01-05 00:13:17.198:E / AndroidRuntime(627):java.lang.ExceptionInInitializerError
01-05 00:13:17.198:E / AndroidRuntime(627):在com.example.allinonedata.YouTubeManager.retrieveVideos(YouTubeManager.java:29)
...
01-05 00:13:17.198:E / AndroidRuntime(627):java.lang.NoClassDefFoundError的:产生的原因com.google.gdata.data.media.MediaSource
 

这里是codemainactivity.java

 包com.example.allinonedata;
 进口的java.util.List;

  进口android.os.Bundle;
  进口android.app.Activity;
  进口android.view.Menu;

   公共类MainActivity延伸活动{
字符串clientID的=Java的codeGeeks;
字符串textQuery =承上启下4;
INT maxResults = 1;
布尔过滤=真;
INT超时= 2000;
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    YouTubeManager YM =新YouTubeManager(clientID的);
    尝试{
    名单< YouTubeVideo>视频= ym.retrieveVideos(textQuery,maxResults,过滤器,超时);

    对于(YouTubeVideo youtubeVideo:视频){
        的System.out.println(youtubeVideo.getWebPlayerUrl());

        的System.out.println(缩略图);
        对于(字符串缩略图:youtubeVideo.getThumbnails()){
            的System.out.println(\ t+缩略图);
        }
        的System.out.println(youtubeVideo.getEmbeddedWebPlayerUrl());
        的System.out.println(********* *******);
    }
    }
    赶上(例外五)
    {

    }
}
 

}

youtubemanager.java

 包com.example.allinonedata;

 进口的java.net.URL;
 进口java.util.LinkedList中;
 进口的java.util.List;

 进口com.google.gdata.client.youtube.YouTubeQuery;
 进口com.google.gdata.client.youtube.YouTubeService;
 进口com.google.gdata.data.media.mediarss.MediaThumbnail;
 进口com.google.gdata.data.youtube.VideoEntry;
 进口com.google.gdata.data.youtube.VideoFeed;
 进口com.google.gdata.data.youtube.YouTubeMediaContent;
 进口com.google.gdata.data.youtube.YouTubeMediaGroup;

 公共类YouTubeManager {

私有静态最后弦乐YOUTUBE_URL =htt​​p://gdata.youtube.com/feeds/api/videos;
私有静态最后弦乐YOUTUBE_EMBEDDED_URL =htt​​p://www.youtube.com/v/;

私人字符串clientID的;

公共YouTubeManager(字符串clientID的){
    this.clientID = clientID的;
}

公开名单< YouTubeVideo> retrieveVideos(字符串textQuery,诠释maxResults,
        布尔过滤器,INT超时)抛出异常{

    YouTubeService服务=新YouTubeService(clientID的);
    service.setConnectTimeout(超时); //米利斯
    YouTubeQuery查询=新YouTubeQuery(新的URL(YOUTUBE_URL));

    query.setOrderBy(YouTubeQuery.OrderBy.RELEVANCE);
    query.setFullTextQuery(textQuery);
    query.setSafeSearch(YouTubeQuery.SafeSearch.NONE);
    query.setMaxResults(maxResults);

    VideoFeed videoFeed = service.query(查询,VideoFeed.class);
    名单< VideoEntry>视频= videoFeed.getEntries();

    返回convertVideos(视频)

}

私人列表< YouTubeVideo> convertVideos(名单< VideoEntry>视频){

    名单< YouTubeVideo> youtubeVideosList =新的LinkedList< YouTubeVideo>();

    对于(VideoEntry videoEntry:视频){

        YouTubeVideo YTV =新YouTubeVideo();

        YouTubeMediaGroup MEDIAGROUP = videoEntry.getMediaGroup();
        字符串webPlayerUrl = mediaGroup.getPlayer()的getURL()。
        ytv.setWebPlayerUrl(webPlayerUrl);

        ?V =字符串查询=;
        INT指数= webPlayerUrl.indexOf(查询);
        字符串embeddedWebPlayerUrl = webPlayerUrl.substring(指数+ query.length());
        embeddedWebPlayerUrl = YOUTUBE_EMBEDDED_URL + embeddedWebPlayerUrl;
        ytv.setEmbeddedWebPlayerUrl(embeddedWebPlayerUrl);

        名单<字符串>缩略图=新的LinkedList<字符串>();
        为(MediaThumbnail mediaThumbnail:mediaGroup.getThumbnails()){
            thumbnails.add(mediaThumbnail.getUrl());
        }
        ytv.setThumbnails(缩略图);

        名单< YouTubeMedia>媒体=新的LinkedList< YouTubeMedia>();
        为(YouTubeMediaContent mediaContent:mediaGroup.getYouTubeContents()){
            medias.add(新YouTubeMedia(mediaContent.getUrl(),mediaContent.getType()));
        }
        ytv.setMedias(媒体);

        youtubeVideosList.add(YTV);

    }

    返回youtubeVideosList;

}
 

}

解决方案

在谷歌-API的Java客户端(里面确实有Android的支持,gdata的客户端不同)没有previously支持YouTube数据API ,但与v3的,这是现在整流的释放;现在有很好的Youtube支持,这个客户你需要什么,应该提供给您。看到这里的细节和code样品:

https://developers.google.com/api-客户端库/爪哇/原料药/ YouTube的/ V3

和也以入门指南这里

https://developers.google.com/youtube/v3/getting-started

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.

OUTPUT FROM 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

HERE IS THE CODEmainactivity.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("\t" + thumbnail);
        }
        System.out.println(youtubeVideo.getEmbeddedWebPlayerUrl());
        System.out.println("**************************************************");
    }
    }
    catch(Exception e)
    {

    }
}

}

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;

}

}

解决方案

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-client-library/java/apis/youtube/v3

and also to Getting started Guide here

https://developers.google.com/youtube/v3/getting-started

这篇关于在Android中使用YouTube数据API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 14:04