2013-07-26 32 views
1

經過大量調試後,我在過去的8個小時中遇到了這個錯誤,我發現通過Intent返回的數據在我的onActivityResult方法中爲null。數據始終爲空onactivityresult facebook android sdk 3.0

這是我用來登錄的代碼。

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方法被調用,數據值爲空的第一次,但下一次,當我點擊它與一些返回的數據相同的按鈕價值觀和我能夠登錄,這並不在某些地方,但在某些設備上

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

我也有這種方法的麻煩。經過幾十個小時的調試後,很明顯問題是:在Manifest中,持有此代碼的Activity被設置爲'noHistory =「true」'。畢竟,我最終使用了與Fragments一起工作的官方登錄按鈕,因爲上面的方法非常髒,使用內部方法等。 – caw

回答

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); 
} 
+0

那麼signInWithFacebook的上述代碼將如何更改?我也見過#1118578請聯繫應用程序開發人員在我的ICS設備上敬酒,但這不包含其他設備。 http://stackoverflow.com/a/13926065/1503130你對這個答案的評論是什麼? – Prateek

+0

Hey @Ming Li,我嘗試使用UILifeCycleHelper實現FB登錄流程,在某些情況下,通常包含FB響應的「mMap」字段的'data'參數爲null。爲什麼會發生這種情況? – toobsco42