android – 如何使用Facebook Graph Api基于Cursor的分页

我没有找到任何关于这个话题的帮助.文件说

Cursor-based pagination is the most efficient method of paging and should always be used where possible – a cursor refers to a random string of characters which mark a specific item in a list of data. Unless this item is deleted,the cursor will always point to the same part of the list,but it will be invalidated if an item is removed. Therefore,your app shouldn’t store any older cursors or assume that they will still be valid.

When reading an edge that supports cursor pagination,you will see the following JSON response:

{
  "data": [
     ... Endpoint data is here
  ],"paging": {
    "cursors": {
      "after": "MTAxNTExOTQ1MjAwNzI5NDE=","before": "NDMyNzQyODI3OTQw"
    },"previous": "https://graph.facebook.com/me/albums?limit=25&before=NDMyNzQyODI3OTQw"
    "next": "https://graph.facebook.com/me/albums?limit=25&after=MTAxNTExOTQ1MjAwNzI5NDE="
  }
}

我使用这种格式进行api调用,我如何在一个循环中遍历所有页面

/* make the API call */
new GraphRequest(
    session,"/{user-id}/statuses",null,HttpMethod.GET,new GraphRequest.Callback() {
        public void onCompleted(GraphResponse response) {
            /* handle the result */
        }
    }
).executeAsync();

解决方法

我想出了一个很好的方法来通过Facebook图形api页面使用光标分页
final String[] afterString = {""};  // will contain the next page cursor
    final Boolean[] noData = {false};   // stop when there is no after cursor 
    do {
        Bundle params = new Bundle();
        params.putString("after",afterString[0]);
        new GraphRequest(
                accessToken,personId + "/likes",params,new GraphRequest.Callback() {
                    @Override
                    public void onCompleted(GraphResponse graphResponse) {
                        JSONObject jsonObject = graphResponse.getJSONObject(); 
                        try {
                            JSONArray jsonArray = jsonObject.getJSONArray("data");

                            //  your code 


                            if(!jsonObject.isNull("paging")) {
                                JSONObject paging = jsonObject.getJSONObject("paging");
                                JSONObject cursors = paging.getJSONObject("cursors");
                                if (!cursors.isNull("after"))
                                    afterString[0] = cursors.getString("after");
                                else
                                    noData[0] = true;
                            }
                            else
                                noData[0] = true;
                        } catch (JSONException e) {
                            e.printStackTrace(); 
                        }
                    }
                }
        ).executeAndWait();
    }
    while(!noData[0] == true);

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...