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

前端之家收集整理的这篇文章主要介绍了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);
原文链接:https://www.f2er.com/android/313078.html

猜你在找的Android相关文章