如何使用Facebook Graph Api基于光标的分页

Web*_*ent 13 pagination android facebook-graph-api

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

基于游标的分页是最有效的分页方法,应尽可能使用 - 游标是指随机字符串,用于标记数据列表中的特定项目.除非删除此项,否则光标将始终指向列表的相同部分,但如果删除项,则无效.因此,您的应用不应存储任何旧游标或假设它们仍然有效.

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="
  }
}
Run Code Online (Sandbox Code Playgroud)

我正在使用这种格式进行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();
Run Code Online (Sandbox Code Playgroud)

Web*_*ent 12

我想出了一个使用光标分页遍历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,
                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);
Run Code Online (Sandbox Code Playgroud)


김준호*_*김준호 10

不要重新发明轮子.

GraphResponse类已经有了一种方便的分页方法.GraphResponse.getRequestForPagedResults()返回 GraphRequest对象,您可以使用该对象进行分页.

我还从facebook-android-sdk的单元测试代码中找到了代码片段.

GraphRequest nextRequest = response.getRequestForPagedResults(GraphResponse.PagingDirection.NEXT);
nextRequest.setCallback(request.getCallback());
response = nextRequest.executeAndWait();
Run Code Online (Sandbox Code Playgroud)


Rom*_*naV 5

尽管你应该使用它GraphResponse.getRequestForPagedResults(),但executeAndWait()除非你在另一个线程中运行它,否则你不能使用它.

您可以使用它更容易executeAsync().

获得第一组结果:

    new GraphRequest(AccessToken.getCurrentAccessToken(),
            "/" + facebookID + "/invitable_friends",
            null,
            HttpMethod.GET,
            new GraphRequest.Callback() {
                public void onCompleted(GraphResponse response) {
                    //your code

                    //save the last GraphResponse you received
                    lastGraphResponse = response;
                }
            }
    ).executeAsync();
Run Code Online (Sandbox Code Playgroud)

使用lastGraphResponse 获取下一组结果:

    GraphRequest nextResultsRequests = lastGraphResponse.getRequestForPagedResults(GraphResponse.PagingDirection.NEXT);
    if (nextResultsRequests != null) {
        nextResultsRequests.setCallback(new GraphRequest.Callback() {
            @Override
            public void onCompleted(GraphResponse response) {
                //your code

                //save the last GraphResponse you received
                lastGraphResponse = response;
            }
        });
        nextResultsRequests.executeAsync();
    }
Run Code Online (Sandbox Code Playgroud)

您可以在一个方法中合并所有这些!