How to use the Facebook Graph Api Cursor-based Pagination

后端 未结 5 2326
南旧
南旧 2021-02-13 10:15

I didn\'t find any help on this topic. The Docs say

Cursor-based pagination is the most efficient method of paging and should always be used where possibl

5条回答
  •  花落未央
    2021-02-13 10:54

    I figured out a good way to traverse through facebook graph api pages using cursor pagination

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

提交回复
热议问题