我正在我的Android应用中集成Facebook
登录并使用Graph API
访问用户信息。
我得到一个不寻常的行为,当我启动我没有使用Facebook
登录的应用程序然后尝试使用Facebook
登录然后它允许我成功登录但不会t允许获取用户信息(即NullPointerException
对象上的JSON
)并且崩溃。
另一方面,如果我再次重新启动我已登录的应用程序(在应用程序崩溃之前成功登录),然后再按下注销按钮,然后再次登录按钮。现在发生了奇怪的事情,我可以在成功登录后访问用户信息。请帮我在第一种情况下访问用户信息。
这是我的代码:
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();
}
}
答案 0 :(得分:1)
您可以尝试像我一样创建Graph
请求:Getting email from Facebook - FB android SDK
完成Callback
后,请使用AccessToken
,例如:loginResult.getAccessToken()
facebookLogin.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
facebookLogin.setReadPermissions(Arrays.asList("public_profile", "email"));
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),
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
}
如果您想在Graph Api Explorer
中看到示例Json
格式,请尝试Graph Api
请尝试这种方式并获得结果!