我怎么能得到名字&在android studio中集成时的facebook的姓氏

时间:2018-03-12 08:30:28

标签: android facebook

我希望在Android工作室实施Facebook API期间获得FB API的名字和姓氏。我只是得到全名,请帮帮我

2 个答案:

答案 0 :(得分:0)

/me?fields=id,name,first_name,last_name

它被称为"声明字段",你必须要求你想要获得的字段,否则你将只获得默认字段(id,name)。

答案 1 :(得分:0)

在您的FaceBook登录按钮上

设置回叫管理器对象

mCallbackManager = CallbackManager.Factory.create();



    btn_FBSignUp.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                   
    //GraphRequest help you get data 
    
                    GraphRequest request = GraphRequest.newMeRequest(
                            loginResult.getAccessToken(),
                            new GraphRequest.GraphJSONObjectCallback() {
                                @Override
                                public void onCompleted(JSONObject object, GraphResponse response) {
                                    Log.i("LoginActivity", response.toString());
                                    try {
                                        // Application code
                                        String email = response.getJSONObject().getString("email");
                                        String Name = object.getString("first_name");
    
                              
                                        Toast.makeText(RegistrationActivity.this, Name, Toast.LENGTH_SHORT).show();
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                    Bundle parameters = new Bundle();

 //this line will define that what you want to get from 

                    parameters.putString("fields", "id, first_name, last_name,email,gender,birthday"); user porfile...
                    request.setParameters(parameters);
                    request.executeAsync();
    
                }
    
                @Override
                public void onCancel() {
                    // App code
                    Toast.makeText(RegistrationActivity.this, "FaceBook Login is Cancled", Toast.LENGTH_SHORT).show();
                }
    
                @Override
                public void onError(FacebookException exception) {
                    // App code
                    Toast.makeText(RegistrationActivity.this, "Something Happend Wrong: try Again", Toast.LENGTH_SHORT).show();
                }
            });