1

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?

Wikzo
  • 170
  • 2
  • 12

1 Answers1

4

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

Community
  • 1
  • 1
Ronak Agrawal
  • 438
  • 3
  • 13
  • Thanks a lot! I am still new to web development, but your solution really helped me :) BTW, do you know if it is possible to get ALL tweets without any kind of "track terms"? Right now, I search for tweets containing the word "Christmas", but can I get any arbitrary tweets (in real time)? "endpoint.trackTerms(Lists.newArrayList("Christmas"));" – Wikzo Dec 04 '15 at 10:36