android - 如何使用 Facebook Graph Api 基于游标的分页

标签 android facebook-graph-api pagination

我没有找到关于这个主题的任何帮助。文档说

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 graph 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,
                HttpMethod.GET,
                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 - 如何使用 Facebook Graph Api 基于游标的分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29902306/

相关文章:

android - Facebook 登录回调 session 始终关闭

angularjs - 限制不工作

android - 在 ListView 中启用选择

android - 在 api 27、28、29 中保护应用程序时,工作管理器不运行

android - 如何在 Android 的 webView 中加载 img 标签?

ios - Facebook Graph API v2.1 升级通知-iOS

android - 如何禁用安卓的触摸屏设备?

javascript - 使用 Facebook API 更改 Facebook 应用程序的基本设置

sql - 关于PostgreSQL性能的两个问题

jquery - Rails 中的服务器端数据表