data always null in onactivityresult facebook android sdk 3.0

旧街凉风 提交于 2020-01-14 03:35:31

问题


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.

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

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:


Please DO NOT use SessionTracker. It's in the com.facebook.internal package, and it's not meant for external consumption. 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.

Secondly, it looks like you're trying to do some session management logic inside your onActivityResult method. You should move all of those logic into a Session.StatusCallback, and ONLY pass through the onActivityResult to the session, something like:

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


来源:https://stackoverflow.com/questions/17878417/data-always-null-in-onactivityresult-facebook-android-sdk-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!