I am integrating Facebook
login in my android app and accessing user info using Graph API
.
I am getting an unusual behaviour, when i launch the app in which i am not logged in with Facebook
and then try to login in with Facebook
then it allows me to successfully login but doesn't allow to get the user info(that is NullPointerException
on JSON
object) and it crashes.
On the other hand if i again relaunch the app in which i am already logged in(successfully logged in before app crashing) and then press logout button and then login button again. Now strange thing happens and I can access user info after successful login. Please help me to access user info in the first case also.
Here is my code :
public class Login extends Activity implements View.OnClickListener{
private LoginButton facebookLogin;
private CallbackManager callbackManager;
private AccessTokenTracker accessTokenTracker;
private AccessToken accessToken;
private Profile facebookProfile;
private ProfileTracker profileTracker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
setContentView(R.layout.activity_login);
callbackManager = CallbackManager.Factory.create();
accessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(
AccessToken oldAccessToken,
AccessToken currentAccessToken) {
// Set the access token using
// currentAccessToken when it's loaded or set.
}
};
// If the access token is available already assign it.
accessToken = AccessToken.getCurrentAccessToken();
facebookProfile = Profile.getCurrentProfile();
/*profileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(
Profile oldProfile,
Profile currentProfile) {
// App code
}
};*/
facebookLogin = (LoginButton)findViewById(R.id.button_facebookLogin_Login);
facebookLogin.setReadPermissions(Arrays.asList("public_profile", "email"));
facebookLogin.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
facebookLogin.setReadPermissions(Arrays.asList("public_profile", "email"));
GraphRequest request = GraphRequest.newMeRequest(accessToken,
new GraphRequest.GraphJSONObjectCallback(){
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.d("TOP","Object = "+object.toString());
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender");
request.setParameters(parameters);
request.executeAsync();
// App code
}
@Override
public void onCancel() {
// App code
}
@Override
public void onError(FacebookException exception) {
// App code
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onDestroy() {
super.onDestroy();
//accessTokenTracker.stopTracking();
//profileTracker.stopTracking();
}
}