YouTube Api播放列表视频限制为50.如何获取更多内容?

时间:2018-04-06 13:17:45

标签: android json youtube-api

我正在尝试从频道的播放列表中提取视频。有132个视频,但我无法获取超过50个视频。

我知道必须获取nextPageToken并与url一起传递? 这是我第一次使用api。

这就是我能够获取50个视频的方式。

修改

private static String GOOGLE_YOUTUBE_API_KEY = "<API Key>";
     private static String CHANNLE_GET_URL="https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=PL-nbe4FPvDBElyW0Iww5suxJqqmuGBgIH&key="+GOOGLE_YOUTUBE_API_KEY;

public ArrayList<YoutubeDataModel> parseVideoListFromResponse(JSONObject jsonObject) {
    ArrayList<YoutubeDataModel> mList = new ArrayList<>();

    if (jsonObject.has("items")) {
        try {
            JSONArray jsonArray = jsonObject.getJSONArray("items");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject json = jsonArray.getJSONObject(i);
                if (json.has("id")) {
                    JSONObject jsonID = json.getJSONObject("id");
                    String video_id = "";
                    if (jsonID.has("videoId")) {
                        video_id = jsonID.getString("videoId");
                    }
                    if (jsonID.has("kind")) {
                        if (jsonID.getString("kind").equals("youtube#video")) {
                            YoutubeDataModel youtubeObject = new YoutubeDataModel();
                            JSONObject jsonSnippet = json.getJSONObject("snippet");
                            String title = jsonSnippet.getString("title");
                            String description = jsonSnippet.getString("description");
                            String publishedAt = jsonSnippet.getString("publishedAt");
                            String thumbnail = jsonSnippet.getJSONObject("thumbnails").getJSONObject("high").getString("url");

                            youtubeObject.setTitle(title);
                            youtubeObject.setDescription(description);
                            youtubeObject.setPublishedAt(publishedAt);
                            youtubeObject.setThumbnail(thumbnail);
                            youtubeObject.setVideo_id(video_id);
                            mList.add(youtubeObject);
                        }
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    return mList;
}

编辑2:

添加了以下内容,但未获取任何值。

try {
   // String data=null;
    JSONObject reader = new JSONObject();
  String next_Page_Token = reader.getString("nextPageToken");
    Log.d("NextPAgeToken", "NpT"+next_Page_Token);
}
catch (JSONException e)
{
    Log.e("Error", "Error is "+e.getMessage());
    Toast.makeText(getApplicationContext(),"No next page found", Toast.LENGTH_LONG).show();
}

1 个答案:

答案 0 :(得分:0)

您需要添加一个额外的查询参数pageToken,如下所示

private static String CHANNLE_GET_URL = 
    "https://www.googleapis.com/youtube/v3/playlistItems" + 
        "?part=snippet" +
        "&maxResults=50" +
        "&playlistId=PL-nbe4FPvDBElyW0Iww5suxJqqmuGBgIH" + 
        "&key="+GOOGLE_YOUTUBE_API_KEY +
        "pageToken=" + NEXT_PAGE_TOKEN;

//Pass like this one
"https://www.googleapis.com/youtube/v3/search?part=snippet&order=date&channelId=UC2LrGJYe_uzI3FBj05BuvLA&key=AIzaSyBlj1dJ9txGcXOOblCJuQ0iwIkhUCgVt1Y&maxResults=50&pageToken=CJYBEAA"

当您获得数据时,您需要通过从NEXT_PAGE_TOKEN获取值来更新nextPageToken

您将获得数据

{
    "kind": "youtube#searchListResponse",
    "etag": "\"RmznBCICv9YtgWaaa_nWDIH1_GM/MgcKz6rwie5hyKKWdwMChcXzNzU\"",
    "nextPageToken": "CJYBEAA",
    "prevPageToken": "CGQQAQ",
    "regionCode": "IN",
    "pageInfo": {
        "totalResults": 184,
        "resultsPerPage": 50
},
    "items": [...]
}

因此,请将此数据转换为JSONObject

JSONObject mainObject = new JSONObject(data);
NEXT_PAGE_TOKEN = mainObject.getString("nextPageToken");
...//Rest your task here

这很好用。