简体   繁体   English

数据在onactivityresult中始终为null Facebook android sdk 3.0

[英]data always null in onactivityresult facebook android sdk 3.0

I am stuck with this error for last 8 hours after a lot of debugging I have found that data returned via Intent is null inside my onActivityResult method. 经过大量调试后的最后8个小时,我一直在此错误中停留,我发现在onActivityResult方法中通过Intent返回的数据为null。

This is the code that I use to login. 这是我用来登录的代码。

signInWithFacebook(); method is called via button in my activity, 在我的活动中通过按钮调用方法,

 Session mCurrentSession;
 SessionTracker mSessionTracker; 

 private void signInWithFacebook() {

    mSessionTracker = new SessionTracker(getBaseContext(), new StatusCallback() {

        @Override
        public void call(Session session, SessionState state, Exception exception) {
        }
    }, null, false);

    String applicationId = Utility.getMetadataApplicationId(getBaseContext());
    mCurrentSession = mSessionTracker.getSession();


    if (mCurrentSession == null || mCurrentSession.getState().isClosed()) {
        mSessionTracker.setSession(null);
        Session session = new Session.Builder(getBaseContext()).setApplicationId(applicationId).build();
        Session.setActiveSession(session);
        mCurrentSession = session;

        System.out.println("session closed or null"+mCurrentSession);
    }
    if (!mCurrentSession.isOpened()) {
        Session.OpenRequest openRequest = null;
        openRequest = new Session.OpenRequest(loginScreen.this);

        if (openRequest != null) {
            openRequest.setPermissions(Arrays.asList("user_birthday", "email", "user_location"));
            openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
            mCurrentSession.openForRead(openRequest);
            //mCurrentSession.openForPublish(openRequest);
            System.out.println("1111");
        }
    }else {
        Request.executeMeRequestAsync(mCurrentSession, new Request.GraphUserCallback() {
              @Override
              public void onCompleted(GraphUser user, Response response) {

                    System.out.println("..reached here...");
                    String json=user.getInnerJSONObject().toString();

                    try {
                        JSONObject fbJson=new JSONObject(json);
                        Email=fbJson.getString("email");
                        Name=fbJson.getString("name");
                        Id=fbJson.getString("id");
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    System.out.println("22222");

                    if(mProgressDialog1!=null)
                    mProgressDialog1.dismiss();

                    DisplayrogressDialog("Signing in ");
                    new LoginThread(Email, "","facebook",Id).start();

                    /*Intent mIntent = new Intent(loginScreen.this, SelectCourseActivity.class);
                    mIntent.putExtra("fbid", Id);
                    mIntent.putExtra("fbemail", Email);
                    mIntent.putExtra("fbname", Name);
                    mIntent.putExtra("signupvia", "facebook");
                    mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
                    startActivity(mIntent);*/

              }
            });
    }
}

And then onActivityResult method is called with data value as null at the first time but next time when I click the same button it returns data with some values and I am able to login, This doesn't happen at certain places but on some devices 然后,第一次调用onActivityResult方法时,数据值将为null,但下次单击同一按钮时,它将返回具有某些值的数据,并且我能够登录,这在某些地方没有发生,但是在某些设备上

public void onActivityResult(int requestCode, int resultCode, Intent data) {
              super.onActivityResult(requestCode, resultCode, data);

              if(mProgressDialog1!=null)
              mProgressDialog1.setMessage("Connecting to Studycopter...");

              else
              DisplayrogressDialogFB("Connecting to Studycopter...","Facebook"); 


              System.out.println(" value of getactive session "+Session.getActiveSession().getState()+" data "+ data);

              if(Session.getActiveSession().getState().equals("OPENING"))
              {
                  mCurrentSession=Session.openActiveSession(this);  
              }


              Session.getActiveSession().onActivityResult(loginScreen.this, requestCode, resultCode, data);


              mCurrentSession=Session.getActiveSession();

              if(mCurrentSession==null)
              {
                  mCurrentSession=Session.openActiveSession(this);
              } 


              if (mCurrentSession.isOpened()) {

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

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

                            String json=user.getInnerJSONObject().toString();

                            try {
                                JSONObject fbJson=new JSONObject(json);
                                Email=fbJson.getString("email");
                                Name=fbJson.getString("name");
                                Id=fbJson.getString("id");
                            } catch (JSONException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                            new FbFurther().execute("");

                      }
                    });

                }
            }

Please DO NOT use SessionTracker. 请不要使用SessionTracker。 It's in the com.facebook.internal package, and it's not meant for external consumption. 它位于com.facebook.internal包中,并不表示供外部使用。 It could change or disappear at any time without any guarantees for backwards compatibility. 它可能随时更改或消失,而没有任何向后兼容性的保证。 You should be able to do everything you need with just the Session class. 仅使用Session类,您应该就能完成所需的一切。

Secondly, it looks like you're trying to do some session management logic inside your onActivityResult method. 其次,您似乎在尝试在onActivityResult方法中执行一些会话管理逻辑。 You should move all of those logic into a Session.StatusCallback, and ONLY pass through the onActivityResult to the session, something like: 您应该将所有这些逻辑移到Session.StatusCallback中,并且仅将onActivityResult传递给会话,例如:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Session.getActiveSession().onActivityResult(loginScreen.this, requestCode, resultCode, data);
}

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

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