obo
obo

Reputation: 1662

Facebook android sdk how to get the number of likes for a given page

I would like to get the number of likes for a given page with its id.

Here is the graph api request I would like to translate with the facebook-android-sdk.

I cannot figure how to make it. I get errors if I try :

Bundle params = new Bundle();
params.putString("fields", "likes");
Request request = new Request(session,"104249301622", params, HttpMethod.GET, new Request.Callback() {  
    @Override
    public void onCompleted(Response response) {
        JSONObject res = response.getGraphObject().getInnerJSONObject();
        System.out.println(res);
    }
});

Upvotes: 3

Views: 3466

Answers (2)

R. Campos
R. Campos

Reputation: 957

In facebook sdk 3.8

Bundle b = new Bundle();
    b.putBoolean("summary", true);


    new Request(
            session,
            id + "/likes",
            b,
            HttpMethod.GET,
            new Request.Callback() {
                public void onCompleted(Response response) {
                                    String s=response.toString();                   
                                 }
            }
    ).executeAsync();

And you will hace the field "total_count" in the Json response.

Upvotes: 1

obo
obo

Reputation: 1662

Here is the method to call :

Session session = Session.getActiveSession();
Bundle params = new Bundle();
params.putString("id", "104249301622");
params.putString("fields", "likes");
Request request = new Request(session, "search", params, HttpMethod.GET, new Request.Callback() {

    @Override
    public void onCompleted(Response response) {
        try {
            JSONObject res = response.getGraphObject().getInnerJSONObject().getJSONArray("data").getJSONObject(0);
            numberOfLikes = res.getInt("likes");
        } catch (Exception e) {
        }
    }
});
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();

The request will be translated in the following graph api request.

Upvotes: 4

Related Questions