如何使用Android Facebook sdk从Facebook获取朋友列表? [英] How to get friend list from Facebook using Android Facebook sdk?

查看:1345
本文介绍了如何使用Android Facebook sdk从Facebook获取朋友列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Facebook上创建了一个应用程序,并实现了实现Facebook SDK的第一步。

I have created an app on facebook and achieve some first steps for implementing Facebook SDK.

这是我要做的:从Facebook获取朋友的列表,并能够从该列表中选择一些朋友,并将其导入到我的应用程序。

This is what I'm trying to do: get list of friends from Facebook and to be able to pick some friends from that list and import them to my app.

我如何做?

推荐答案

获取登录到您的应用程序的用户的Facebook好友列表?听起来你需要执行一个Facebook图表请求来获取该列表,然后提取你想要的朋友。

https://developers.facebook.com/docs/graph-api/reference/user/friendlists/

Are you trying to get the Facebook friendlist of the user logged in into your app? Sounds like you need to perform a facebook graph request to acquire that list, and then withdraw the friends you want.
https://developers.facebook.com/docs/graph-api/reference/user/friendlists/

如果你想在Android java中做到这一点,她就是一个例子:

If you wanted to do it in Android java, her is an example:

    AccessToken token = AccessToken.getCurrentAccessToken();
        GraphRequest graphRequest = GraphRequest.newMeRequest(token, new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
                try {
                    JSONArray jsonArrayFriends = jsonObject.getJSONObject("friendlist").getJSONArray("data");
                    JSONObject friendlistObject = jsonArrayFriends.getJSONObject(0);
                    String friendListID = friendlistObject.getString("id"); 
                    myNewGraphReq(friendListID);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    Bundle param = new Bundle();
    param.putString("fields", "friendlist", "members");
    graphRequest.setParameters(param);
    graphRequest.executeAsync();

由于member是friendlist中的一个边缘,您可以使用您的friendlist Id以获得该特定朋友列表的成员。 https://developers.facebook.com/docs/graph -api / reference / friend-list / members /

Since "member" is an edge in "friendlist" you can do a new request with your friendlist Id to get the members of that particular friend list. https://developers.facebook.com/docs/graph-api/reference/friend-list/members/

private void myNewGraphReq(String friendlistId) {
    final String graphPath = "/"+friendlistId+"/members/";
    AccessToken token = AccessToken.getCurrentAccessToken();
    GraphRequest request = new GraphRequest(token, graphPath, null, HttpMethod.GET, new GraphRequest.Callback() {
        @Override
        public void onCompleted(GraphResponse graphResponse) {
            JSONObject object = graphResponse.getJSONObject();
            try {
                JSONArray arrayOfUsersInFriendList= object.getJSONArray("data");  
                /* Do something with the user list */
                /* ex: get first user in list, "name" */
                JSONObject user = arrayOfUsersInFriendList.getJSONObject(0);
                String usersName = user.getString("name");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });
     Bundle param = new Bundle();
    param.putString("fields", "name");
    request.setParameters(param);
    request.executeAsync();
}

在Facebook Graph请求的文档中,您可以看到可以使用用户对象。我没有足够的代表发布另一个链接不幸的是。

In the documentation for Facebook Graph request you can see what you can do with the User objects. I don't have enough rep to post another link unfortunately.

请记住,用户必须使用Facebook登录才能获得执行这些操作所需的访问令牌。

Keep in mind that a user must have signed in with facebook to get the access token needed to do these operations.

嗯,希望这是你正在寻找的东西。

Well, hope it was something remotely like this you were looking for.

这篇关于如何使用Android Facebook sdk从Facebook获取朋友列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆