如何获取Facebook用户性别,电子邮件,电话号码详细信息

时间:2015-12-16 12:30:55

标签: android facebook

public class FacebookLoginActivity extends ActionBarActivity {
    private List<String>           permissions    = new ArrayList<String>();
    // FaceBook
    private Session.StatusCallback statusCallback = new SessionStatusCallback();
    private Button   facebook_sigin;
    private TextView name, email, gender, phone, address;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fb);
        name = (TextView) findViewById(R.id.textView1);
        gender = (TextView) findViewById(R.id.gender);
        phone = (TextView) findViewById(R.id.phone);
        address = (TextView) findViewById(R.id.address);
        facebook_sigin = (Button) findViewById(R.id.facebooksigin);

        permissions.add("email");
        permissions.add("gender");
        permissions.add("address");


        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        facebook_sigin.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (UtillClass.isConnectingToInternet(FacebookLoginActivity.this)) {
                    Session session = Session.getActiveSession();
                    if (session != null && !session.isOpened()
                            && !session.isClosed()) {
                        Log.e("facebook login", "1");
                        Session.OpenRequest request = new Session.OpenRequest(
                                FacebookLoginActivity.this);
                        request.setPermissions(permissions);
                        request.setCallback(statusCallback);
                        session.openForRead(request);
                    } else {
                        Log.e("facebook login", "2");
                        Session.openActiveSession(FacebookLoginActivity.this, true,
                                statusCallback);
                    }
                }
            }
        });

        Session session = Session.getActiveSession();
        if (session == null) {
            if (session == null) {
                session = new Session(this);
                session.openForRead(new Session.OpenRequest(this).setPermissions(Arrays.asList("email")));
            }
            Session.setActiveSession(session);
        }
    }

    public class SessionStatusCallback implements Session.StatusCallback {

        @Override
        public void call(final Session session, SessionState state,
                         Exception exception) {

            Request request = new Request(session, "/me", null, HttpMethod.GET,
                    new Request.Callback() {

                        public void onCompleted(Response response) {

                            Log.e("Facebook Response ", "Response==>"+response);
                            Log.e("Facebook Login ", "onCompleted");
                            Log.e("session", "" + session.getAccessToken());

                            try {

                                JSONObject userDetails = response
                                        .getGraphObject().getInnerJSONObject();
                                Log.e("fblogijsnresponse",userDetails.toString());
                                Log.e("facebook user id=",
                                        userDetails.getString("id"));

                                if (userDetails.has("name")) {
                                    Log.e("facebook user name=",
                                            userDetails.getString("name"));
                                    name.setText(userDetails.getString("name"));
                                }

                                if (userDetails.has("first_name")) {
                                    Log.e("fbuserfname=", userDetails.getString("first_name"));
                                }

                                if (userDetails.has("last_name")) {
                                    Log.e("fbuserlname=",userDetails.getString("last_name"));
                                }

                                if (userDetails.has("gender")) {
                                    Log.e("fbgender","fbgender");
                                    Log.e("fbgender=",userDetails.getString("gender"));
                                    gender.setText(userDetails.getString("gender"));
                                }

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }

                    });

            request.executeAsync();
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.e("facebook", "onStart");
        Session.getActiveSession().addCallback(statusCallback);

    }

    @Override
    public void onStop() {
        super.onStop();
        Log.e("facebook", "onStop");
        Session.getActiveSession().removeCallback(statusCallback);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.e("facebook", "onSaveInstanceState");
        Session session = Session.getActiveSession();
        Session.saveSession(session, outState);
    }

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

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        // goBack();
    }

}

我试图获取Facebook用户性别,电子邮件,电话和地址详细信息。可以请任何帮助我找出这些细节。上面的代码我添加了性别,电子邮件,地址的权限,但在Facebook响应中只有facebook用户名。我不知道是否能得到所有细节都没有请帮我解决这个问题。

1 个答案:

答案 0 :(得分:-1)

请以这种方式定义权限。

your_fb_button.setReadPermissions("user_friends");
your_fb_button.setReadPermissions("public_profile");
your_fb_button.setReadPermissions("email");
your_fb_button.setReadPermissions("user_birthday");

有关详细信息,请按以下示例操作:Android Facebook 4.0 SDK How to get Email, Date of Birth and gender of User