如何通过Android中的Data API v3.0使用videoID从youtube检索单个视频的详细信息? [英] How to retrieve details of single video from youtube using videoID through Data API v3.0 in Android?

查看:88
本文介绍了如何通过Android中的Data API v3.0使用videoID从youtube检索单个视频的详细信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器将videoID列表发送到Android.现在,我想在列表视图中显示这些视频的标题,缩略图和评论数.我已经在Web中使用对https://www.googleapis.com/youtube/v3/videos?part=snippet&id={VIDEO_ID}&key={YOUR_API_KEY}的GET请求完成了此操作,但是如何在Android中执行此操作?是否有任何YouTube SDK可以初始化YouTube对象?如何使用VideoID从YouTube检索此信息?

My server sends the list of videoID to Android. Now, I want to show Title, Thumbnail and Number of Comments on these videos in List View. I have done this in web using GET request to https://www.googleapis.com/youtube/v3/videos?part=snippet&id={VIDEO_ID}&key={YOUR_API_KEY} but how to do this in Android? Is there any YouTube SDK to initialize YouTube object? How do I retrieve this information from YouTube using VideoID?

我找到了使用 YouTube数据的方法Java的API客户端库,但是它给出了运行时错误,没有任何解释.

I have found a way to this using YouTube Data API Client Library for Java but It is giving runtime error without any explanation.

这是我使用的代码

/**
 * Define a global instance of a Youtube object, which will be used
 * to make YouTube Data API requests.
 */
private static YouTube youtube;

youtube = new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(), new HttpRequestInitializer(){
        public void initialize(com.google.api.client.http.HttpRequest request) throws IOException {
        }
    }).setApplicationName("youtube-cmdline-search-sample").build();

// Call the YouTube Data API's videos.list method to retrieve videos.
    VideoListResponse videoListResponse = youtube.videos().
        list("snippet").setId(videoId).execute();

    // Since the API request specified a unique video ID, the API
    // response should return exactly one video. If the response does
    // not contain a video, then the specified video ID was not found.
    List<Video> videoList = videoListResponse.getItems();
    if (videoList.isEmpty()) {
        System.out.println("Can't find a video with ID: " + videoId);
        return;
    }
    Video video = videoList.get(0)
    // Print information from the API response.
}

推荐答案

YouTube提供(至少)两个与您的问题相关的官方库:

YouTube provides (at least) two official libraries relevant to your question:

  • YouTube Android Player API
  • YouTube Data API Client Library for Java

顾名思义,第一个库是专门为Android平台开发的.它的重点是通过提供播放器框架使您能够将视频播放功能合并到应用程序中.如果您的目标是使用户能够简单地播放YouTube视频,则可能最容易实现.请注意,该库要求在设备上安装正式的YouTube应用.

As the name already suggests, the first library is specifically developed for the Android platform. Its focus is on enabling you to incorporate video playback functionality into an app by providing a player framework. If your goal is to enable users to simply play YouTube videos, then is probably easiest to implement. Do note that this library requires the official YouTube app to be installed on the device.

第二个库更通用(尽管有

The second library is more generic (although there are separate instructions for using it on Android) and provides a wrapper around YouTube's Data API to make interfacing with it a little easier. Hence, it allows you to do basically everything you can also do with the web API. As such, it solves a different problem than the Android Player API and is more likely the way to go if you want full control over how you display video data in your own UI.

您的第三个选择是完全按照您的基于Web的解决方案进行操作:自行调用API,解析响应并将相关数据绑定到UI组件.各种网络库(即翻新)可以大大简化此过程.

Your third option would be to do exactly what you did for your web-based solution: make the API call yourself, parse the response and bind up the relevant data to your UI components. Various networking libraries (i.e. Retrofit) can greatly simplify this process.

这篇关于如何通过Android中的Data API v3.0使用videoID从youtube检索单个视频的详细信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆