Facebook Graph api:不返回FriendsList数据

时间:2017-04-08 13:48:41

标签: android facebook facebook-graph-api

我使用以下代码来获取Facebook好友的数量。但我只回复id

 GraphRequest getFriendsCountRequest = GraphRequest.newGraphPathRequest(
                fbToken,
                "/me",
                new GraphRequest.Callback() {
                    @Override
                    public void onCompleted(
                            GraphResponse response) {
                        // Application code

                        try {
                            System.out.println(response.getJSONObject().toString());
                            Long friendsCount = response.getJSONObject()
                                    .getJSONObject("friends")
                                    .getJSONObject("summary")
                                    .getLong("total_count");
                            System.out.println("Total Friends: " + friendsCount);
                        } catch (JSONException e) {
                            Log.e(LoggedInActivity.class.getCanonicalName(), "Error in getting count from response", e);
                        }

                    }
                });
        Bundle parameters = new Bundle();
        parameters.putString("fields", "friends");
        getFriendsCountRequest.setParameters(parameters);
        getFriendsCountRequest.executeAsync();

使用Graph Explorer时,我得到了/me/fields=friends

的预期回复

请帮忙!

1 个答案:

答案 0 :(得分:0)

尝试以下代码:使用Graph api检索好友列表。

public class MainActivity extends AppCompatActivity {

CallbackManager callbackManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    facebookSDKInitialize();
    setContentView(R.layout.activity_main);
    LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
    loginButton.setReadPermissions("user_friends");
    getLoginDetails(loginButton);
}

/*
 * Register a callback function with LoginButton to respond to the login result.
 */
protected void getLoginDetails(LoginButton login_button){

    // Callback registration
    login_button.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult login_result) {
            GraphRequestAsyncTask graphRequestAsyncTask = new GraphRequest(
                    login_result.getAccessToken(),
                    //AccessToken.getCurrentAccessToken(),
                    "/me/friends",
                    null,
                    HttpMethod.GET,
                    new GraphRequest.Callback() {
                        public void onCompleted(GraphResponse response) {
                            Intent intent = new Intent(MainActivity.this,FriendsList.class);
                            try {
                                JSONArray rawName = response.getJSONObject().getJSONArray("data");
                                intent.putExtra("jsondata", rawName.toString());
                                startActivity(intent);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
            ).executeAsync();

        }

        @Override
        public void onCancel() {
            // code for cancellation
        }

        @Override
        public void onError(FacebookException exception) {
            //  code to handle error
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
}

protected void facebookSDKInitialize() {

    FacebookSdk.sdkInitialize(getApplicationContext());
    callbackManager = CallbackManager.Factory.create();
}

@Override
protected void onResume() {
    super.onResume();

    // Logs 'install' and 'app activate' App Events.
    AppEventsLogger.activateApp(this);
}

@Override
protected void onPause() {
    super.onPause();

    // Logs 'app deactivate' App Event.
    AppEventsLogger.deactivateApp(this);
}

在gradle中:

 compile 'com.facebook.android:facebook-android-sdk:4.6.0'

以下是完整的the list of possible vars