我使用TextView显示登录的facebook用户的姓名和电子邮件,但我真的不知道如何获取姓名和电子邮件?
private void onSessionStateChange(Session session, SessionState state,
Exception exception) {
if (session != currentSession) {
return;
}
if (state.isOpened()) {
// Log in just happened.
Toast.makeText(getApplicationContext(), "session opened",
Toast.LENGTH_SHORT).show();
} else if (state.isClosed()) {
// Log out just happened. Update the UI.
Toast.makeText(getApplicationContext(), "session closed",
Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:1)
您可以通过以下方式获取姓名和电子邮件:
// use their Facebook info
JSONObject json = Util.parseJson(facebook.request("me"));
String facebookID = json.getString("id");
String firstName = json.getString("first_name");
String lastName = json.getString("last_name");
Toast.makeText(uiActivity,"Thank you for Logging In, "
+ firstName + " "
+ lastName + "!"
, Toast.LENGTH_SHORT).show();
答案 1 :(得分:1)
这是 facebook Sdk 4 + 的工作示例您也可以关注此Link
这是你的XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.facebook.login.widget.LoginButton
android:id="@+id/connectWithFbButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal"
android:text=" connect_with_facebook" />
</LinearLayout>
将其放入onCreate
private LoginButton loginButton;
CallbackManager callbackManager;
loginButton = (LoginButton) findViewById(R.id.connectWithFbButton);
loginButton.setReadPermissions(Arrays.asList("public_profile", "email", "user_birthday"));
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please Wait..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
pDialog.dismiss();
Log.d("Response",response.getJSONObject().toString());
if (response.getError() != null) {
// handle error
} else {
String email = object.optString("email");
String fname = object.optString("fname");
String lname = object.optString("lname");
Log.d("Email",email);
Log.d("fname",fname);
Log.d("lname",lname);
// Log.d("Response", response.getInnerJsobject.toString());
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
// App code
Log.d("LoginActivity", "cancel");
}
@Override
public void onError(FacebookException exception) {
// App code
Log.d("LoginActivity", exception.getCause().toString());
}
});
在你OnActivityResult写下面
@Override
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
callbackManager.onActivityResult(requestCode, responseCode, intent);
}
答案 2 :(得分:0)
请参阅:详细信息here
private class SessionStatusCallback implements Session.StatusCallback {
private String fbAccessToken;
@Override
public void call(Session session, SessionState state, Exception exception) {
updateView();
if (session.isOpened()) {
fbAccessToken = session.getAccessToken();
// make request to get facebook user info
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
Log.i("fb", "fb user: "+ user.toString());
String fbId = user.getId();
String fbAccessToken = fbAccessToken;
String fbName = user.getName();
String gender = user.asMap().get("gender").toString();
String email = user.asMap().get("email").toString();
Log.i("fb", userProfile.getEmail());
}
});
}
}
}