android - onactivityresult facebook android sdk 3.0 中的数据始终为空

标签 android facebook facebook-android-sdk

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

这是我用来登录的代码。

signInWithFacebook(); 方法在我的 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);*/

              }
            });
    }
}

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

                      }
                    });

                }
            }

最佳答案

请不要使用 SessionTracker。它位于 com.facebook.internal 包中,不适合外部使用。它可以随时更改或消失,而无需任何向后兼容性保证。只需使用 Session 类,您就应该能够完成所需的一切。

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

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

关于android - onactivityresult facebook android sdk 3.0 中的数据始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17878417/

相关文章:

android - 错误在路径 : DexPathList[[zip file 上找不到类 "com.example.i.MainActivity"

java - Android countDownTimer - 使 millisUntilFinished float

Facebook Graph 高分辨率照片

android - Session.getActiveSession() 在启动应用程序时返回 null,尽管我已连接

android - 如何在android中以编程方式启用/禁用蓝牙

android - 来自 http url 的在线视频流,在 android 应用程序中具有缓存

c# - 从 Facebook 获取我的 friend

facebook - Unity Facebook SDK 不保存以前的登录 session

android - 如何在 Android 上向特定好友发送 Facebook 发送请求?

android - 导出已签名的应用程序时出现 facebook sdk 3.8 lint 错误