我能够使用代码实现facebook登录:
public class MainActivity extends ActionBarActivity implements FacebookCallback<LoginResult> {
//List<String> permissionNeeds=Arrays.asList("user_photos","friends_photos", "email", "user_birthday", "user_friends");
TextView t;
String get_id, get_name, get_gender, get_email, get_birthday, get_locale, get_location;
private CallbackManager mCallbackManager;
private LoginButton mFbLoginButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//init facebook sdk and
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
//instantiate callbacks manager
mCallbackManager = CallbackManager.Factory.create();
mFbLoginButton=(LoginButton)findViewById(R.id.login_button);
mFbLoginButton.registerCallback(mCallbackManager, this);
mFbLoginButton.setReadPermissions(Arrays.asList("user_status","email"));
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//manage login result
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
public void onSuccess(LoginResult loginResults) {
Toast.makeText(getApplicationContext(), "Success ",
Toast.LENGTH_LONG).show();
}
public void onCancel() {
}
public void onError(FacebookException e) {
}
}
现在我如何获取用户详细信息,如姓名,电子邮件,年龄等。我在网上搜索了一些示例,但我没有成功。我应该把代码放在哪里?
答案 0 :(得分:1)
为此,您可以使用此功能。
//add variable names.
private static String uid,email;
public void getuserdata() {
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
Log.v("LoginActivity", response.toString());
try {
//and fill them here like so.
uid = object.getString("id");
email = object.getString("email")
} catch (JSONException e) {
e.printStackTrace();
}
getGroups();
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender");
request.setParameters(parameters);
request.executeAsync();
}
要使用它,只需在你的on create中调用getuserdata()。