Facebook SDK - 获取朋友详细信息

时间:2014-02-28 08:20:30

标签: android facebook

我需要获取用户选择的朋友的详细信息。

用户可以使用默认的Facebook好友选择器选择他的朋友。但是当我将选择作为GraphUser的列表时,我看不到用户的任何细节。特别是,我无法获得用户名。

这是朋友选择器的活动:

public class FacebookFriendPickerActivity extends FragmentActivity {

public static final String IEXTRA_SELECTED_FRIENDS = "selected friends";

private FriendPickerFragment friendPickerFragment;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_facebook_frind_picker);

    Bundle args = getIntent().getExtras();
    FragmentManager manager = getSupportFragmentManager();
    Fragment fragmentToShow = null;

    if (savedInstanceState == null) {
        friendPickerFragment = new FriendPickerFragment(args);
    } else {
        friendPickerFragment = (FriendPickerFragment) manager.findFragmentById(R.id.picker_fragment);
    }
    // Set the listener to handle errors
    friendPickerFragment.setOnErrorListener(new PickerFragment.OnErrorListener() {
        @Override
        public void onError(PickerFragment<?> fragment, FacebookException error) {
            Toast.makeText(FacebookFriendPickerActivity.this, error.getLocalizedMessage(), Toast.LENGTH_LONG).show();
            setResult(RESULT_CANCELED);
            finish();
        }
    });
    // Set the listener to handle button clicks
    friendPickerFragment.setOnDoneButtonClickedListener(new PickerFragment.OnDoneButtonClickedListener() {
        @Override
        public void onDoneButtonClicked(PickerFragment<?> fragment) {
            List<GraphUser> users = friendPickerFragment.getSelection();
            if (users.size() > 0) {
                ArrayList<String> usernames = new ArrayList<String>(users.size());
                for (GraphUser user : users)
                    usernames.add(user.getUsername());
                Intent data = new Intent();
                data.putStringArrayListExtra(IEXTRA_SELECTED_FRIENDS, usernames);
                setResult(RESULT_OK, data);
                finish();
            } else {
                setResult(RESULT_CANCELED);
                finish();
            }
        }
    });
    fragmentToShow = friendPickerFragment;
    manager.beginTransaction().replace(R.id.picker_fragment, fragmentToShow).commit();
}

@Override
protected void onStart() {
    super.onStart();
    friendPickerFragment.loadData(false);
}
}

当我尝试读取用户名列表时,我总是得到null。

提前致谢。

1 个答案:

答案 0 :(得分:1)

当您使用basic_info权限(默认权限)查询好友列表时,您只会获得ID和名称字段。要获取用户名字段,您必须将其作为额外字段添加到friendPickerFragment。

可以通过添加这行代码

来简单地完成
friendPickerFragment.setExtraFields(Arrays.asList("username"));

检查相关文档here

上的setExtraFields方法