【问题标题】:How to search videos with youtube data API in Android如何在 Android 中使用 youtube 数据 API 搜索视频
【发布时间】:2014-03-21 18:30:49
【问题描述】:

我正在开发一个需要通过关键字搜索 YouTube 视频的 Android 应用程序。我使用了 Youtube 数据 API,代码如下:

    try {
        youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, new HttpRequestInitializer() {
            public void initialize(HttpRequest request) throws IOException {
            }
        }).setApplicationName("YoutubeQoE").build();

        // Define the API request for retrieving search results.
        YouTube.Search.List search = youtube.search().list("id,snippet");

        // Set your developer key from the Google Cloud Console for
        // non-authenticated requests. See:
        // https://cloud.google.com/console
        search.setKey(DeveloperKey.DEVELOPER_KEY);
        search.setQ("dogs");

        // Restrict the search results to only include videos. See:
        // https://developers.google.com/youtube/v3/docs/search/list#type
        search.setType("video");

        // To increase efficiency, only retrieve the fields that the
        // application uses.
        //search.setFields("items(id/kind,id/videoId,snippet/title,snippet/thumbnails/default/url)");
        search.setMaxResults(25);

        SearchListResponse searchResponse = search.execute();
        List<SearchResult> lista = searchResponse.getItems();

    } catch (GoogleJsonResponseException e) {
        System.err.println("There was a service error: " + e.getDetails().getCode() + " : "
                + e.getDetails().getMessage());
    } catch (IOException e) {
        System.err.println("There was an IO error: " + e.getCause() + " : " + e.getMessage());
    } catch (Throwable t) {
        t.printStackTrace();
    }

对于 de DEVELOPER_KEY,我在 google 开发者控制台中使用了公共 API 访问权限。

但是当我执行程序时,一行出现了问题:

SearchListResponse searchResponse = search.execute();

在 android manifest.xml 我有这些权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

如果有人可以帮助我,我将非常感激

【问题讨论】:

  • 您遇到了什么错误?
  • 亲爱的 Josesvm,我正在做和你一样的尝试。但是我无法获得支持这个搜索 java 类文件的库。所以请分享 jar 文件或 jar 文件的位置。

标签: android youtube youtube-api android-youtube-api


【解决方案1】:

您可以将您的项目与YouTube Direct Lite for Android 进行比较。是的,不用 OAuth2,设置 API 密钥就足够了。

【讨论】:

  • 非常感谢。在您的帮助下,我找到了问题所在。虽然我在 android 应用程序中使用它,但我应该使用 Web 浏览器的 API 密钥。
  • 你有其他例子还是只有 youtube Direct lite
【解决方案2】:

实现此目的的另一种方法是从中获得结果:

https://www.googleapis.com/youtube/v3/search?part=snippet&q=eminem&type=video&key=<key>

你可以使用参数来获得你真正需要的东西。您还需要来自https://console.developers.google.com/ 的 API 密钥。

【讨论】:

  • 您能否解释一下。我正在尝试通过使用 google youtube api v3 输入搜索关键字来获取视频。所以我使用的是 google developers.google.com/youtube/v3/code_samples/… 提供的示例代码。但我无法获得支持这个示例类的jar文件。所以请告诉建议。
【解决方案3】:

通常,当有人尝试在 UI Thread 上执行网络调用时会发生此错误,这在 Android 中是不允许的。 如果您收到此错误,请检查 logcat - (error-android-os-networkonmainthreadexception) 通过这个答案来解决这个问题 - fix of neworkonmainthreadexception

【讨论】:

    猜你喜欢
    • 2018-01-28
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    • 2013-03-20
    • 2012-12-10
    • 1970-01-01
    相关资源
    最近更新 更多