Android - 从Facebook JSON端点获取朋友数量 - android

时间:2017-12-24 20:11:38

标签: android json facebook

我在Facebook上有一个JSON端点,它返回经过身份验证的用户的电子邮件,生日和其他参数。我的挑战是能够从JSON端点获得朋友的数量。以下是我的代码段

private void FaceBookLogin() {
        fbLoginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {

            @Override
            public void onSuccess(LoginResult loginResult) {

                // App code
                GraphRequest request = GraphRequest.newMeRequest(
                        loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(
                                    JSONObject object,
                                    GraphResponse response) {
                                // Application code
                                Log.v("Profile ---------   ", response.toString());
                                Log.v("Object ---------   ", object.toString());

                                try {
                                    Name = object.getString("name");
                                    Email = object.getString("email");
                                    DOB = object.getString("birthday");
                                    FRIENDSCOUNT= object.getString("parameterhere"); //here is the challenge
                                    Log.v("Email = ", " " + Email);
                                    Log.v("PUBLIC PROFILE = ", " " + PUBLICPROFILE);
                                    //                                    Toast.makeText(getApplicationContext(), "Name " + Name, Toast.LENGTH_LONG).show();

                                    Toast.makeText(LoginActivity.this, "Login Successful!", Toast.LENGTH_LONG).show();

                                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                    startActivity(intent);

请问,如何从上述代码段中获取用户的朋友数量?

1 个答案:

答案 0 :(得分:1)

当您点击图表API请求时,您可以获取可邀请的朋友数量。

/* make the API call */

GraphRequest request =  new GraphRequest(
 loginResult.getAccessToken(),
"/{user-id}/friends",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
    public void onCompleted(GraphResponse response) {
        /* handle the result */
           Log.v("Profile ---------   ", response.toString());
    }
}
).executeAsync();

您可以在控制台中打印此响应,并获取所需的结果。