Friends list of a friend using Twitter4J

前端 未结 7 891
旧时难觅i
旧时难觅i 2021-01-12 23:40

How can I obtain the friends list of a friend or follower using Twitter4J?

Using getFriendsId(), I\'m only able to retrieve the friend\'s/follower\'s list of that cu

7条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-13 00:20

    This code works! (without exceeding rate limits). Referred twitter4j documentation and other answers on StackOverflow.

        try {
                // get friends
                long cursor = -1;
                PagableResponseList pagableFollowings;
                do {
                    pagableFollowings = twitter.getFriendsList(twitter.getId(), cursor);
                    for (User user : pagableFollowings) {
                        listFriends.add(user); // ArrayList
                    }
                } while ((cursor = pagableFollowings.getNextCursor()) != 0);
    
                // get followers
                cursor = -1;
                PagableResponseList pagableFollowers;
                do {
                    pagableFollowers = twitter.getFollowersList(twitter.getId(), cursor);
                    for (User user : pagableFollowers) {
                        listFollowers.add(user); // ArrayList
                    }
                } while ((cursor = pagableFollowers.getNextCursor()) != 0);
    
            } catch (TwitterException e) {
                printError(e);
            }
    

提交回复
热议问题