图 API 在 android 中无法正常工作
Graph API is not working properly in android
我正在 Facebook
登录我的 android 应用程序并使用 Graph API
访问用户信息。
我遇到异常行为,当我启动未使用 Facebook
登录的应用程序,然后尝试使用 Facebook
登录时,它允许我成功登录,但不允许获取用户信息(即 JSON
对象上的 NullPointerException
)并且它崩溃了。
另一方面,如果我再次重新启动我已经登录的应用程序(在应用程序崩溃之前成功登录),然后按注销按钮,然后再次登录按钮。现在奇怪的事情发生了,成功登录后我可以访问用户信息。请帮助我在第一种情况下访问用户信息。
这是我的代码:
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();
}
}
你能像我一样尝试创建 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
}
如果您想查看示例 Json
格式,请尝试 Graph Api
in : Graph Api Explorer
就这样试试吧,请出结果!
我正在 Facebook
登录我的 android 应用程序并使用 Graph API
访问用户信息。
我遇到异常行为,当我启动未使用 Facebook
登录的应用程序,然后尝试使用 Facebook
登录时,它允许我成功登录,但不允许获取用户信息(即 JSON
对象上的 NullPointerException
)并且它崩溃了。
另一方面,如果我再次重新启动我已经登录的应用程序(在应用程序崩溃之前成功登录),然后按注销按钮,然后再次登录按钮。现在奇怪的事情发生了,成功登录后我可以访问用户信息。请帮助我在第一种情况下访问用户信息。
这是我的代码:
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();
}
}
你能像我一样尝试创建 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
}
如果您想查看示例 Json
格式,请尝试 Graph Api
in : Graph Api Explorer
就这样试试吧,请出结果!