0

我正在尝试将 Facebook 登录信息集成到我的应用程序中。用户使用 Facebook 登录,然后应用程序获取用户电子邮件地址,该电子邮件地址被传递到我的应用程序登录系统(帐户存储在远程 mysql 数据库中)。我有以下代码:

public class FbLoginActivity extends Activity {

    private static List<String> permissions;
    Session.StatusCallback statusCallback = new SessionStatusCallback();
    ProgressDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fblogin);
        Button fbButton = (Button) findViewById(R.id.fbshare);
        /***** FB Permissions *****/
        permissions = new ArrayList<String>();
        permissions.add("email");
        /***** End FB Permissions *****/
        fbButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Check if there is any Active Session, otherwise Open New
                // Session
                Session session = Session.getActiveSession();
                if (!session.isOpened()) {
                    session.openForRead(new Session.OpenRequest(
                            FbLoginActivity.this).setCallback(statusCallback)
                            .setPermissions(permissions));
                } else {
                    Session.openActiveSession(FbLoginActivity.this, true,
                            statusCallback);
                }
            }
        });
        Session session = Session.getActiveSession();
        if (session == null) {
            if (savedInstanceState != null) {
                session = Session.restoreSession(this, null, statusCallback,
                        savedInstanceState);
            }
            if (session == null) {
                session = new Session(this);
            }
            Session.setActiveSession(session);
            session.addCallback(statusCallback);
            if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
                session.openForRead(new Session.OpenRequest(this).setCallback(
                        statusCallback).setPermissions(permissions));
            }
        }
    }

    private class SessionStatusCallback implements Session.StatusCallback {

        @Override
        public void call(Session session, SessionState state,
                Exception exception) {
            // Check if Session is Opened or not
            processSessionStatus(session, state, exception);
        }
    }

    public void processSessionStatus(Session session, SessionState state,
            Exception exception) {
        if (session != null && session.isOpened()) {
            if (session.getPermissions().contains("email")) {
                // Show Progress Dialog
                dialog = new ProgressDialog(FbLoginActivity.this);
                dialog.setMessage("Logging in..");
                dialog.show();
                Request.executeMeRequestAsync(session,
                        new Request.GraphUserCallback() {

                            @Override
                            public void onCompleted(GraphUser user,
                                    Response response) {

                                if (dialog != null && dialog.isShowing()) {
                                    dialog.dismiss();
                                }
                                if (user != null) {
                                    Map<String, Object> responseMap = new HashMap<String, Object>();
                                    GraphObject graphObject = response
                                            .getGraphObject();
                                    responseMap = graphObject.asMap();
                                    Log.i("FbLogin", "Response Map KeySet - "
                                            + responseMap.keySet());
                                    // TODO : Get Email
                                    // responseMap.get("email");
                                    String fb_id = user.getId();
                                    String email = null;
                                    String name = (String) responseMap
                                            .get("name");
                                    if (responseMap.get("email") != null) {
                                        email = responseMap.get("email")
                                                .toString();
                                        Intent i = new Intent(FbLoginActivity.this, FbLogin2Activity.class);
                                        i.putExtra("Email", email);
                                        startActivity(i);
                                    } else {
                                        // Clear all session info & ask user to
                                        // login again
                                        Session session = Session
                                                .getActiveSession();
                                        if (session != null) {
                                            session.closeAndClearTokenInformation();
                                        }
                                    }
                                }
                            }
                        });
            } else {
                session.requestNewReadPermissions(new Session.NewPermissionsRequest(
                        FbLoginActivity.this, permissions));
            }
        }
    }

    /********** Activity Methods **********/
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d("FbLogin", "Result Code is - " + resultCode + "");
        Session.getActiveSession().onActivityResult(FbLoginActivity.this,
                requestCode, resultCode, data);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO Save current session
        super.onSaveInstanceState(outState);
        Session session = Session.getActiveSession();
        Session.saveSession(session, outState);
    }

    @Override
    protected void onStart() {
        // TODO Add status callback
        super.onStart();
        Session.getActiveSession().addCallback(statusCallback);
    }

    @Override
    protected void onStop() {
        // TODO Remove callback
        super.onStop();
        Session.getActiveSession().removeCallback(statusCallback);
    }
}  

然后我收到以下错误

09-12 15:28:07.728: E/AndroidRuntime(30897): FATAL EXCEPTION: main
09-12 15:28:07.728: E/AndroidRuntime(30897): java.lang.UnsupportedOperationException: Session: an attempt was made to open an already opened session.
09-12 15:28:07.728: E/AndroidRuntime(30897):    at com.facebook.Session.open(Session.java:947)
09-12 15:28:07.728: E/AndroidRuntime(30897):    at com.facebook.Session.openForRead(Session.java:385)
09-12 15:28:07.728: E/AndroidRuntime(30897):    at com.test.app.FbLoginActivity$1.onClick(FbLoginActivity.java:48)
09-12 15:28:07.728: E/AndroidRuntime(30897):    at android.view.View.performClick(View.java:4278)
09-12 15:28:07.728: E/AndroidRuntime(30897):    at android.view.View$PerformClick.run(View.java:17430)
09-12 15:28:07.728: E/AndroidRuntime(30897):    at android.os.Handler.handleCallback(Handler.java:725)
09-12 15:28:07.728: E/AndroidRuntime(30897):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-12 15:28:07.728: E/AndroidRuntime(30897):    at android.os.Looper.loop(Looper.java:213)
09-12 15:28:07.728: E/AndroidRuntime(30897):    at android.app.ActivityThread.main(ActivityThread.java:5092)
09-12 15:28:07.728: E/AndroidRuntime(30897):    at java.lang.reflect.Method.invokeNative(Native Method)
09-12 15:28:07.728: E/AndroidRuntime(30897):    at java.lang.reflect.Method.invoke(Method.java:511)
09-12 15:28:07.728: E/AndroidRuntime(30897):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
09-12 15:28:07.728: E/AndroidRuntime(30897):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:564)
09-12 15:28:07.728: E/AndroidRuntime(30897):    at dalvik.system.NativeStart.main(Native Method)

但是有时它在某些设备上运行良好。

4

2 回答 2

1

在您的 fbButton 的 onClick 侦听器中,您有:

Session session = Session.getActiveSession();
if (!session.isOpened()) {
    session.openForRead(...);
} else {
    Session.openActiveSession(...);
}

但是在你的 else 子句中,会话已经打开,所以你不需要再次打开它。相反,您应该做的是:

Session session = Session.getActiveSession();
if (session == null) {
    Session.openActiveSession(...);
} else if (!session.isOpened()) {
    session.openForRead(...);
}
于 2013-09-13T05:15:43.553 回答
0

它是说试图打开一个已经打开的会话,所以你试图在某个地方重新打开一个已经打开的会话。这可能与缓存会话有关。您可以查看此链接: Session.getActiveSession() 在启动应用程序时返回 null,尽管我已连接

于 2013-09-12T15:36:32.013 回答