Get Latest Tweet


We are going to display the latest tweet that matches the user's search criteria.


1. Make a UI (User Interface) with a button and a text field for the search term (see below).



2. When the "Search" button is clicked, print "Tweet, Tweet".

3.Download these 2 jars, add them to your project, then add them to your project’s build path.

Apache Commons Logging        commons-logging-1.1.3.jar
Twitter4J                                 twitter4j-core-3.0.5

4. Use this method to get the latest tweet when the user clicks the button. Print the tweet to the console.

Make sure you import from twitter4j



private String getLatestTweet(String searchingFor) {

      Twitter twitter = new TwitterFactory().getInstance();

      AccessToken accessToken = new AccessToken(
            "2453751158-IVD2VGZsvwZiRKxNe3Gs2lMjg30nvSkV1xSuPFf",
            "vBa5PjKfuMTK1LLBG51nCUI9r5d6Ph5cAHrS73spg6Nvu");

      twitter.setOAuthConsumer("YqeZdD2hYxOKv4QOkmp0i2djN",
            "6XUB1r8KXBvd8Pk9HHW3NgphMxHvHWBLAr5TihnckMU0ttyGST");

      twitter.setOAuthAccessToken(accessToken);

      Query query = new Query(searchingFor);
      try {
            QueryResult result = twitter.search(query);
            return result.getTweets().get(0).getText();
      } catch (Exception e) {
            System.err.print(e.getMessage());
            return "What the heck is that?";
      }
}



5. Now display the contents of the latest tweet on the User Interface.