数据实的onActivityResult Android SDK中始终3.0空Android、onActivityResult、SDK

由网友(矫情伱麻痹)分享简介:我坚持了大量的调试后,这个错误的最后8小时,我已经发现,通过意图为空里面我的onActivityResult返回的数据方法。这是我用于登录的code。 signInWithFacebook(); 方法是通过按钮调用我的活动, 会话mCurrentSession; SessionTracker mSessionTra...

我坚持了大量的调试后,这个错误的最后8小时,我已经发现,通过意图为空里面我的onActivityResult返回的数据方法。

这是我用于登录的code。

signInWithFacebook(); 方法是通过按钮调用我的活动,

 会话mCurrentSession; SessionTracker mSessionTracker; 私人无效signInWithFacebook(){    mSessionTracker =新SessionTracker(getBaseContext(),新StatusCallback(){        @覆盖        公共无效呼叫(会话的会话,SessionState会状态,异常除外){        }    },空,假);    串的applicationID = Utility.getMetadataApplicationId(getBaseContext());    mCurrentSession = mSessionTracker.getSession();    如果(mCurrentSession == NULL || mCurrentSession.getState()。在isClosed()){        mSessionTracker.setSession(NULL);        会话的会话=新Session.Builder(getBaseContext())setApplicationId(的applicationID).build();        Session.setActiveSession(会话);        mCurrentSession =会议;        的System.out.println(会议结束或空+ mCurrentSession);    }    如果(!mCurrentSession.isOpened()){        Session.OpenRequest openRequest = NULL;        openRequest =新Session.OpenRequest(loginScreen.this);        如果(openRequest!= NULL){            openRequest.setPermissions(Arrays.asList(user_birthday,电子邮件,USER_LOCATION));            openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);            mCurrentSession.openForRead(openRequest);            //mCurrentSession.openForPublish(openRequest);            的System.out.println(1111);        }    }其他{        Request.executeMeRequestAsync(mCurrentSession,新Request.GraphUserCallback(){              @覆盖              公共无效onCompleted(GraphUser用户,响应响应){                    的System.out.println(..到达这里......);                    。JSON字符串= user.getInnerJSONObject()的toString();                    尝试{                        JSONObject的fbJson =新的JSONObject(JSON);                        电子邮件= fbJson.getString(电子邮件);                        名称= fbJson.getString(名称);                        ID = fbJson.getString(ID);                    }赶上(JSONException E){                        // TODO自动生成catch块                        e.printStackTrace();                    }                    的System.out.println(22222);                    如果(mProgressDialog1!= NULL)                    mProgressDialog1.dismiss();                    DisplayrogressDialog(登入);                    新LoginThread(电子邮件,,脸谱,ID)。开始();                    / *意图mIntent =新意图(loginScreen.this,SelectCourseActivity.class);                    mIntent.putExtra(fbid,身份证);                    mIntent.putExtra(fbemail,邮件);                    mIntent.putExtra(fbname,名称);                    mIntent.putExtra(signupvia,脸谱);                    mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);                    startActivity(mIntent); * /              }            });    }} 

和则的onActivityResult方法被调用,数据值作为下一次空在第一时间,但是当我点击该按钮将其与一些值返回数据,我能够登录,这不会发生在某些地方,但是,一些设备

 公共无效的onActivityResult(INT申请code,INT结果code,意图数据){              super.onActivityResult(要求code,结果code,数据);              如果(mProgressDialog1!= NULL)              mProgressDialog1.setMessage(连接到Studycopter ......);              其他              DisplayrogressDialogFB(连接到Studycopter ...,脸谱);              的System.out.println(getactive会话值+ Session.getActiveSession()的getState()+数据+数据);              如果(Session.getActiveSession()的getState()。等于(开放))              {                  mCurrentSession = Session.openActiveSession(本);              }              。Session.getActiveSession()的onActivityResult(loginScreen.this,请求code,结果code,数据);              mCurrentSession = Session.getActiveSession();              如果(mCurrentSession == NULL)              {                  mCurrentSession = Session.openActiveSession(本);              }              如果(mCurrentSession.isOpened()){                Request.executeMeRequestAsync(mCurrentSession,新Request.GraphUserCallback(){                      @覆盖                      公共无效onCompleted(GraphUser用户,响应响应){                            。JSON字符串= user.getInnerJSONObject()的toString();                            尝试{                                JSONObject的fbJson =新的JSONObject(JSON);                                电子邮件= fbJson.getString(电子邮件);                                名称= fbJson.getString(名称);                                ID = fbJson.getString(ID);                            }赶上(JSONException E){                                // TODO自动生成catch块                                e.printStackTrace();                            }                            新FbFurther()执行();                      }                    });                }            } 
从Android转投iPhone 3k助手教你移动数据

解决方案

请不要使用SessionTracker。它在com.facebook.internal包,它并不意味着供外部使用。它可以更改或在任何时间消失无向后兼容任何保证。你应该能够做到你需要只用Session类的一切。

其次,它看起来就像你正在试图做你的onActivityResult方法里面的一些会话管理逻辑。您应该所有这些逻辑搬进Session.StatusCallback,并通过唯一的onActivityResult传递到会话,是这样的:

 公共无效的onActivityResult(INT申请code,INT结果code,意图数据){    。Session.getActiveSession()的onActivityResult(loginScreen.this,请求code,结果code,数据);} 

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("");

                      }
                    });

                }
            }

解决方案

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

阅读全文

相关推荐

最新文章