如何使用Android Facebook SDK集成“赞”和“评论”功能?

时间:2013-08-15 03:32:20

标签: android facebook-graph-api facebook-like facebook-comments

我想在我的应用中实现“赞”和“评论”功能。我用了这段代码:

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,我在哪里可以调用这些方法来使它们工作?

1 个答案:

答案 0 :(得分:3)

请确保prerequisites设置正确。具体来说,请查看步骤4的中间部分,以确保使用调试密钥库正确生成密钥哈希。

否则下面的代码应该有帮助

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;
        }
    }