在android中获取Facebook用户详细信息

时间:2013-07-25 04:00:55

标签: android facebook facebook-android-sdk

我开发了一个Android应用程序,我从Facebook获取用户详细信息。我能够获取用户名,名字,姓氏,但电子邮件在某些Facebook帐户中为空。在某些Facebook帐户中,我发现电子邮件ID和一些Facebook帐户电子邮件为空?为什么?

1 个答案:

答案 0 :(得分:4)

问题是电子邮件权限这是获取电子邮件ID的工作代码。 这是我在这里给出的 Unable to access facebook login?

  Session.openActiveSession(this, true, new Session.StatusCallback() {

            // callback when session changes state
            @Override
            public void call(Session session, SessionState state,
                    Exception exception) {
                if (session.isOpened()) {
                    // make request to the /me API

                    List<String> permissions = session.getPermissions();
                    if (!isSubsetOf(PERMISSIONS, permissions)) {
                        pendingPublishReauthorization = true;
                        Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(
                                RegisterActivity.this, PERMISSIONS);
                        session.requestNewReadPermissions(newPermissionsRequest);
                        return;
                    }
                    Request.executeMeRequestAsync(session,
                            new Request.GraphUserCallback() {

                        // callback after Graph API response with
                        // user object
                        @Override
                        public void onCompleted(GraphUser user,
                                Response response) {
                            if (user == null) {
                                Toast.makeText(
                                        RegisterActivity.this
                                        .getApplicationContext(),
                                        "Facebook Error",
                                        Toast.LENGTH_LONG).show();

                            } else {
                                Toast.makeText(
                                        RegisterActivity.this
                                        .getApplicationContext(),
                                        user.getName()
                                        + " Logged in Successfully.",
                                        Toast.LENGTH_LONG).show();
                                GraphUser abc = user;

                                id = user.getId();
                                name = user.getName();
                                gender = user.getProperty("gender")
                                        .toString();
                                editname.setText(name);
                                username.setText(user.getUsername());
                                JSONObject jo = user
                                        .getInnerJSONObject();
                                Log.d("Details", jo.toString());
                                try {
                                    emailid.setText(user
                                            .getProperty("email")
                                            .toString());
                                } catch (Exception e1) {
                                    // TODO Auto-generated catch
                                    // block
                                    e1.printStackTrace();
                                }

                                try {
                                    location = user.getLocation()
                                            .getProperty("name")
                                            .toString();
                                } catch (Exception e) {

                                    e.printStackTrace();
                                }



                            }

                            return;
                        }

                    });

                }
            }
        });

onActivityResult 方法

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Session.getActiveSession().onActivityResult(this, requestCode,
            resultCode, data);
}

isSubsetoff()方法

   private boolean isSubsetOf(Collection<String> subset,
        Collection<String> superset) {
    for (String string : subset) {
        if (!superset.contains(string)) {
            return false;
        }
    }
    return true;
}
根据您的需要

权限

  private static final List<String> PERMISSIONS = Arrays.asList("email",
        "user_about_me", "user_location");