Search code examples
javatwittertwitter4j

Twitter API Twitter4j getUserID


I am using Twitter4j to get tweets from the user's I'm following. Getting 1000 per time, but I'm a bit stuck on how I would include user ID and username in the out put.

Here is the code I'm using in order to get the tweets:

try {
            ResponseList<Status> a = twitter.getHomeTimeline(new Paging(1,1000));

            for (Status b: a){
                System.out.println(b.getText());
            }
        }

Does anybody know what I'd have to add in order to output the ID, Username and then the Tweet?

Thanks Z19


Solution

  • You can get the id and user name using following methods.

    User user = b.getUser() --> Return the user associated with the status. then using user.getId() and user.getName() you can get the id and user name.

    try {
                ResponseList<Status> a = twitter.getHomeTimeline(new Paging(1,1000));
    
                for (Status b: a){
                    long userId = b.getUser().getId();// user Id
                    String userName = b.getUser().getName(); // user name
                    String tweetText = b.getText(); // tweet
    
                    System.out.println(userId+" "+userName+" "+tweetText);
                }
            }
    

    For more info you can refer following links:

    1. Twitter 4j Status
    2. Twitter 4j User