Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter4j - Status converted to JSON returns only null value

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.

like image 644
sirdan Avatar asked Jan 18 '26 18:01

sirdan


1 Answers

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!

like image 140
A. Harkous Avatar answered Jan 21 '26 08:01

A. Harkous