Search code examples
javatwittertwitter-hbc

Twitter hbc API: how to get the individual tweet texts?


I am going to use Twitter for some semantic text analysis in a school class. I downloaded the Hosebird Client for Java and is running the FilterStreamExample.java: https://github.com/twitter/hbc/blob/master/hbc-example/src/main/java/com/twitter/hbc/example/FilterStreamExample.java

Running it, I get a lot of data about the users' profiles, their settings, background images, etc. I just want the tweeted text only. And maybe the location and username.

It may be a stupid question, but how do I make it only display the "texts" information? Right now, it just prints out everything.

// Do whatever needs to be done with messages
    for (int msgRead = 0; msgRead < 1000; msgRead++) {
      String msg = queue.take();
      System.out.println(msg);
    }

I could probably do a search for "text" in the strings themselves, but it seems a bit cumbersome. Isn't there any better way to do it?


Solution

  • The response from the twitter Streaming API is JSON String. Parse the string into JSON Object and get the value from the key "text"

    import org.json.*;
    for (int msgRead = 0; msgRead < 1000; msgRead++) {
          String msg = queue.take();
          JSONObject obj = new JSONObject(msg);
         String text= obj.getString("text");
          System.out.println(msg);
    }
    *Not Tested
    

    Refer the following for parsing JSON in Java How to parse JSON in Java