gpt4 book ai didi

java - 利用 Youtube DATA API 根据关键字检索 Android 应用中的视频详细信息

转载 作者:行者123 更新时间:2023-11-30 01:44:03 25 4
gpt4 key购买 nike

我已广泛尝试通过下载示例 here,利用 Youtube DATA API 根据关键字在 android 应用程序中检索 youtube 视频详细信息.

如我的问题 here 中所述,它会导致大量导入复杂性、冲突和重复索引编制.

是否有更简单的方法使用 Java 为 Android 应用程序执行此操作?

最佳答案

是的!

您可以根据文档中列出的示例向 youtube 发出简单的 GET HTTP 请求 here .这不需要任何 google api JAR 下载

代码示例:

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 .

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

  3. 您还需要在 How to send HTTP request GET/POST in Java 上包含此处的代码.

编辑 1:删除 videoCaption 参数以获得与 Youtube 网站匹配的结果。

编辑 2:如果您想搜索一个短语 spice boat 那么输入应该用引号括起来,即变量

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

那么搜索就真的准确了。

关于java - 利用 Youtube DATA API 根据关键字检索 Android 应用中的视频详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33982758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com