java - 如何使用 android 版 YouTube API 搜索主题来找到 YouTube 上视频的链接?

标签 java android eclipse android-listview

我知道如何使用 Android youtube API 使用视频 id 获取图像的缩略图,但是如何使用视频标题查找视频的缩略图?

最佳答案

要解析 YouTube 搜索视频 Json Feed,请使用此链接:
https://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&q=my+love+by+shovon

这里搜索的是“my love by shovon”。

使用任何 json 解析器解析它并获取搜索项目的 YouTube 视频。使用此 Json 解析器。

public JSONObject getJson()
{
    DefaultHttpClient   httpclient = new DefaultHttpClient(new BasicHttpParams());
    HttpPost httppost = new HttpPost("https://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&q=my%20love%20by%20shovon");
    // Depends on your web service
    httppost.setHeader("Content-type", "application/json");

    InputStream inputStream = null;
    String result = null;
    HttpResponse response = null;
    try {
        response = httpclient.execute(httppost);
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }           
    HttpEntity entity = response.getEntity();

    try {
        inputStream = entity.getContent();
    } catch (IllegalStateException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    // json is UTF-8 by default i beleive
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null)
        {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    result = sb.toString();
    JSONObject job = null;
    try {
        job = new JSONObject(sb.toString());
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return job;
}

关于java - 如何使用 android 版 YouTube API 搜索主题来找到 YouTube 上视频的链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17675378/

相关文章:

java - 连接两台计算机(通过无线网络的机器)

java - ArrayIndexOutOfBoundsException : length=20; index=20 when select item from spinner

android - 使用 sqlite ORDERBY 字段

java - 如何在我的项目(如 android 文档)中使用我的文档注释?

java - 将请求从 servlet 重定向到 jsp 时出现 404 错误

java - Google 推送通知 java 处理程序

Java HttpURLConnection 避免重定向并转到原始页面

安卓 : Difference between action id and fragment id in navigation component

java - 使用 Eclipse 类路径变量替换绝对路径 "sourcepath"?

java - 使用 static final 声明的数组作为 Java 注释的参数时可能存在错误?