java - (Java、JSON)使用 Youtube API 从包含已删除项目的播放列表中获取数据

标签 java android json youtube youtube-api

我在 SO 上搜索了很多主题,并且取得了很大的进展。但现在我遇到了一个问题,关于如何正确地从这个 URL 读取数据: http://gdata.youtube.com/feeds/api/playlists/PLsksxTH4pR3KtrWWeupRy5h0Di7N_MufB?v=2&alt=jsonc&max-results=50

在位置 22 中,有一个视频被 Youtube 拒绝并因此被屏蔽,因此无法观看。我现在遇到的问题是,我无法成功跳过该条目并继续下一个条目。即使上述 URL 中有 50 个条目,我的应用程序也只会显示 21 个条目,即停止在被阻止的视频所在的位置。

我尝试使用该视频的 JsonObject“状态”并检查它是否具有“拒绝”值,但我无法做到这一点。

欢迎任何帮助,提前致谢:)

这是我的代码:

ArrayList<String> msg = new ArrayList<String>();
ArrayList<String> title = new ArrayList<String>();
ArrayList<Bitmap> thumb = new ArrayList<Bitmap>();

public void getData()
{
    HttpClient httpclient = new DefaultHttpClient();
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    HttpGet request = new HttpGet("http://gdata.youtube.com/feeds/api/playlists/PLsksxTH4pR3KtrWWeupRy5h0Di7N_MufB?v=2&alt=jsonc&max-results=50");

    try
    {
        HttpResponse response = httpclient.execute(request);
        HttpEntity resEntity = response.getEntity();
        String _response= EntityUtils.toString(resEntity); // content will be consume only once

        JSONObject json = new JSONObject(_response);

        JSONArray jsonArray = json.getJSONObject("data").getJSONArray("items");
        for (int i = 0; i < jsonArray.length(); i++) {

            JSONObject jsonObject = jsonArray.getJSONObject(i).getJSONObject("video");

            String title1 = jsonObject.getString("title");
            title.add(title1);

            String thumbUrl = jsonObject.getJSONObject("thumbnail").getString("sqDefault");
            URL url1 = new URL(thumbUrl);
            Bitmap bmp = BitmapFactory.decodeStream(url1.openConnection().getInputStream());
            thumb.add(bmp);
            String url;
            try {

                url = jsonObject.getJSONObject("player").getString("default");
                msg.add(url);
            } catch (JSONException ignore) {
            }
        }
    }
    catch(Exception e1)
    {
        e1.printStackTrace();
    }

    httpclient.getConnectionManager().shutdown();
}

最佳答案

I tried to use the JsonObject "status" of that video and checking if it has the value "rejected", but I couldn't do it.

items JSONArray 获取所有项目,而不包含被阻止的项目。使用JSONObject.has在从 video JSONObject.like 访问其他键之前检查 status 对象是否可用的方法:

JSONObject jsonObject = jsonArray.getJSONObject(i).getJSONObject("video");
if(jsonObject.has("status")){
  // ignore video object

}else{
  //... get other thumbnail, player objects
}

关于java - (Java、JSON)使用 Youtube API 从包含已删除项目的播放列表中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29507095/

相关文章:

java - Eclipse 构建路径 - 声明的包不匹配

android - fragment 在 Activity 轮换上重叠

android - 我可以在我的 android 应用程序中禁用 systemui 吗?

ios - 在 iOS 中获取并解析 JSON

javascript - 单击按钮后获取当前用户

java - 无法禁用 java 中 logback.xml 中的调试级别

android - 按下后设置密码

java - 如何在android中获取jsonObject值的值?

java - 使用Gson解析动态JSON数据

java - 单击超链接调用 servlet