繁体   English   中英

Android-Facebook SDK 3-仅墙贴

[英]Android - Facebook SDK 3 - Wall post only

我是Facebook SDK(3)的新手,并已成功编译并运行HelloFacebookSample。 现在,我只想发布墙上的帖子,所以我试图删除所有不必要的内容,并且只具有帖子状态功能

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    postStatusUpdateButton = (Button) findViewById(R.id.postStatusUpdateButton);
    postStatusUpdateButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            onClickPostStatusUpdate();
        }
    });
}

private void onClickPostStatusUpdate() {
    performPublish(PendingAction.POST_STATUS_UPDATE);
}

private void performPublish(PendingAction action) {
    Session session = Session.getActiveSession();
    if (session != null) {
        pendingAction = action;
        if (hasPublishPermission()) {
            // We can do the action right away.
            handlePendingAction();
        } else {
            // We need to reauthorize, then complete the action when we get called back.
             Session.ReauthorizeRequest reauthRequest = new Session.ReauthorizeRequest(this, PERMISSIONS).
                        setRequestCode(REAUTHORIZE_ACTIVITY).
                        setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
            session.reauthorizeForPublish(reauthRequest);
        }
    }
}

但是,在performPublish会话上为null。

我自己遇到了这个问题。 会话为空,因为用户未登录; 用户必须先登录才能调用reauthorizeRequest或发送请求。

一种方法是确保用户单击LoginButton并成功登录。 如果他们在尝试发布状态更新时未登录(会话为null),则应显示AlertDialog告诉他们登录-或更好的是,打开登录对话框。

LoginButton是一个很好的便捷工具,但不幸的是,Facebook只是为您提供了选择,您可以选择完全按原样使用LoginButton还是从头开始编写所有内容。 但是,如果在Eclipse中打开LoginButton,则可以看到重要的代码:

Session currentSession = sessionTracker.getSession();
                if (currentSession == null || currentSession.getState().isClosed()) {
                    sessionTracker.setSession(null);
                    Session session = new Session.Builder(context).setApplicationId(applicationId).build();
                    Session.setActiveSession(session);
                    currentSession = session;
                }
                if (!currentSession.isOpened()) {
                    Session.OpenRequest openRequest = null;
                    if (parentFragment != null) {
                        openRequest = new Session.OpenRequest(parentFragment);
                    } else if (context instanceof Activity) {
                        openRequest = new Session.OpenRequest((Activity)context);
                    }

                    if (openRequest != null) {
                        openRequest.setPermissions(permissions);
                        openRequest.setLoginBehavior(loginBehavior);

                        if (SessionAuthorizationType.PUBLISH.equals(authorizationType)) {
                            currentSession.openForPublish(openRequest);
                        } else {
                            currentSession.openForRead(openRequest);
                        }
                    }
                }

这是登录代码(即显示登录屏幕,以便用户可以登录)。 您可以自己使用SessionTracker,但我认为Facebook可能并不意味着人们可以使用它(它不在SDK文档中,并且包是com.facebook.internal,这表示他们不希望开发人员使用它)- -但是您可以根据需要调整代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM