Facebook graph api cursor based paging not working in android

主宰稳场 提交于 2019-12-23 22:52:11

问题


I am trying to get all pages url liked by user. I'm able to fetch records using below method, but paging not working. I'm getting same data every time with same before - after cursor token lead to infinite loop. Limit params also not working.

Request:

final String[] afterString = {""};  // will contain the next page cursor
final Boolean[] noData = {false};   // stop when there is no after cursor

            do {

                GraphRequest request = GraphRequest.newMeRequest(
                        AccessToken.getCurrentAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject object, GraphResponse response) {
                                JSONObject jsonObject = response.getJSONObject();
                                Log.e("cursor data", jsonObject.toString());


                                try {
                                    JSONObject childObject = jsonObject.getJSONObject("likes");
                                    JSONArray jsonArray = childObject.getJSONArray("data");

                                    Log.e("cursor data", jsonArray.toString());
                                    //  your code


                                    if (!childObject.isNull("paging")) {
                                        JSONObject paging = childObject.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();
                                }
                            }
                        });

                Bundle parameters = new Bundle();
                parameters.putString("fields", "likes{link}");
                parameters.putString("after", afterString[0]);
                parameters.putString("limit", "20");
                request.setParameters(parameters);
                request.executeAndWait();

            }
            while (!noData[0] == true);

Response:

{
  "likes": {
    "data": [
      {
        "link": "https:\/\/www.facebook.com\/hackingworld27\/",
        "id": "206659129366770"
      },
      {
        "link": "https:\/\/www.facebook.com\/getBksy\/",
        "id": "259965487527431"
      },
      {
        "link": "https:\/\/www.facebook.com\/festivallapp\/",
        "id": "1688234858119600"
      },
      {
        "link": "https:\/\/www.facebook.com\/FestivalBooks-967208323354910\/",
        "id": "967208323354910"
      },
      {
        "link": "https:\/\/www.facebook.com\/subhamfashion\/",
        "id": "1454168668186632"
      },

    ],
    "paging": {
      "cursors": {
        "before": "MjA2NjU5MTI5MzY2Nzcw",
        "after": "MTEyMTgwMjU1NDg2MTI0"
      }
    }
  },
  "id": "858224610968939"
}

Above response is sample response. I tried to send request by user id who has liked so many pages, but getting same 25 records maximum every time.

来源:https://stackoverflow.com/questions/45319372/facebook-graph-api-cursor-based-paging-not-working-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!