我正在构建一个应用程序,其中我有一个回收站的看法,其中有我的YouTube频道视频。一切正常。我成功地得到了前10个结果。但现在当用户向下滚动并达到第10个结果时,我想加载下10个结果,
我的json响应有nextPagetOken,但我不知道如何使用nextPagetOken在回收器视图中加载下一页视频,
有什么帮助吗?
https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=10&playlistId=PLWz5rJ2EKKc_XOgcRukSoKKjewFJZrKV0&fields=nextPageToken,items(id,snippet(title,description,position,thumbnails(medium,high),resourceId/videoId))&key=API_KEY;
JSON解析代码:
private ArrayList<VideoData> parseJSONReaponse(JSONObject response) {
ArrayList<VideoData> listVideos = new ArrayList<>();
if (response != null || response.length() > 0) {
String nextPageToken = Constants.NA;
try {
if (response.has(KEY_NEXTPAGETOKEN)
&& !response.isNull(KEY_NEXTPAGETOKEN) ){
nextPageToken = response.getString(KEY_NEXTPAGETOKEN);
}
JSONArray arrayItems = response.getJSONArray(KEY_ITEMS);
for (int i = 0; i < arrayItems.length(); i++) {
String title = Constants.NA;
String urlThumbnail = Constants.NA;
String resourceID = Constants.NA;
JSONObject currentItems = arrayItems.getJSONObject(i);
if (currentItems.has(KEY_SNIPPET)
&& !currentItems.isNull(KEY_SNIPPET)) {
JSONObject objectSnippet = currentItems.getJSONObject(KEY_SNIPPET);
if (objectSnippet.has(KEY_TITLE)
&& !objectSnippet.isNull(KEY_TITLE)) {
title = objectSnippet.getString(KEY_TITLE);
}
if (objectSnippet.has(KEY_THUMBNAIL)
&& !objectSnippet.isNull(KEY_THUMBNAIL)) {
JSONObject objectThumbnail = objectSnippet.getJSONObject(KEY_THUMBNAIL);
if (objectThumbnail.has(KEY_THUMBNAIL_MEDIUM)
&& !objectThumbnail.isNull(KEY_THUMBNAIL_MEDIUM)) {
JSONObject objectThumbnailMedium = objectThumbnail.getJSONObject(KEY_THUMBNAIL_MEDIUM);
if (objectThumbnailMedium.has(KEY_THUMBNAIL_MEDIUM_URL)
&& !objectThumbnailMedium.isNull(KEY_THUMBNAIL_MEDIUM_URL)) {
urlThumbnail = objectThumbnailMedium.getString(KEY_THUMBNAIL_MEDIUM_URL);
}
}
}
if (objectSnippet.has(KEY_RESOURCEID)
&& !objectSnippet.isNull(KEY_RESOURCEID)) {
JSONObject objectResourceId = objectSnippet.getJSONObject(KEY_RESOURCEID);
if (objectResourceId.has(KEY_VIDEOID)
&& !objectResourceId.isNull(KEY_VIDEOID)) {
resourceID = objectResourceId.getString(KEY_VIDEOID);
}
}
}
VideoData videoData = new VideoData();
videoData.setTitle(title);
videoData.setUrlThumbnail(urlThumbnail);
videoData.setVideoId(resourceID);
if (!title.equals(Constants.NA)){
listVideos.add(videoData);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return listVideos;
}
最佳答案
您可以尝试发出相同的请求,但需要额外的参数pageToken=<the-nextPageToken-you-received>