在做Facebook帖子功能时,某些代码行没有被执行

时间:2013-05-06 06:13:37

标签: android facebook-android-sdk

我希望在Facebook上发帖,因为使用facebook sdk,但是某些代码行没有执行。弹出一个登录对话框,当我登录时,SessionEvents.AuthListener listener应该听取登录成功。但它不听。每当我开始app时,它都会要求登录 下面是我的代码。

public class FacebookConnector {
private Facebook mFacebook;
private AuthListener mSessionListener;
private Context context;
private String[] permissions;
private Activity activity;

public FacebookConnector(String appId, Activity activity, Context context, String[] permissions) {

    this.mFacebook = new Facebook(appId);
    SessionStore.restore(mFacebook, context);
    SessionEvents.addAuthListener(mSessionListener);
    SessionEvents.addLogoutListener((LogoutListener) mSessionListener);
    this.context = context;
    this.permissions = permissions;
    this.activity = activity;

}

public void postMessageOnWall(String msg) {
    if (mFacebook.isSessionValid()) {
        Bundle parameters = new Bundle();
        parameters.putString("message", msg);
        try {
            String response = mFacebook.request("me/feed", parameters, "POST");
            System.out.println(response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        login();
    }
}

public void login() {
    if (!mFacebook.isSessionValid()) {
        mFacebook.authorize(this.activity, this.permissions, Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());
    }
}

public Facebook getFacebook() {
    return mFacebook;
}
}

和我的活动课......

private void onFacebookBtnClicked() {
    if (facebookConnector.getFacebook().isSessionValid()) {
        postMessageInThread();
    } else {
        SessionEvents.AuthListener listener = new SessionEvents.AuthListener() {
            @Override
            public void onAuthSucceed() {
                postMessageInThread();
            }

            @Override
            public void onAuthFail(String error) {

            }
        };
        SessionEvents.addAuthListener(listener);
        facebookConnector.login();
    }
}
private void postMessageInThread() {
    Thread t = new Thread() {
        public void run() {
            try {
                facebookConnector.postMessageOnWall(mQuote.getQuoteText() + "\n" + mQuote.getAuthorName());
                mFacebookHandler.post(mUpdateFacebookNotification);
            } catch (Exception ex) {
                Log.e("Facebook", "Error sending msg", ex);
            }
        }
    };
    t.start();
}

final Runnable mUpdateFacebookNotification = new Runnable() {
    public void run() {
        Toast.makeText(getBaseContext(), "Facebook updated !", Toast.LENGTH_LONG).show();
    }
};

postMessageInThread方法中没有执行,不知道为什么

2 个答案:

答案 0 :(得分:1)

将此权限设置为公开

private String[] PERMISSIONS = new String[]

    { "publish_stream", "read_stream", "offline_access" };

这将检查会话是否存在,如果是,那么它将直接更新,如果没有,然后要求用户权限更新在墙上。

答案 1 :(得分:1)

嗨朋友们,我得到了解决方案,对于我的问题,所以现在发布答案。我只需要覆盖onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    mFacebook.authorizeCallback(requestCode, resultCode, data);
}

我刚添加它并让我的应用程序正常工作。