Android Facebook 4.0 SDK如何获取电子邮件,用户出生日期

时间:2016-11-07 11:17:50

标签: android android-facebook

活动代码:

 loginButton = (LoginButton) findViewById(R.id.login_button);
    loginButton.setReadPermissions(Arrays.asList("public_profile", "email", "user_birthday", "user_friends"));
    callbackManager = CallbackManager.Factory.create();
    loginButton.registerCallback(callbackManager, new FacebookCallback < LoginResult > () {
     @Override
     public void onSuccess(LoginResult loginResult) {
      AccessToken accessToken = loginResult.getAccessToken();
      Profile profile = Profile.getCurrentProfile();
      GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
       @Override
       public void onCompleted(JSONObject object, GraphResponse response) {
        try {
         String id = object.getString("id");
         String name = object.getString("name");
         String gender = object.getString("gender");
         String first_name = object.getString("first_name");
         String last_name = object.getString("last_name");
         String email = object.getString("email");
         String birthday = object.getString("birthday");
         tv_profile_name.setText(name);
         firstname.setText(first_name);
         lastname.setText(last_name);
         genderr.setText(gender);
         dob.setText(birthday);
         emailid.setText(email);
         String imageurl = "https://graph.facebook.com/" + id + "/picture?type=large";
         Picasso.with(MainActivity.this).load(imageurl).into(iv_profile_pic);
        } catch (JSONException e) {
         e.printStackTrace();
        }
       }
      });
      Bundle parameters = new Bundle();
      parameters.putString("fields", "id,name,email,gender, birthday,first_name,last_name");
      request.setParameters(parameters);
      request.executeAsync();
      accessTokenTracker = new AccessTokenTracker() {
       @Override
       protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
        if (currentAccessToken == null) {
         tv_profile_name.setText("");
         firstname.setText("");
         lastname.setText("");
         genderr.setText("");
         dob.setText("");
         emailid.setText("");
         iv_profile_pic.setVisibility(View.INVISIBLE);
        }
       }
      };
     }
     @Override
     public void onCancel() {}
     @Override
     public void onError(FacebookException error) {}
    });
    }

1 个答案:

答案 0 :(得分:0)

在onComplete方法中,使用GraphResponse而不是JSONObject的参数。这是我的代码:

@Override
                    public void onCompleted(JSONObject object, GraphResponse response) {
                        JSONObject jsonObject = response.getJSONObject();
                        if (jsonObject != null) {
                            try {
                                email1 = jsonObject.getString("email");
                                gender1 = jsonObject.getString("gender");
                                birthday1 = jsonObject.getString("birthday");
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }

试试,让我知道。