7

I am using the Facebook Android SDK. Is there an easy way to get a user's friends who have downloaded the app? For example, the app Draw Something implements this. I can't seem to find any information on this subject

I would guess that if it was possible, some extra information would be needed in the httppost to access this information.

James Fazio
  • 6,370
  • 9
  • 38
  • 47
  • hey, i too want friends list in a listview. i have used request facebook friends method as in the first part of JPMagalhaes answer below. But i dont know how to habdle the result. I mean how to get the result in a listview. what i am supposed to do with List users. u can answer it here: http://stackoverflow.com/questions/26773639/working-with-facebook-sdk – Hirak Chhatbar Nov 07 '14 at 04:58

3 Answers3

54

* Note: since 3.14 version, me/friends will only return friends that also use the app, so below implementation is deprecated. See the new "Invitable Friends" or "Taggable Friends" APIs for alternatives.

Using the new Facebook SDK for Android (3.0) is very easy to get your user's friend list. Following is the code:

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!
                }
            });
}

Nevertheless, in order to get the user's friends who are using your Facebook app is a little bit complicated (due to Facebook API documentation). But yes, it is possible.

First of all, create your request:

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");
JPMagalhaes
  • 3,611
  • 1
  • 25
  • 26
  • 2
    +1 for completeness and correctness, I can't say it better than @BlakeMiller. – VansFannel Jun 29 '13 at 13:02
  • How does the "installed" property works? Does that include users who granted read permissions to the app? – Jacek Kwiecień May 27 '14 at 17:58
  • I'm not sure, @Xylian. What exactly you mean with 'read permissions'? Following the documentation, installed mean: "Is the app making the request installed?" – JPMagalhaes May 27 '14 at 19:19
  • 4
    If you create your app after 30/Apr/2014 , you can only obtain the user's friends who are also using your Facebook app. You are **no longer** able to retrieve the friend list – user782104 Jun 04 '14 at 03:15
  • @JPMagalhaes - hey, can u plz tell me how to handle result that we get in the first part of your answer. (how to get friends's list in a listview) – Hirak Chhatbar Nov 07 '14 at 12:58
  • You have to implement an adapter to handle this. – JPMagalhaes Nov 10 '14 at 02:08
  • how can i get all the friends of user ?? – rana_sadam Apr 17 '15 at 08:30
  • @JPMagalhaes The first code `requestFacebookFriends` doesn't work for me, the packages are missing even if I **do** have the `import com.facebook.FacebookSdk;` set in motion. Could you tell me what the problem is? – agiro Nov 11 '17 at 18:41
  • 1
    @agiro, this answer is from 2013, last updated in 2014. So I guess this is deprecated. See the new "Invitable Friends" or "Taggable Friends" APIs for alternatives. – JPMagalhaes Nov 15 '17 at 00:31
  • Thanks. Pain is, the official FB documentation has lead me astray too :D – agiro Nov 15 '17 at 10:40
2

I implemented it in this way

Bundle params = new Bundle();

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

And during displaying of the items on the list,in the getView() method i did this

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

if (b){
    Log.d("VIVEK",jsonObject.getString("name"));
}
animuson
  • 53,861
  • 28
  • 137
  • 147
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
1

I hope this post helps you: How can i get my friends using my facebook App with graph api asp.net

According to the Facebook SDK, the has_added_app field is deprecated and you should use the is_app_user

Community
  • 1
  • 1
Peter Aron Zentai
  • 11,482
  • 5
  • 41
  • 71
  • Thanks for the extra info, so is there no way to make a call to the facebook graph api to get users who are using the app? – James Fazio Apr 17 '12 at 21:53
  • As I found you can execute FQL query to collect this info: SELECT uid FROM user WHERE is_app_user=1 and uid IN (SELECT uid2 FROM friend WHERE uid1 = {USERS'S FB ID}) – Peter Aron Zentai Apr 17 '12 at 21:58