android - 使用播放列表网址获取YouTube视频

标签 android android-emulator youtube youtube-api

如何从http://gdata.youtube.com/feeds/api/playlists/EL5BQGc0nPiVI?v=2&alt=json获取视频列表

我制作了一个程序,可以使用这种URL来获取视频列表:请检查this,但是这次我想使用Android中的播放列表来获取视频列表,例如使用this URL,实际的YouTube视频URL:

在这里,我使用的是JSON,但现在我不知道要使用上述播放列表URL获取视频列表所需做的代码更改。

  @Override
public void run() {
    try {

        HttpClient client = new DefaultHttpClient();

        HttpUriRequest request = new HttpGet("http://gdata.youtube.com/feeds/api/playlists/EL5BQGc0nPiVI?v=2&alt=json"); 





        // Get the response that YouTube sends back
        HttpResponse response = client.execute(request);
        // Convert this response into a readable string
    String jsonString = StreamUtils.convertToString
            (response.getEntity().getContent());
        // Create a JSON object that we can use from the String
        JSONObject json = new JSONObject(jsonString);



        // Get are search result items
    JSONArray jsonArray = json.getJSONObject("data").getJSONArray("items");

        // Create a list to store are videos in
        List<Video> videos = new ArrayList<Video>();

        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            // The title of the video
            String title = jsonObject.getString("title");

            String url;
            try {
    url = jsonObject.getJSONObject("player").getString("default");
    } catch (JSONException ignore) {
    url = jsonObject.getJSONObject("player").getString("default");
            }


            String thumbUrl = jsonObject.getJSONObject("thumbnail").getString("sqDefault");


            videos.add(new Video(title, url, thumbUrl));
        }

        Library lib = new Library(username, videos);

        Bundle data = new Bundle();
        data.putSerializable(LIBRARY, lib);

        Message msg = Message.obtain();
        msg.setData(data);
        replyTo.sendMessage(msg);

    } catch (ClientProtocolException e) {
        Log.e("Feck", e);
    } catch (IOException e) {
        Log.e("Feck", e);
    } catch (JSONException e) {
        Log.e("Feck", e);
    }
}

最佳答案

 String JKYouTubeActivity.YOUTUBE_INFO_URL=http://gdata.youtube.com/feeds/api/playlists/_ID_?v=2&alt=json

 private String getUrl(String id) throws IOException, JSONException {
        HttpClient client = new DefaultHttpClient();
        HttpGet clientGetMethod = new HttpGet(JKYouTubeActivity.YOUTUBE_INFO_URL.replace("_ID_", id));
        HttpResponse clientResponse = null;
        clientResponse = client.execute(clientGetMethod);
        String infoString = _convertStreamToString(clientResponse.getEntity().getContent());
        String urldata=new JSONObject(infoString).getJSONObject("entry").getJSONObject("media$group").getJSONArray("media$content").getJSONObject(0).getString("url");
        return new JSONObject(infoString).getJSONObject("entry").getJSONObject("media$group").getJSONArray("media$content").getJSONObject(0).getString("url");
    }

    private String _convertStreamToString(InputStream iS) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(iS));
        StringBuilder sB = new StringBuilder();
        String line = null;
        try {
            while ((line = reader.readLine()) != null)
            {
                sB.append(line).append("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                iS.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sB.toString();
    }

}
获取url数据后,您可以流式传输它或做任何您想做的事情。同样,您也可以获取缩略图和标题

关于android - 使用播放列表网址获取YouTube视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14099817/

相关文章:

android - 模拟器 : ERROR: the user data image is used by another emulator. 正在中止

firefox - 访问Firefox扩展/附加组件的脚本

javascript - 隐藏弹出窗口时暂停/停止 YouTube 视频

java - SwipeRefreshLayout + AsyncTask : Fail to refresh data

Android 语言翻译错误

android - 如果使用主机名而不是 IP 地址,Android 应用程序将抛出 UnknownHostException

android - 当我尝试安装 HAXM 时正在创建两个文件夹

javascript - Google + YouTube JS API 阻止框架访问跨源框架

android - PendingIntent 不适用于 FCM 通知

android - 如果我们手动强制停止应用程序,推送通知将不起作用