如何在Facebook SDK 4.0.1(Android)中获取Facebook用户电子邮件

时间:2015-04-23 13:43:16

标签: facebook-android-sdk

在SDK 3中,我使用user.asMap().get("email"))通过它收到了电子邮件。现在,升级到4.0后,我无法从配置文件对象中获取它。有什么建议吗?

1 个答案:

答案 0 :(得分:3)

现在首先我要说的是,人们不应该利用你的权利投票给别人,而应该尝试提供帮助!

我终于发布了答案,因为它可以帮助那些面临类似问题的人:)

您只需要在onsuccess方法中添加此部分。

  GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {

                            try {
                                email = graphResponse.getJSONObject().getString("email");
                                updateUI();
                            } catch (org.json.JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                    request.executeAsync();

jsonObject包含您需要的所有数据:)

Here is the complete code





  LoginManager.getInstance().registerCallback(callbackManager,
                        new FacebookCallback<LoginResult>() {
                            @Override
                            public void onSuccess(LoginResult loginResult) {
                                handlePendingAction();


                                 GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {

                                try {
                                    email = graphResponse.getJSONObject().getString("email");
                                    updateUI();
                                } catch (org.json.JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                        request.executeAsync(); 


                            }

                            @Override
                            public void onCancel() {
                                if (pendingAction != PendingAction.NONE) {
                                    showAlert();
                                    pendingAction = PendingAction.NONE;
                                }

                            }

                            @Override
                            public void onError(FacebookException exception) {
                                if (pendingAction != PendingAction.NONE
                                        && exception instanceof FacebookAuthorizationException) {
                                    showAlert();
                                    pendingAction = PendingAction.NONE;
                                }
                                updateUI();
                            }

                            private void showAlert() {
                                new AlertDialog.Builder(LoginScreenActivity.this)
                                        .setTitle(R.string.cancelled)
                                        .setMessage(R.string.permission_not_granted)
                                        .setPositiveButton(R.string.ok, null)
                                        .show();
                            }
                        });