I am testing an application which streams Twitter data.
List<Status> statuses = twitter.getHomeTimeline();
for (Status status : statuses) {
String rawJSON = TwitterObjectFactory.getRawJSON(status);
System.out.println(rawJSON);
}
This code works fine and prints something like this
{"in_reply_to_status_id_str":null,"in_reply_to_status_id":null,"coordinates":null,"created_at":"Tue Dec 05 13:29:02 +0000 2017","truncated":false,"in_reply_to_user_id_str":null,"source":"http://twitter.com/#!/download/ipad\" rel=\"nofollow\">Twitter for iPad</a>","retweet_count":1,"retweeted":false,"geo":null,"in_reply_to_screen_name":null,"is_quote_status":false,"entities":{"urls":[],"hashtags":[],"user_mentions":[],"symbols":[]}".....}
When I implement a live stream query setting the keywords to be tracked, it just prints null.
while(true){
Status status = queue.poll();
if (status == null) {
Thread.sleep(100);
}
if(status!=null){
String json = TwitterObjectFactory.getRawJSON(status);
System.out.println(json);
}
}
How can I solve this? Thank you.
from TwitterObjectFactory javadoc
getRawJSON(java.lang.Object obj) Returns a raw JSON form of the provided object. Note that raw JSON forms can be retrieved only from the same thread invoked the last method call and will become inaccessible once another method call
When you invoke any method to retrieve data after the one you used to retrieve statuses you are trying to convert, they will become inaccessible, in this case the method returns null instead of json. To solve, you have to convert them directly after crawling them.
Sadly, I don't know a work around for this issue, I even don't know why they do this!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With