I am using facebook graph request to retrieve friends list. But, How can I get to create a function and return it after the call graphRequest.executeAsync()
is done?.
private Map<String, String> getFacebookFriends(AccessToken accessToken, Profile profile) throws InterruptedException, ExecutionException {
final Map<String, String> friendsMap = new HashMap<>();
GraphRequest graphRequest = new GraphRequest(accessToken, "/me/friends", null, HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
JSONObject jGraphObj = response.getJSONObject();
try {
JSONArray friendsData = jGraphObj.getJSONArray("data");
for (int i = 0; i < friendsData.length(); i++) {
JSONObject friend = friendsData.getJSONObject(i);
String friendId = friend.getString("id");
String friendName = friend.getString("name");
friendsMap.put(friendId, friendName);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
);
List<GraphResponse> gResponseList = graphRequest.executeAsync().get();
return friendsMap;
}
I am currently using the same technique like in this post. By calling it like graphRequest.executeAsync().get();
. But it seems like it's not working.
The function above will return the friendsMap
before the graphRequest
is done.
Any suggestion is appreciated.