Ad

Getting Likes Data (id And Name) From JSONArray - Facebook Api

- 1 answer

i'm trying to get id and name from likes data in the facebook graph api on android:

{
   "data": [
      {
         "id": "1433908706707486_917483885016640"
      },
      {
         "id": "1433908706707486_910831695681859"
      },
      {
         "id": "1433908706707486_906550586109970",
         "likes": {
            "data": [
               {
                  "id": "xxxx",
                  "name": "xxx"
               }
            ],
            "paging": {
               "cursors": {
                  "before": "MTEwNTU0NTg3OTQ2NjE3NgZDZD",
                  "after": "MTEwNTU0NTg3OTQ2NjE3NgZDZD"
               }
            }
         }
      },
      {
         "id": "1433908706707486_902931413138554",
         "likes": {
            "data": [
               {
                  "id": "xxx",
                  "name": "Muhammad Waseem"
               }
            ],
            "paging": {
               "cursors": {
                  "before": "NzI3NTMxNjA0MDA0MDA5",
                  "after": "NzI3NTMxNjA0MDA0MDA5"
               }
            }
         }
      },
      {
         "id": "1433908706707486_134266116671758"
      }
   ],
   "paging": {
      "previous": "https://graph.faceb....",
      "next": "https://graph.faceb...
   }
}

here is the problem:

12-27 14:56:45.542 21352-21352/com.facebook.pv2 W/System.err: org.json.JSONException: Unterminated object at character 25 of {Response:  responseCode: 200, graphObject: {"data":[{"id":"1781423402174843_1912187512431764"},{"id":"1781423402174843_1906871369630045","likes":{"data":[{"id":"xxx","name":"Umer Hameed"}],"paging":{"cursors":{"after":"NjM5NTg5OTE5NTY0Njk0","before":"NjM5NTg5OTE5NTY0Njk0"}}}},{"id":"1781423402174843_1905874473063068"},{"id":"1781423402174843_1904892806494568","likes":{"data":[{"id":"xxx","name":"Shafiqe Ali"},{"id":"xxx","name":"Ahmad Chauhan"},{"id":"xxx","name":"Umer Hameed"},{"id":"xxx","name":"Arslan Mughal"},{"id":"xxxx","name":"Zahid Rachid"}],"paging":{"cursors":{"after":"NDQ1NTMxMzQ5MTEzNjc0","before":"MTEzNTM5MDgzMzIzOTc4NgZDZD"}}}},{"id":"1781423402174843_1904460023204513","likes":{"data":[{"id":"xxx","name":"Waqas Sunny"},{"id":"xxxx","name":"Arslan Mughal"},{"id":"xxxx","name":"Ahmad Chauhan"}],"paging":{"cursors":{"after":"MTAzMzM2MTg0MzQ2MTk2NwZDZD","before":"Nzc2NDM4NjYyNTEzNDc4"}}}},{"id":"1781423402174843_1904368313213684","likes":{"data":[{"id":"xxxx","name":"Ahmad Chauhan"}],"paging":{"cursors":{"after":"MTAzMzM2MTg0MzQ2MTk2NwZDZD","before":"MTAzMzM2MTg0MzQ2MTk2NwZDZD"}}}},{"id":"1781423402174843_1883311385319377","likes":{"data":[{"id":"1033361843461967","name":"Ahmad Chauhan"},{"id":"639589919564694","name":"Umer Hameed"}],"paging":{"cursors":{"after":"NjM5NTg5OTE5NTY0Njk0","before":"MTAzMzM2MTg0MzQ2MTk2NwZDZD"}}}},{"id":"1781423402174843_1852042315112951","likes":{"data":[{"id":"1263530067099941","name":"Arslan Mughal"},{"id":"1135390833239786","name":"Shafiqe Ali"},{"id":"1033361843461967","name":"Ahmad Chauhan"}],"paging":{"cursors":{"after":"MTAzMzM2MTg0MzQ2MTk2NwZDZD","before":"MTI2MzUzMDA2NzA5OTk0MQZDZD"}}}},{"id":"1781423402174843_1850409191942930","likes":{"data":[{"id":"xxx","name":"Nasir Mughal"},{"id":"xxxx","name":"Shafiqe Ali"},{"id":"xxxx","name":"Taha Sheikh"},{"id":"xxxx","name":"Waqas Sunny"},{"id":"xxxx","name":"Arslan Mughal"},{"id":"xxxx","name":"Zahid Rachid"}],"paging":{"cursors":{"after":"NDQ1NTMxMzQ5MTEzNjc0","before":"MTA4NTMyMzEzODIzOTYxNAZDZD"}}}},{"id":"1781423402174843_1849173595399823","likes":{"data":[{"id":"xxxx","name":"Umer Hameed"},{"id":"xxxx","name":"Waqas Sunny"},{"id":"xxxx","name":"Shafiqe Ali"},{"id":"xxxx","name":"Arslan Mughal"}],"paging":{"cursors":{"after":"MTI2MzUzMDA2NzA5OTk0MQZDZD","before":"NjM5NTg5OTE5NTY0Njk0"}}}},{"id":"1781423402174843_1847584705558712"},{"id":"1781423402174843_1845844379066078","likes":{"data":[{"id":"445531349113674","name":"Zahid Rachid"}],"paging":{"cursors":{"after":"NDQ1NTMxMzQ5MTEzNjc0","before":"NDQ1NTMxMzQ5MTEzNjc0"}}}},{"id":"1781423402174843_1837370286580154","likes":{"data":[{"id":"1135390833239786","name":"Shafiqe Ali"},{"id":"455768698087604","name":"Usama Ishaq"}],"paging":{"cursors":{"after":"NDU1NzY4Njk4MDg3NjA0","before":"MTEzNTM5MDgzMzIzOTc4NgZDZD"}}}},{"id":"1781423402174843_1832619910388525"},{"id":"1781423402174843_1828230240827492"},{"id":"1781423402174843_1828176680832848"},{"id":"1781423402174843_1820949544888895"},{"id":"1781423402174843_1807792559537927","likes":{"data":[{"id":"1120628128049019","name":"Sheheryar Ali Mehar"},{"id":"1033361843461967","name":"Ahmad Chauhan"},{"id":"776438662513478","name":"Waqas Sunny"},{"id":"445531349113674","name":"Zahid Rachid"}],"paging":{"cursors":{"after":"NDQ1NTMxMzQ5MTEzNjc0","before":"MTEyMDYyODEyODA0OTAxOQZDZD"}}}},{"id":"1781423402174843_1790316361285547"},{"id":"1781423402174843_1787495658234284"},{"id":"1781423402174843_1780168658966984","likes":{"data":[{"id":"xxxx","name":"Tahir Farooq"},{"id":"xxxx","name":"Shafiqe Ali"},{"id":"xxxx","name":"Arslan Mughal"}],"paging":{"cursors":{"after":"MTI2MzUzMDA2NzA5OTk0MQZDZD","before":"MTAyMTA4NTE2NzY3NTk1MTEZD"}}}},{"id":"1781423402174843_1775850492732

I have approved user_posts permission from facebook and my code gets user's wall posts only, please help megetting the id and names who likes my post. thanks

Ad

Answer

Try this

try {

        JSONObject resObject = new JSONObject("your response");
        JSONArray jsonArray = resObject.getJSONArray("data");

        for (int i = 0; i < jsonArray.length(); i++) {

            JSONObject jsonObject = jsonArray.getJSONObject(i);

            Log.i("ID", "-->" + jsonObject.getString("id"));
            Log.i("Likes", "-->" + jsonObject.getString("likes"));

            JSONObject jsonObjectlikes = jsonObject.getJSONObject("likes");

            JSONArray jsonArrayLikeData = jsonObjectlikes.getJSONArray("data");

            for (int j = 0; j < jsonArrayLikeData.length(); j++) {

                JSONObject jsonObjectlike = jsonArrayLikeData.getJSONObject(i);
                Log.i("Like ID", "-->" + jsonObjectlike.getString("id"));
                Log.i("Like Name", "-->" + jsonObjectlike.getString("name"));
            }


        }


    }
    catch (JSONException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

EDIT

new GraphRequest(
        AccessToken.getCurrentAccessToken(),
"/me/posts",
parameters,
HttpMethod.GET,
        new GraphRequest.Callback() {
    public void onCompleted(GraphResponse response) {
    /* handle the result */
        //Log.v("TAG", "Facebook Photos response: " + response);

        try {
            if (response.getError() == null) {


                JSONObject resObject = new JSONObject(response);
                JSONArray jsonArray = resObject.getJSONArray("data");

                for (int i = 0; i < jsonArray.length(); i++) {

                    JSONObject jsonObject = jsonArray.getJSONObject(i);

                    Log.i("ID", "-->" + jsonObject.getString("id"));
                    Log.i("Likes", "-->" + jsonObject.getString("likes"));

                    JSONObject jsonObjectlikes = jsonObject.getJSONObject("likes");

                    JSONArray jsonArrayLikeData = jsonObjectlikes.getJSONArray("data");

                    for (int j = 0; j < jsonArrayLikeData.length(); j++) {

                        JSONObject jsonObjectlike = jsonArrayLikeData.getJSONObject(i);
                        Log.i("Like ID", "-->" + jsonObjectlike.getString("id"));
                        Log.i("Like Name", "-->" + jsonObjectlike.getString("name"));
                    }


                }

                //set your adapter here

            } else {
                Log.v("TAG", response.getError().toString());
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}).executeAsync();

EDIT NEW

try 
{
    JSONObject resObject = new JSONObject("your response");
    JSONArray jsonArray = resObject.getJSONArray("data");


        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);

            Log.i("ID", "-->" + jsonObject.getString("id"));

            if(jsonArray.getJSONObject(i).has("likes"))
            {

                JSONObject jsonObjectlikes = jsonObject.getJSONObject("likes");

                JSONArray jsonArrayLikeData = jsonObjectlikes.getJSONArray("data");

                for (int j = 0; j < jsonArrayLikeData.length(); j++) {

                    JSONObject jsonObjectlike = jsonArrayLikeData.getJSONObject(j);

                    Log.i("Like ID", "-->" + jsonObjectlike.getString("id"));
                    Log.i("Like Name", "-->" + jsonObjectlike.getString("name"));
                }

            }



        }
}
catch (JSONException e)
{
     // TODO Auto-generated catch block
     e.printStackTrace();
}

OUTPUT

enter image description here

Ad
source: stackoverflow.com
Ad