7

我正在使用 Facebook Android SDK。有没有一种简单的方法可以让用户的朋友下载了该应用程序?例如,应用程序 Draw Something 实现了这一点。我似乎找不到有关此主题的任何信息

我猜如果可能的话,httppost 中需要一些额外的信息来访问这些信息。

4

3 回答 3

54

*注意:从 3.14 版本开始,我/朋友只会返回同样使用该应用程序的朋友,因此不推荐使用以下实现。请参阅新的“Invitable Friends”或“Taggable Friends”API 以获取替代方案。

使用新的Facebook SDK for Android (3.0)很容易获得用户的好友列表。以下是代码:

private void requestFacebookFriends(Session session) {
    Request.executeMyFriendsRequestAsync(session,
            new Request.GraphUserListCallback() {
                @Override
                public void onCompleted(List<GraphUser> users,
                        Response response) {
                    // TODO: your code for friends here!
                }
            });
}

然而,为了获取正在使用您的 Facebook 应用程序的用户的朋友有点复杂(由于 Facebook API 文档)。但是,是的,这是可能的

首先,创建您的请求:

private Request createRequest(Session session) {
    Request request = Request.newGraphPathRequest(session, "me/friends", null);

    Set<String> fields = new HashSet<String>();
    String[] requiredFields = new String[] { "id", "name", "picture",
            "installed" };
    fields.addAll(Arrays.asList(requiredFields));

    Bundle parameters = request.getParameters();
    parameters.putString("fields", TextUtils.join(",", fields));
    request.setParameters(parameters);

    return request;
}

Note that you need to insert the field "installed" in your request. I'm requesting the user picture path with the same request. Check your possibilities here.

Next, you can use above code to create your request and then get your friends:

private void requestMyAppFacebookFriends(Session session) {
    Request friendsRequest = createRequest(session);
    friendsRequest.setCallback(new Request.Callback() {

        @Override
        public void onCompleted(Response response) {
            List<GraphUser> friends = getResults(response);
            // TODO: your code here
        }
    });
    friendsRequest.executeAsync();
}

Note that using this generic request, you don't receive a GraphUser list as response. You'll need following code to get the response as GraphUser list:

private List<GraphUser> getResults(Response response) {
    GraphMultiResult multiResult = response
            .getGraphObjectAs(GraphMultiResult.class);
    GraphObjectList<GraphObject> data = multiResult.getData();
    return data.castToListOf(GraphUser.class);
}

Now you can use your user's friend list, with the information if each of your friends use your Facebook app:

GraphUser user = friends.get(0);
boolean installed = false;
if(user.getProperty("installed") != null)
    installed = (Boolean) user.getProperty("installed");
于 2013-03-04T06:48:40.547 回答
2

我以这种方式实现它

Bundle params = new Bundle();

params.putString("fields", "name, picture, location, installed");

在显示列表中的项目期间,在 getView() 方法中我这样做了

boolean b = jsonObject.getBoolean("installed");

if (b){
    Log.d("VIVEK",jsonObject.getString("name"));
}
于 2012-05-17T09:45:48.000 回答
1

我希望这篇文章对你有帮助: 我如何让我的朋友使用我的 facebook App 和 graph api asp.net

根据Facebook SDK,该has_added_app字段已弃用,您应该使用is_app_user

于 2012-04-17T21:11:45.280 回答