簡體   English   中英

在Android中登錄Facebook后,會話在OnActivityResult方法中為空

[英]Session is becoming null in OnActivityResult Method after login to Facebook in Android

我正在嘗試使用Facebook實現登錄功能,成功登錄后,我必須獲取用戶的一些信息。 現在登錄,我寫了一些代碼。 當我按“登錄”按鈕時,它正在打開Facebook,從facebook本地應用程序中獲取登錄信息,並且登錄時也沒有錯誤。 但是之后,它又返回到我的應用程序的登錄頁面,並在OnActivityResult()方法中,會話變為空。 所以我無法獲得詳細信息。 我寫了下面的代碼:

      if(view == mBtnFacebook)
        {
               Session currentSession = Session.getActiveSession();
                if (currentSession == null || currentSession.getState().isClosed()) {
                    Session session = new Session.Builder(LoginScreen.this).build();
                    Session.setActiveSession(session);
                    currentSession = session;
                }

                if (currentSession.isOpened()) {
                    // Do whatever u want. User has logged in
                     Log.d("Testing", "current session is opened already  ");

                } else if (!currentSession.isOpened()) {
                    // Ask for username and password

//                  currentSession.openForRead(new Session.OpenRequest(this).setCallback(statusCallback);
                    OpenRequest op = new Session.OpenRequest((Activity) this);

                    op.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
                    op.setCallback(null);

                    List<String> permissions = new ArrayList<String>();
                    permissions.add("email");
                    op.setPermissions(permissions);

                    Session s = new Session(LoginScreen.this);//new Builder(LoginScreen.this).build();
                    Session.setActiveSession(s);
                    s.openForPublish(op);
                }
                }

                public void call(Session session, SessionState state, Exception exception) {
            }

            @Override
            public void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                Log.d("Testing", "in onActivityResult");
                if (Session.getActiveSession() != null)
                    Session.getActiveSession().onActivityResult(this, requestCode,
                            resultCode, data);

                Session currentSession = Session.getActiveSession();
                if (currentSession == null || currentSession.getState().isClosed()) {
                    Log.d("Testing", "current session null");
                    Session session1 = new Session.Builder(getBaseContext()).build();
                    Session.setActiveSession(session1);
                    currentSession = session1;
                }

                if (currentSession.isOpened()) {
                    Log.d("Testing", "current session already opened");
                    Session.openActiveSession(this, true, new Session.StatusCallback() {

                        @Override
                        public void call(final Session session, SessionState state,
                                Exception exception) {

                            if (session.isOpened()) {

                                Request.executeMeRequestAsync(session,
                                        new Request.GraphUserCallback() {

                                            @Override
                                            public void onCompleted(GraphUser user,
                                                    Response response) {
                                                if (user != null) {

                                                mFbUser = user;

                                                }
                                            }
                                        });
                            }
                        }
                    });
                }
            }

我該如何解決這個問題。 請幫忙。

謝謝,阿林丹。

我已經解決了這個問題。 基本上我使用的是舊版本的SDK,但設備中的FB App是最新的。 因此,它給了問題。 感謝Akhil指出可以幫助我解決的SDK版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM