Friends list of a friend using Twitter4J

前端 未结 7 889
旧时难觅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<User> pagableFollowings;
                do {
                    pagableFollowings = twitter.getFriendsList(twitter.getId(), cursor);
                    for (User user : pagableFollowings) {
                        listFriends.add(user); // ArrayList<User>
                    }
                } while ((cursor = pagableFollowings.getNextCursor()) != 0);
    
                // get followers
                cursor = -1;
                PagableResponseList<User> pagableFollowers;
                do {
                    pagableFollowers = twitter.getFollowersList(twitter.getId(), cursor);
                    for (User user : pagableFollowers) {
                        listFollowers.add(user); // ArrayList<User>
                    }
                } while ((cursor = pagableFollowers.getNextCursor()) != 0);
    
            } catch (TwitterException e) {
                printError(e);
            }
    
    0 讨论(0)
  • 2021-01-13 00:20

    You can use

    twitter.getFollowersIDs("username", cursor);
    

    http://twitter4j.org/javadoc/twitter4j/api/FriendsFollowersResources.html#getFollowersIDs-java.lang.String-long- which returns only 5000 user not all users. Also it is limited 15 times in 15 minutes.(https://dev.twitter.com/rest/reference/get/friends/ids)

    Also, you can use,

    twitter.getFollowersList("username", cursor);
    

    http://twitter4j.org/javadoc/twitter4j/api/FriendsFollowersResources.html#getFollowersList-java.lang.String-long- which is also limited with 20 user. Also it is limited 15 times in 15 minutes for user auth, 30 times in 15 minutes for app auth (https://dev.twitter.com/rest/reference/get/friends/list)

    For unlimited access, you can look at https://gnip.com/ or whitelisted user access of twitter.

    0 讨论(0)
  • 2021-01-13 00:25
    long lCursor = -1;
    IDs friendsIDs = twitter.getFriendsIDs(userID, lCursor);
    System.out.println(twitter.showUser(userID).getName());
    System.out.println("==========================");
    do
    {
      for (long i : friendsIDs.getIDs())
       {
           System.out.println("follower ID #" + i);
           System.out.println(twitter.showUser(i).getName());
       }
    }while(friendsIDs.hasNext());
    
    0 讨论(0)
  • 2021-01-13 00:28
             PagableResponseList<User> friendlist= twitter.getFriendsList(user.getScreenName(), -1);
    
             int sizeoffreindlist= friendlist.size();
    
             for(int i=0;i<sizeoffreindlist;i++)
             {
                 System.out.println(friendlist.get(i));
             }
    

    It will provide you a list of 20 friends as the default limit is 20

    0 讨论(0)
  • 2021-01-13 00:31

    You only need to do this:

    Twitter twitter = mTwitterApp.getTwitterInstance();
    long cursor = -1;
    List<User> users=twitter.getFriendsList(mTwitterApp.getUserID(), cursor);
    

    Here users is a list users who are your friends(you are following them). mTwitterApp.getUserID() is your login useris which is a long value.

    0 讨论(0)
  • 2021-01-13 00:39

    This will show the name of your friend's followers.

          User u1 = null ;
          long cursor = -1;
          IDs ids;
          System.out.println("Listing followers's ids.");
          do {
                  ids = twitter.getFollowersIDs("username", cursor);
              for (long id : ids.getIDs()) {
                  System.out.println(id);
                  User user = twitter.showUser(id);
                  System.out.println(user.getName());
              }
          } while ((cursor = ids.getNextCursor()) != 0);
    
    0 讨论(0)
提交回复
热议问题