如何使用Facebook SDK 4获取姓名和电子邮件?

时间:2015-05-19 07:19:39

标签: android facebook-graph-api android-asynctask android-logcat android-facebook

我正在使用 Facebook SDK 4 ,我在sdk 2之前做过这个...但对我来说一些语法已经改变所以我遇到了一些问题...我无法取用户名,电子邮件

这是我的XML文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  xmlns:facebook="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

 <com.facebook.login.widget.LoginButton
  android:id="@+id/LoginFbButton"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="  Login With Facebook" />

</LinearLayout>

这是我登录Facebook的代码

private void loginToFacebook() {
Session.openActiveSession(this, true, new Session.StatusCallback() {

    @Override
    public void call(Session session, SessionState state, Exception exception) {
        if (session.isOpened()) {
            Log.i(TAG, "Access Token" + session.getAccessToken());
            Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
                @Override
                public void onCompleted(GraphUser user, Response response) {
                    if (user != null) {
                        try {


                             firstName=user.getFirstName();
                             lastName=user.getLastName();
                             Log.d("Facebook User", "details:" + firstName+""+lastName);
                             }catch(Exception e){

请参阅我的Logcat

  com.facebook.katana.platform.PlatformService has leaked ServiceConnection com.facebook.fbservice.ops.DefaultBlueServiceOperationFactory$DefaultOperation$BlueServiceConnection@4307e1d8 that was originally bound here    E/ActivityThread(28413): android.app.ServiceConnectionLeaked: Service com.facebook.katana.platform.PlatformService has leaked ServiceConnection com.facebook.fbservice.ops.DefaultBlueServiceOperationFactory$DefaultOperation$BlueServiceConnection@4307e1d8 that was originally bound here  

2 个答案:

答案 0 :(得分:1)

您可以在此link

中找到有关ur Soln的详细信息

也可以参考Facebook开发人员notes

做这样的事

  GraphRequest request = GraphRequest.newMeRequest(
                     loginResult.getAccessToken(),
                     new GraphRequest.GraphJSONObjectCallback() {
                         @Override
                         public void onCompleted(
                                 JSONObject object,
                                 GraphResponse response) {
                             pDialog.dismiss();
                             Log.d("Response",response.getJSONObject().toString());
                             if (response.getError() != null) {
                                 // handle error
                             } else {
                                 String email = object.optString("email");
                                 String fname = object.optString("fname");
                                 String lname = object.optString("lname");
                               Log.d("Email",email);
                               Log.d("fname",fname);
                               Log.d("lname",lname);
                          //     Log.d("Response", response.getInnerJsobject.toString());

                             }
                         }
                     });

答案 1 :(得分:0)

如果我理解v4,你首先需要获得令牌,然后解决你想要的所有查询,我个人会这样做:

    mLoginFacebookButton.setFragment(this);
    mLoginFacebookButton.setReadPermissions(Arrays.asList("email"));
    mLoginFacebookButton.registerCallback(mCallbackManager, callback);

回调是:

FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(final LoginResult loginResult) {

        GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(JSONObject user, GraphResponse response) {
                email = user.getString("email");
                firstName = user.getString("first_name");
                lastName = user.getString("last_name");
            }
        }).executeAsync();
    }

    @Override
    public void onCancel() {

    }

    @Override
    public void onError(FacebookException exception) {

    }
};