使用“Google APIs Client Library for Java”访问 youtube 视频数据

发布于 2024-12-05 15:54:39 字数 3025 浏览 1 评论 0原文

我正在尝试实现一个可以浏览和显示 youtube 内容(最重要的是缩略图)的 android 应用程序。

现在通常的方法是通过 Youtube API 和 Gdata,但不幸的是,它们在 Android 上运行得不好。

因此,我尝试使用 Google APIs Client Library for Java (http://code.google.com/p/google-api-java-client/),

它显然与 Android 完全兼容。不幸的是,由于它仍处于开发早期,因此没有太多信息。

我已成功查询 youtube 的数据库并返回标题和描述等数据,但在返回缩略图时遇到很多麻烦。老实说,我不太清楚查询过程是如何工作的。

这是代码的主要部分:

HttpTransport transport = new NetHttpTransport();                  
        final JsonFactory jsonFactory = new JacksonFactory();      
        final JsonCParser parser = new JsonCParser(jsonFactory);

        HttpRequestFactory factory = transport.createRequestFactory(new HttpRequestInitializer() {

            @Override
            public void initialize(HttpRequest request) throws IOException {
                // TODO Auto-generated method stub

                GoogleHeaders headers = new GoogleHeaders();
                headers.setApplicationName("1Google-YouTubeSample/1.0");
                headers.gdataVersion = "2";
                request.setHeaders(headers);
                request.addParser(parser);
            }
        });


        YouTubeUrl url = new YouTubeUrl("https://gdata.youtube.com/feeds/api/videos");
        url.author = "whatever";
        url.maxResults = 2;

        HttpRequest request;
        try {
            request = factory.buildGetRequest(url);

            VideoFeed feed = request.execute().parseAs(VideoFeed.class);

            for (Video video : feed.items) {

                Log.v("ddarz", "video URL" + video.player.defaultUrl + "/default.jpg");
                TextView tv1 = (TextView) findViewById(R.id.textView1);
                tv1.setText(video.title);

                TextView tv2 = (TextView) findViewById(R.id.textView2);
                tv2.setText(video.description);

                ImageView iv = (ImageView) findViewById(R.id.imageView1);
                Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL("http://img.youtube.com/vi/bQVoAWSP7k4/1.jpg").getContent());
                //iv.setImageBitmap(bitmap); 

            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }

和类:

public static class VideoFeed {
        @Key List<Video> items;
        int totalItems;
      }

      public static class Video {
        @Key String title;
        @Key String description;
        @Key Player player;
        @Key DateTime updated;
      }

      public static class Player {
        @Key("default") String defaultUrl;
      }

      public static class YouTubeUrl extends GenericUrl {
        @Key final String alt = "jsonc";
        @Key String author;
        @Key("max-results") Integer maxResults;

        YouTubeUrl(String url) {
          super(url);
        }
      }

代码非常粗糙,但到目前为止它可以工作,但在尝试从 URL 检索图像时会失败。

有谁对如何最好地访问视频数据(缩略图等?)或一般如何解决此问题有任何见解和/或建议?不幸的是,Android 平台上的 YouTube 访问很少被提及。

非常感谢!

I am trying to implement an android application that can browse and display youtube content (most importantly thumbnails).

Now normally the way to go would be through the Youtube API and Gdata but unfortunately, they do not play well with Android.

Therefore I have attempted to use the Google APIs Client Library for Java (http://code.google.com/p/google-api-java-client/)

which apparently is fully compatible with Android. Unfortuantely as it is still early in its development there is not much informartion around.

I have managed to succesfully query youtube's database and return data such as titles and descriptions but I am having a lot of trouble returning thumbnails. To be honest, I am not quite sure how the query process works.

here is the main part of the code:

HttpTransport transport = new NetHttpTransport();                  
        final JsonFactory jsonFactory = new JacksonFactory();      
        final JsonCParser parser = new JsonCParser(jsonFactory);

        HttpRequestFactory factory = transport.createRequestFactory(new HttpRequestInitializer() {

            @Override
            public void initialize(HttpRequest request) throws IOException {
                // TODO Auto-generated method stub

                GoogleHeaders headers = new GoogleHeaders();
                headers.setApplicationName("1Google-YouTubeSample/1.0");
                headers.gdataVersion = "2";
                request.setHeaders(headers);
                request.addParser(parser);
            }
        });


        YouTubeUrl url = new YouTubeUrl("https://gdata.youtube.com/feeds/api/videos");
        url.author = "whatever";
        url.maxResults = 2;

        HttpRequest request;
        try {
            request = factory.buildGetRequest(url);

            VideoFeed feed = request.execute().parseAs(VideoFeed.class);

            for (Video video : feed.items) {

                Log.v("ddarz", "video URL" + video.player.defaultUrl + "/default.jpg");
                TextView tv1 = (TextView) findViewById(R.id.textView1);
                tv1.setText(video.title);

                TextView tv2 = (TextView) findViewById(R.id.textView2);
                tv2.setText(video.description);

                ImageView iv = (ImageView) findViewById(R.id.imageView1);
                Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL("http://img.youtube.com/vi/bQVoAWSP7k4/1.jpg").getContent());
                //iv.setImageBitmap(bitmap); 

            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }

And the classes:

public static class VideoFeed {
        @Key List<Video> items;
        int totalItems;
      }

      public static class Video {
        @Key String title;
        @Key String description;
        @Key Player player;
        @Key DateTime updated;
      }

      public static class Player {
        @Key("default") String defaultUrl;
      }

      public static class YouTubeUrl extends GenericUrl {
        @Key final String alt = "jsonc";
        @Key String author;
        @Key("max-results") Integer maxResults;

        YouTubeUrl(String url) {
          super(url);
        }
      }

The code is pretty crude but to this point it works, it fails when trying to retrieve the image from the URL.

Does anyone have any insight and/or suggestions on how to best access a video's data (thumbnails etc?) or how in general to go about this problem? Unfortunately youtube access on the Android platform is a seldom spoken of issue.

Many thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深者入戏 2024-12-12 15:54:39

您正在使用 google api 页面中的示例代码。它们仅涵盖 YouTube 帖子的一些基本元素。但是,添加此行后您可以访问更多信息:

url.alt = "jsonc";

这样,YouTube API 将返回 JSON-C 格式。现在,您可以使用缩略图的新类来扩展 VideoFeed 类。我的看起来像这样:

public static class Thumbnail {
    @Key("sqDefault")
    String thumbnail_sq;
    @Key("hqDefault")
    String thumbnail_hq;

    @Override
    public String toString() {
        return thumbnail_sq;
    }
}
  • sqDefault: 120px*90px
  • hqDefault: 480px*360px

现在我只是返回标准质量的缩略图。使用@Key,您可以分配变量,Google Api 将使用这些变量自动将您的请求的 JSON 格式答案解析为 VideoFeed。您可以使用返回的字符串下载图像并将其显示在 ImageView 中。

希望这有帮助:)

You are using the example code from the google api pages. They only cover a few basic elements of a YouTube post. However, you may access much more information when you add this line:

url.alt = "jsonc";

This way the YouTube API will return all requests in JSON-C format. You can now extend the class VideoFeed with a new class for the thumbnails. Mine looks like this:

public static class Thumbnail {
    @Key("sqDefault")
    String thumbnail_sq;
    @Key("hqDefault")
    String thumbnail_hq;

    @Override
    public String toString() {
        return thumbnail_sq;
    }
}
  • sqDefault: 120px*90px
  • hqDefault: 480px*360px

For now I'm just returning the standard quality thumbnail. With @Key you can assign variables that the google Api will use to automatically parse them into VideoFeed from the JSON formatted answer to your request. You can use the returned string to download the image and display it in an ImageView.

Hope this helps :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文