简体   繁体   English

Android如何在用户已经登录时获取用户Facebook信息

[英]Android how to get user Facebook information when user is already logged in

How can I get users facebook information when he is already logged in? 当用户已经登录后,我如何获得用户的Facebook信息? I think I am doing everything fine but its not working.. 我想我一切都很好,但是没有用。

1st. 1号 I am getting the accessToken and checking if it exists. 我正在获取accessToken并检查它是否存在。 If it does exist I try to get user data. 如果确实存在,我会尝试获取用户数据。

AccessToken accessToken = AccessToken.getCurrentAccessToken();

   if (accessToken != null) {

      String userData = getUserDataFromFacebook(accessToken, headerTitle);

      headerTitle.setText(userData);
   }

2nd. 2号 I try to get the user data the same way as i would get it at the first facebook login. 我尝试以与第一次Facebook登录时相同的方式获取用户数据。

getUserDataFromFacebook:

    private String getUserDataFromFacebook(AccessToken accessToken, final TextView headerTitle) {

Log.v("LoginActivity", "I am here");  // this log works

        GraphRequest.newMeRequest(
                accessToken,
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(JSONObject object, GraphResponse response) {

                        Log.v("LoginActivity", "RESPONSE: " + response); //this log doesn`t work.

                        // Application code
                        try {
                             Log.v("LoginActivity", "I am here"); //this log doesn`t work
                             name = object.getString("name");

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

                });

        return name;

    }

The biggest problem is that the the onCompleted method is not called and I cant access any of the information. 最大的问题是onCompleted方法没有被调用,我无法访问任何信息。 I have no idea why.. 我不知道为什么..

PS I am terrible at Java and this is my first android application. PS我在Java方面很糟糕,这是我的第一个android应用程序。

Try this code as it is it will help you to get data from facebook. 尝试使用此代码,因为它可以帮助您从Facebook获取数据。 make sure you already set permission for data on FacebookLoginButton. 确保您已经在FacebookLoginButton上设置了数据权限。

private void getUserDataFromFacebook() {
            GraphRequest request = GraphRequest.newMeRequest(
                    AccessToken.getCurrentAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response) {
                            Log.e(" Response", response.toString());
                            String res = (String) object.toString();
                            Log.e("Response", res);
                            Gson gson = new Gson();
                            if (res.length() > 0) {

                               //do your work here with your res

                            } else {
                                Utils.showDialog(SelectFacebookAlbumActivity.this, "",
                                        "Error in getting data");
                            }

                        }
                    });

            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name");
            request.setParameters(parameters);
            request.executeAsync();
        }

Let us know it it works for you. 让我们知道它对您有用。

You're not actually executing the request. 您实际上并没有执行请求。

GraphRequest request = GraphRequest.newMeRequest(
        accessToken,
        new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(
                   JSONObject object,
                   GraphResponse response) {
                // your logic
            }
        });
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link");
request.setParameters(parameters);
request.executeAsync();

Keep in mind this is an Async call so you can't return the name from the function like you've tried to do. 请记住,这是一个异步调用,因此您无法像尝试那样从函数中返回名称。 Instead you'll have to implement a callback mechanism. 相反,您将必须实现回调机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM