Reputation: 91
I am trying to fetch videos from a channel's playlist. There are 132 videos but I cant fetch more than 50 videos.
I know that nextPageToken has to be fetched and passed along with url? This is my first time working with api.
This is how I am able to fetch 50 videos.
Edit
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;
}
Edit 2:
Added the following but it is not fetching any value.
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();
}
Upvotes: 4
Views: 1290
Reputation: 768
You need to add one extra query parameter pageToken
like below
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"
and when you get data, you need to update NEXT_PAGE_TOKEN
by fetching value from nextPageToken
.
You will get data as
{
"kind": "youtube#searchListResponse",
"etag": "\"RmznBCICv9YtgWaaa_nWDIH1_GM/MgcKz6rwie5hyKKWdwMChcXzNzU\"",
"nextPageToken": "CJYBEAA",
"prevPageToken": "CGQQAQ",
"regionCode": "IN",
"pageInfo": {
"totalResults": 184,
"resultsPerPage": 50
},
"items": [...]
}
So, convert this data into JSONObject
as
JSONObject mainObject = new JSONObject(data);
NEXT_PAGE_TOKEN = mainObject.getString("nextPageToken");
...//Rest your task here
This works fine.
Upvotes: 2