Search code examples
javatwittertwitter4j

How to convert friend id's to corresponding name in twitter using twitter4j?


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?


Solution

  • 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.