在Android中登录Facebook后,OnActivityResult方法中的会话将变为空
我正在尝试使用facebook实现登录功能,成功登录后,我必须获取用户的一些信息。现在为了登录,我写了一些代码。当我按下“登录”按钮时,它会打开facebook,从facebook本机应用程序获取登录信息,并无误登录。但在这之后,它将返回到我的应用程序的登录页面,在OnActivityResult方法中,会话将变为null。所以我无法得到细节。 我已经编写了以下代码:在Android中登录Facebook后,OnActivityResult方法中的会话将变为空,android,facebook,session,facebook-graph-api,Android,Facebook,Session,Facebook Graph Api,我正在尝试使用facebook实现登录功能,成功登录后,我必须获取用户的一些信息。现在为了登录,我写了一些代码。当我按下“登录”按钮时,它会打开facebook,从facebook本机应用程序获取登录信息,并无误登录。但在这之后,它将返回到我的应用程序的登录页面,在OnActivityResult方法中,会话将变为null。所以我无法得到细节。 我已经编写了以下代码: if(view == mBtnFacebook) { Session
if(view == mBtnFacebook)
{
Session currentSession = Session.getActiveSession();
if (currentSession == null || currentSession.getState().isClosed()) {
Session session = new Session.Builder(LoginScreen.this).build();
Session.setActiveSession(session);
currentSession = session;
}
if (currentSession.isOpened()) {
// Do whatever u want. User has logged in
Log.d("Testing", "current session is opened already ");
} else if (!currentSession.isOpened()) {
// Ask for username and password
// currentSession.openForRead(new Session.OpenRequest(this).setCallback(statusCallback);
OpenRequest op = new Session.OpenRequest((Activity) this);
op.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
op.setCallback(null);
List<String> permissions = new ArrayList<String>();
permissions.add("email");
op.setPermissions(permissions);
Session s = new Session(LoginScreen.this);//new Builder(LoginScreen.this).build();
Session.setActiveSession(s);
s.openForPublish(op);
}
}
public void call(Session session, SessionState state, Exception exception) {
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("Testing", "in onActivityResult");
if (Session.getActiveSession() != null)
Session.getActiveSession().onActivityResult(this, requestCode,
resultCode, data);
Session currentSession = Session.getActiveSession();
if (currentSession == null || currentSession.getState().isClosed()) {
Log.d("Testing", "current session null");
Session session1 = new Session.Builder(getBaseContext()).build();
Session.setActiveSession(session1);
currentSession = session1;
}
if (currentSession.isOpened()) {
Log.d("Testing", "current session already opened");
Session.openActiveSession(this, true, new Session.StatusCallback() {
@Override
public void call(final Session session, SessionState state,
Exception exception) {
if (session.isOpened()) {
Request.executeMeRequestAsync(session,
new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user,
Response response) {
if (user != null) {
mFbUser = user;
}
}
});
}
}
});
}
}
我如何解决这个问题。请帮忙
谢谢,
阿林丹。我已经解决了这个问题。基本上,我使用的SDK版本较旧,但我的FB应用程序在设备上是最新的。因此,它提出了这个问题。感谢Akhil指出帮助我解决问题的SDK版本。您使用的是哪个版本的FB SDK?我完全不知道。但我最近下载并添加了它。有办法知道吗?