我正在检索推文并将其存储在文本文件中。而不是以纯文本字符串格式检索推文,如何检索状态对象的JSON格式的推文。因为我将在检索完成后上传到Oracle数据库,这将需要将JSON的格式更改为.csv文件。用纯文本是不可能的。 我的代码低于任何帮助将不胜感激。
p public static void main(String[] args) {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("")
.setOAuthConsumerSecret("")
.setOAuthAccessToken("")
.setOAuthAccessTokenSecret("");
StatusListener listener = new StatusListener(){
public void onStatus(Status status) {
long iD = status.getId();
String username = status.getUser().getScreenName();
String tweetText = status.getText();
Date createdAt = status.getCreatedAt();
System.out.println(iD+" "+username+" "+tweetText+" "+createdAt);
String text = iD+" "+username+""+tweetText+" "+createdAt;
try {
PrintWriter pw = new PrintWriter(new FileWriter("TwitterTweetData.txt", true));
pw.println(text);
pw.close();
} catch ( IOException e ) {
e.printStackTrace();
}
}
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {}
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {}
public void onException(Exception ex) {
ex.printStackTrace();
}
public void onScrubGeo(long arg0, long arg1) {}
public void onStallWarning(StallWarning arg0) {}
};
TwitterStreamFactory tf = new TwitterStreamFactory(cb.build());
TwitterStream twitterStream = tf.getInstance();
twitterStream.addListener(listener);
FilterQuery filtre = new FilterQuery();
String[] keywordsArray = {"#Hello"}; // Removed Hashtags I'll be using
filtre.track(keywordsArray);
twitterStream.filter(filtre);
}
}
答案 0 :(得分:1)
这里写的是状态..:
PrintWriter pw = new PrintWriter(new FileWriter("TwitterTweetData.txt", true));
pw.println(status);
pw.close();
您的意思是将pw.println(status);
替换为
pw.println(DataObjectFactory.getRawJSON(status));
答案 1 :(得分:0)