利用YouTube数据API基于关键字的Andr​​oid应用程序,以获取视频信息 [英] Making use of Youtube DATA API to retrieve video details in android app based on keyword

查看:325
本文介绍了利用YouTube数据API基于关键字的Andr​​oid应用程序,以获取视频信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经广泛地试图利用YouTube数据API来获取基于关键字的Andr​​oid应用程序的YouTube视频的详细信息,通过下载示例的这里

I have extensively tried to Making use of Youtube DATA API to retrieve youtube video details in android app based on keyword, by downloading the example here.

这导致了大量进口的复杂性,矛盾和重复德兴作为我的问题<一描述href="http://stackoverflow.com/questions/33975264/com-android-dex-dexexception-given-when-trying-to-integrate-youtube-data-api-int/33975672?noredirect=1#comment55711875_33975672">here.

It leads to lot of import complexities,conflicts and duplicate dexing as described in my question here.

有没有一种简单的方法来做到这一点使用Java的Andr​​oid应用程序?

Is there a simpler way to do this using Java for an android app?

推荐答案

是有!

您可以只是做一个简单的GET HTTP请求到YouTube的基础上,他们列出了documenation的此处。这不需要任何谷歌API的JAR文件下载

You can just make a simple GET HTTP request to youtube based on the examples they have listed in the documenation here. This does not require any google api JAR downloads.

code例如:

MakeHTTPRequest makeHTTPRequest = new MakeHTTPRequest();
    String response = "";
    String apiKey = MY_KEY; //Set your Android Key here

    String query = "";
    // input = input.replace(",","+");
    if(input.contains(" "))
        query = "https://www.googleapis.com/youtube/v3/search?part=snippet&q=%22"+input.replace(" ","+")+"%22&type=video&key="+apiKey;
    else
        query = "https://www.googleapis.com/youtube/v3/search?part=snippet&q=%22="+input+"%22&type=video&key="+apiKey;
    try {
        response = makeHTTPRequest.sendGet(query);
    }
    catch (Exception e){
        e.printStackTrace();
    }

    System.out.println("Youtube results : "+response);

    JSONObject responseJSON= null;
    try {
        responseJSON = new JSONObject(response);

        JSONArray items = responseJSON.getJSONArray("items");


        for(int i=0;i<items.length();i++){
            System.out.println("Item "+i+" : ");
            System.out.println("Title : "+items.getJSONObject(i).getJSONObject("snippet").get("title"));
            System.out.println("Description : "+items.getJSONObject(i).getJSONObject("snippet").get("description"));
            System.out.println("URL : https://www.youtube.com/watch?v="+items.getJSONObject(i).getJSONObject("id").getString("videoId"));



        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

其他的东西需要的:

  1. 让您的 API_KEY

您将需要下载的 org.json 罐或JSON解析输出任何其他支持JAR。

You will need to download the org.json jar or any other supporting jar for JSON parsing the output.

您还需要在这里包括code上的如何发送HTTP请求GET / POST在Java中

You also need to include the code here on How to send HTTP request GET/POST in Java.

修改1 :删除videoCaption参数来获得与YouTube网站匹配的结果。

EDIT 1: Remove videoCaption parameter to get matching results with Youtube website.

编辑2 :如果你要搜索的发言权短语的香料船的再投入应该用引号括起来,也就是变量

EDIT 2: If you want to search for say a phrase spice boat then input should be enclosed in quotes, that is, variable

input = "%22spice+boat%22";

那么搜索是忠实地准确。

Then the search is truely accurate.

这篇关于利用YouTube数据API基于关键字的Andr​​oid应用程序,以获取视频信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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