如何使用youtube v3 api json响应中的nextPageToken在回收站视图中加载更多视频

时间:2015-07-30 05:05:12

标签: android json youtube android-recyclerview android-youtube-api

我正在开展一个项目,该项目需要使用YouTube数据api v3列出来自频道的所有视频。一切正常。我成功获得了前10个结果。但是当用户向下滚动并达到第10个结果时,我想加载下10个结果,

我的json响应有nextPageToken但我不知道如何在代码中使用nextPageToken来获取我的回收站视图中的下一页视频,请帮帮我..提前谢谢..

json回复:

    {
      "nextPageToken": "CAMQAA",
      "pageInfo": {
        "totalResults": 90,
        "resultsPerPage": 3
      },
      "items": [
        {
          "id": "PLqKOsfcZLLdnWQcjBQs_goXt1XUWyyOo91GYUKZyan9g",
          "snippet": {
            "title": "DevBytes: What's new in Android 5.0 Lollipop",
            "description": "Ankur Kotwal introduces the developer highlights of Android 5.0 Lollipop in this DevByte. These are as follows:\n\n00:14 Material Design - http://google.com/design/spec\n00:54 Enhanced Notifications - http://youtu.be/Uiq2kZ2JHVY\n01:56 WebView updates\n02:22 Project Volta - http://youtu.be/KzSKIpJepUw\n03:03 Android Runtime (ART) - http://source.android.com/devices/tech/dalvik/art.html\n03:21 64-bit support - http://d.android.com/tools/sdk/ndk\n03:49 Concurrent documents on Overview\n04:15 Android Work - http://youtu.be/FbVWtYPpzIs\n04:36 Advanced camera capabilities - http://goo.gl/aXXTJ9\n05:05 Game development - http://youtu.be/qW3Ufg1JNRE\n05:45 Connectivity - http://youtu.be/2fZThdNbHcQ\n06:24 What’s next - http://d.android.com/about/versions/android-5.0.html",
    "thumbnails": {
      "medium": {
        "url": "https://i.ytimg.com/vi/um1S2u022HA/mqdefault.jpg",
        "width": 320,
        "height": 180
      },
      "high": {
        "url": "https://i.ytimg.com/vi/um1S2u022HA/hqdefault.jpg",
        "width": 480,
        "height": 360
      }
    },
    "position": 0,
    "resourceId": {
      "videoId": "um1S2u022HA"
    }
  }
},
{
  "id": "PLqKOsfcZLLdnWQcjBQs_goeQ8gou_NAnSdPTUnh8ONIE",
  "snippet": {
    "title": "DevBytes: Google Play services 6.1",
    "description": "Learn about Google Play services 6.1 in this DevByte from Magnus Hyttsten. Google Play services 6.1 adds Enhanced Ecommerce analytics support from Google Tag Manager and offers new improvements to the Google Drive Android API. With this release, we're also including a refresh of the Google Fit developer preview, so that you can test your fitness apps on any Android device.",
    "thumbnails": {
      "medium": {
        "url": "https://i.ytimg.com/vi/7E-sqJkrlJ0/mqdefault.jpg",
        "width": 320,
        "height": 180
      },
      "high": {
        "url": "https://i.ytimg.com/vi/7E-sqJkrlJ0/hqdefault.jpg",
        "width": 480,
        "height": 360
      }
    },
    "position": 1,
    "resourceId": {
      "videoId": "7E-sqJkrlJ0"
    }
  }
},
{
  "id": "PLqKOsfcZLLdnWQcjBQs_goeKwZG5KaR_D1oPZUTIhFFk",
  "snippet": {
    "title": "DevBytes: Wearable DataLayer API",
    "description": "Wearables are here, and extending your user experience to wear devices is the first step in your wearable strategy. The new DataLayer Apis for Android make it quick and easy to sync events, data, and images between your handheld and wearable device.",
    "thumbnails": {
      "medium": {
        "url": "https://i.ytimg.com/vi/2zReOOEUP18/mqdefault.jpg",
        "width": 320,
        "height": 180
      },
      "high": {
        "url": "https://i.ytimg.com/vi/2zReOOEUP18/hqdefault.jpg",
        "width": 480,
        "height": 360
      }
    },
    "position": 2,
    "resourceId": {
      "videoId": "2zReOOEUP18"
    }
  }
}
]
}

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;
} 

0 个答案:

没有答案