数据总是在onactivityresult facebook android sdk 3.0中为null

时间:2013-07-26 10:00:04

标签: android facebook facebook-android-sdk

经过大量调试后,我发现通过Intent返回的数据在我的onActivityResult方法中为空,我在过去8小时内遇到此错误。

这是我用来登录的代码。

在我的活动中通过按钮调用

signInWithFacebook();方法,

 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);*/

              }
            });
    }
}

然后在第一次调用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("");

                      }
                    });

                }
            }

1 个答案:

答案 0 :(得分:2)

请不要使用SessionTracker。它位于com.facebook.internal包中,并不代表外部消费。它可以在任何时候改变或消失,而不保证向后兼容性。您应该只使用Session类就可以完成所需的一切。

其次,看起来您正在尝试在onActivityResult方法中执行一些会话管理逻辑。您应该将所有这些逻辑移动到Session.StatusCallback中,并且只将onActivityResult传递给会话,例如:

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