Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get json user from json tweet

Tags:

java

twitter4j

I have a tweet that I had stored in a file. I want to extract the user as a json object and store it in a different file. The following results in java.lang.IllegalStateException: Apparently jsonStoreEnabled is not set to true.

String jsonStatus; //Content read from a file
Status status = TwitterObjectFactory.createStatus(jsonStatus); //no problem here
String jsonUser = TwitterObjectFactory.getRawJSON(status.getUser()); //Exception

How can I set jsonStoreEnabled to true? Or, is there another way of doing it? I don't have to stick with Twitter4j to create jsonUser. I tried json-simple, but the resulting String is not parsable with Twitter4j.

like image 939
mossaab Avatar asked Nov 18 '14 00:11

mossaab


1 Answers

TwitterObjectFactory has a variable called registeredAtleastOnce that if false you will get java.lang.IllegalStateException: Apparently jsonStoreEnabled is not set to true.. You can see this page TwitterObjectFactory So, if you don't want that error you will have to make once call to the Twitter API, for example for the first line of your file

ConfigurationBuilder cb = new ConfigurationBuilder();
             cb.setDebugEnabled(true)
             .setOAuthConsumerKey(Ckey)
             .setOAuthConsumerSecret(CkeySecret)
             .setOAuthAccessToken(AToken))
             .setOAuthAccessTokenSecret(AtokenSecret)
             .setJSONStoreEnabled(true);
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
String jsonStatus; //Content read from a file
Status status = TwitterObjectFactory.createStatus(jsonStatus); //your status
User u2 = t.showUser(status.getUser().getScreenName()); // you use the twitter object once
String jsonUser = TwitterObjectFactory.getRawJSON(status.getUser()); //now you won't get the error
System.out.print(jsonUser); //your happy json

In that way you wouldn't get that error

like image 74
FeanDoe Avatar answered Oct 11 '22 17:10

FeanDoe