Search code examples
androidfacebookfacebook-likefacebook-android-sdk

get number of likes of photo shared from app


i share a photo using facebook sdk in this way:

File file = new File("imagePath");

Request photoRequest = Request.newUploadPhotoRequest(session, file, new Callback() {            
    @Override
    public void onCompleted(Response response) {
        if(response.getError()==null){
            try {
               String id = response.getGraphObject().getInnerJSONObject().get("id").toString();
              //store id
            } catch (JSONException e1) {
                e1.printStackTrace();
            }
        }
    }           
});
photoRequest.getParameters().putString("message", "xxx");
photoRequest.executeAsync();

Now i want to get likes on that id. I try in this way:

Session session = Session.getActiveSession();
Bundle bundle = new Bundle();
bundle.putString("id", id); // this is the id stored before
bundle.putString("fields", "likes");
Request request = new Request(session, "search", bundle, HttpMethod.GET, new Request.Callback() {

    @Override
    public void onCompleted(Response response) {
        try {
            JSONObject obj = response.getGraphObject().getInnerJSONObject();
            //Obj is always -> {"data":[]}
        } catch (Exception e) {
        }
    }
});
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();

What facebook API gets is always {"data":[]}.

I use this permission:

session.requestNewPublishPermissions(new NewPermissionsRequest(act, "publish_actions","user_photos","user_status"));

What could be the problem?? Can someone help me? Thanks!


Solution

  • I solved in this way:

    Session session = Session.getActiveSession();
    Bundle params = new Bundle();
    String fql    = "SELECT like_info,src_big FROM photo WHERE object_id=\""+shared.getId()+"\"";
    params.putString("q", fql);
    Request request = new Request(session, "/fql",params, HttpMethod.GET, new Request.Callback() {      
        @Override
        public void onCompleted(Response response) {
            String message = "Foto cancellata";
            try {
                JSONObject resp = response.getGraphObject().getInnerJSONObject();
                JSONArray  data = resp.getJSONArray("data");
                if(data.length()>0){
                    JSONObject like_info = data.getJSONObject(0).getJSONObject("like_info");
                    String src           = data.getJSONObject(0).getString("src_big");
                    if(!image.exists()){
                        aq.id(shared_image).image(src);
                    }                   
                    int count = like_info.getInt("like_count");
                }else{
                   //PICTURE HAS BEEN DELETED
                }
            } catch (Exception e) {
                e.printStackTrace();
            }       
        }
    });
    RequestAsyncTask task = new RequestAsyncTask(request);
    task.execute();