我使用Facebook SDK 4.16.1
使用Profile
我可以获得first name
,last name
,name
和id
我的代码是像这样
public class LoginActivity extends Activity {
String id, fname, lname, email, name, gender, locale, verified;
private CallbackManager callbackManager;
private AccessTokenTracker accessTokenTracker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginactivity);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
accessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
}
};
LoginButton loginButton = (LoginButton)findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList(Constants.FACEBOOK_PERMISSIONS));
FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
FB_TOKEN=loginResult.getAccessToken().getToken();
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
response.getError();
Log.e("JSON:", object.toString());
try {
email = object.getString("email");
gender = object.getString("gender");
locale = object.optString("locale");
verified = object.optString("verified");
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link,birthday,first_name,last_name,email,gender,verified,locale");
request.setParameters(parameters);
request.executeAsync();
id = profile.getId();
fname = profile.getFirstName();
lname = profile.getLastName();
name = profile.getName();
nextActivity(profile);
Toast.makeText(getApplicationContext(), "Logging in...", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException e) {
}
};
loginButton.registerCallback(callbackManager, callback);
}
}
我的常量看起来像这样
public static final String[] FACEBOOK_PERMISSIONS = new String[] {
"public_profile",
"user_friends",
"email" };
我试图获取电子邮件,性别,区域设置以及是否已经过验证。我如何获得其他信息?对于Profile
更新
我像这样更新我的GraphRequest
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
response.getError();
Log.e("JSON-RESULT:", object.toString());
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(object.toString());
email = jsonObject.getString("email");
gender = jsonObject.getString("gender");
locale = jsonObject.getString("locale");
verified = jsonObject.getString("verified");
Log.e(TAG, email + " This is the email");
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link,birthday,first_name,last_name,email,gender,verified,locale");
request.setParameters(parameters);
request.executeAsync();
在我的Logcat
中,它显示我正确设置了电子邮件和其他电子邮件,但是当您放置GraphRequest
时它不会进入breakpoints
,因此我的全球variable
电子邮件仍为空。即使在Log.e("JSON:", object.toString());
和Log.e(TAG, email + " This is the email");
中,它也显示我得到并设置了值。我想知道如何获得Bundle
的价值?
答案 0 :(得分:1)
尝试使用以下代码获取其他信息.....
private FacebookCallback<LoginResult> mFacebookCallback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// Log.d("Login", "onSuccess");
GraphRequest.newMeRequest(
loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject me, GraphResponse response) {
if (response.getError() != null) {
// handle error
} else {
email = me.optString("email");
String id = me.optString("id");
String email=me.optString("email");
String name= me.optString("name");
String gender= me.optString("gender");
String verified= me.optBoolean("is_verified") + "";
}
}
}).executeAsync();
}
@Override
public void onCancel () {
// Log.d("Login", "onCancel");
}
@Override
public void onError (FacebookException e){
// Log.d("Login", "onError " + e);
}
};