如何通过 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?

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

问题描述

我的服务器将 videoID 列表发送到 Android.现在,我想在列表视图中显示这些视频的标题、缩略图和评论数.我已经使用对 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:

顾名思义,第一个库是专门为 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.

第二个库更通用(虽然有 在 Android 上使用它的单独说明) 并提供了一个围绕 YouTube 数据 API 的包装器,以使其与它的交互更容易一些.因此,它基本上允许您做所有您可以使用 Web API 做的事情.因此,它解决了与 Android Player API 不同的问题,如果您想完全控制在自己的 UI 中显示视频数据的方式,它更有可能是您的选择.

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 组件.各种网络库(即 Retrofit)可以大大简化这个过程.

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天全站免登陆