How to get Facebook friends list using Android Facebook sdk?

I created an application on facebook and took the first few steps to implement the Facebook SDK.

This is what I am trying to do: get a list of friends from Facebook and select some friends from this list and import them into your application.

How am i doing this?

+6
source share
1 answer

Are you trying to get the Facebook friend list registered in your application? It looks like you need to fulfill the facebook schedule request to get this list, and then bring out the friends you want.
https://developers.facebook.com/docs/graph-api/reference/user/friendlists/

If you want to do this in Android java, this 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(); 

Since "member" is an edge in "friendlist", you can make a new request with your friendlist id to get members of this 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(); } 

In the documentation for the Facebook Graph request, you can see what you can do with User objects. Unfortunately, I do not have enough reputation to publish another link.

Keep in mind that the user had to log in with facebook in order to get the access token needed to complete these operations.

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

+10
source

All Articles