0

Here I'm able to get all friends ids. But for the same I need to get name of corresponding id. How can I get it? For getting friends ids I'm using Twitter4j lib and my code is:

String friendsIds = twitter.getFriendsIDs(MyId, cursor).toString();

For this output is:

friendsIds:IDsJSONImpl{ids=[43347766, 2369925598, 238933377, 243381784, 946613156, 541261639], previousCursor=0, nextCursor=0}

How can I get names for those ids?

Jonathan
  • 20,053
  • 6
  • 63
  • 70
tajMahal
  • 418
  • 6
  • 18
  • 40

1 Answers1

1

You can use lookupUsers(long[]) to bulk retrieve your friends and get access to their screen names, e.g.:

final IDs friendIds = twitter.getFriendsIDs(MyId, cursor);
final ResponseList<User> users = twitter.lookupUsers(friendIds.getIDs());

for (User u : users) {
    System.out.println(u.getScreenName());
}

Note that you can retrieve up to a maximum of 100 users at a time using the lookupUsers API call, so you may have to split the ids up if you have more than 100 friends.

Jonathan
  • 20,053
  • 6
  • 63
  • 70
  • Thank you @Jonathan, How can i sort those names according to alphabetically,could you please help me? – tajMahal Jun 10 '14 at 13:30
  • No problem. Take a look at [this existing answer](http://stackoverflow.com/a/2784576/69875) - you can apply the same approach using `getScreenName()` instead of `getStartDate()`. – Jonathan Jun 10 '14 at 13:36