How to use the Facebook Graph Api Cursor-based Pagination

后端 未结 5 2329
南旧
南旧 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:33

    I use this code:

    final String[] afterString = {""}; 
    final Boolean[] noData = {true};
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    
    do{
       GraphRequest request = GraphRequest.newGraphPathRequest(
                AccessToken.getCurrentAccessToken(),
                "/me/likes",
                new GraphRequest.Callback() {
                    @Override
                    public void onCompleted(GraphResponse response) {
                        // Insert your code here
                        JSONObject jsonObject = response.getJSONObject();
                        try{
                            if(jsonObject.length() > 1) {
    
                                JSONObject jsonFacebook = (JSONObject) new JSONTokener(jsonObject.toString()).nextValue();
                                JSONObject likes_paging = (JSONObject) new JSONTokener(jsonFacebook.getJSONObject("paging").toString()).nextValue();
    
                                ArrayList likes = new ArrayList();
                                for (int i = 0; i < jsonFacebook.getJSONArray("data").length(); i++) {
                                    likes.add(jsonFacebook.getJSONArray("data").getJSONObject(i).getString("name"));
                                }
                                afterString[0] = (String) likes_paging.getJSONObject("cursors").get("after");
    
                            }else{
                                noData[0] = false;
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    
        Bundle parameters = new Bundle();
        parameters.putString("pretty", "0");
        parameters.putString("limit", "100");
        parameters.putString("after", afterString[0]);
        request.setParameters(parameters);
        request.executeAndWait();
    }while(noData[0] == true);
    

提交回复
热议问题