Search code examples
androidfacebook-graph-apifacebook-likefacebook-comments

How to integrate "Like" and "Comment" feature using Android Facebook SDK?


I want to implement "Like" and "Comment" feature in my app. I used this code:

public static void like(String postID) {
String grapPath = String.format("%s/likes", postID);
Request request = new Request(Session.getActiveSession(), grapPath,
    null, HttpMethod.POST, new Callback() {
   @Override
   public void onCompleted(Response response) {
    Log.i(TAG, response.toString()+" Success!");
   }
});
Request.executeBatchAsync(request);
}

public static void postComment(String comment, String postID) {
String grapPath = String.format("%s/comments", postID);
Bundle bundle = new Bundle();
bundle.putString("message", comment);
Request request = new Request(Session.getActiveSession(), grapPath,
        bundle, HttpMethod.POST, new Callback() {
    @Override
    public void onCompleted(Response response) {
        Log.i(TAG, "Success!");
    }
});
    Request.executeBatchAsync(request);
 }

Hhow and where can I call these methods to make them work?


Solution

  • Please make sure the prerequisites are set up correctly. Specifically check out the middle of step 4 to make sure you generated your key hash correctly using your debug keystore.

    Otherwise code below should help out

    private boolean hasPublishPermission() {
            Session session = Session.getActiveSession();
            return session != null && session.getPermissions().contains("publish_actions");
        }
    private void postStatusUpdate() {
           if (hasPublishPermission()) {
                final String message = "Posting to facebook";
                Request request = Request
                        .newStatusUpdateRequest(Session.getActiveSession(), message, place, tags, new Request.Callback() {
                            @Override
                            public void onCompleted(Response response) {
                                showPublishResult(message, response.getGraphObject(), response.getError());
                            }
                        });
                request.executeAsync();
            } else {
                pendingAction = PendingAction.POST_STATUS_UPDATE;
            }
        }