本文整理汇总了Java中twitter4j.json.DataObjectFactory.getRawJSON方法的典型用法代码示例。如果您正苦于以下问题:Java DataObjectFactory.getRawJSON方法的具体用法?Java DataObjectFactory.getRawJSON怎么用?Java DataObjectFactory.getRawJSON使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twitter4j.json.DataObjectFactory
的用法示例。
在下文中一共展示了DataObjectFactory.getRawJSON方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import twitter4j.json.DataObjectFactory; //导入方法依赖的package包/类
/**
* Usage: java twitter4j.examples.json.SaveRawJSON
*
* @param args String[]
*/
public static void main(String[] args) {
Twitter twitter = new TwitterFactory().getInstance();
System.out.println("Saving public timeline.");
try {
new File("statuses").mkdir();
List<Status> statuses = twitter.getHomeTimeline();
for (Status status : statuses) {
String rawJSON = DataObjectFactory.getRawJSON(status);
String fileName = "statuses/" + status.getId() + ".json";
storeJSON(rawJSON, fileName);
System.out.println(fileName + " - " + status.getText());
}
System.out.print("\ndone.");
System.exit(0);
} catch (IOException ioe) {
ioe.printStackTrace();
System.out.println("Failed to store tweets: " + ioe.getMessage());
} catch (TwitterException te) {
te.printStackTrace();
System.out.println("Failed to get timeline: " + te.getMessage());
System.exit(-1);
}
}
示例2: onStatus
import twitter4j.json.DataObjectFactory; //导入方法依赖的package包/类
public void onStatus(Status status) {
this.status = status;
String json = DataObjectFactory.getRawJSON(status);
try {
assertNotNull(json);
Status statusFromJSON = DataObjectFactory.createStatus(json);
assertEquals(status, statusFromJSON);
} catch (TwitterException e) {
e.printStackTrace();
}
// System.out.println("got status from stream:" + status.toString());
assertNotNull(status.getText());
// System.out.println(status.getCreatedAt() + ":" + status.getText() + " from:" + status.getSource());
notifyResponse();
}
示例3: open
import twitter4j.json.DataObjectFactory; //导入方法依赖的package包/类
protected void open(final String tweetDump) {
openWriter(tweetDump + ".0");
listener = new StatusListener(){
public void onStatus(Status status) {
try {
String line = DataObjectFactory.getRawJSON(status);
writer.append(line);
writer.newLine();
currentFileSize += line.length();
if (maxJsonFileSize > 0){
if (currentFileSize >= maxJsonFileSize*1024){
closeWriter();
fileCounter++;
currentFileSize = 0;
openWriter(tweetDump + "." + fileCounter);
}
}
} catch (IOException e){
e.printStackTrace();
closeWriter();
}
}
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {}
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {}
public void onException(Exception ex) {
ex.printStackTrace();
}
@Override
public void onScrubGeo(long arg0, long arg1) {
}
@Override
public void onStallWarning(StallWarning arg0) {
//System.out.println(arg0.toString());
}
};
Runtime.getRuntime().addShutdownHook(new Shutdown(this));
}
示例4: start
import twitter4j.json.DataObjectFactory; //导入方法依赖的package包/类
@Override
public void start() {
// The StatusListener is a twitter4j API, which can be added to a Twitter
// stream, and will execute methods every time a message comes in through
// the stream.
StatusListener listener = new StatusListener() {
// The onStatus method is executed every time a new tweet comes in.
// We want to produce a new message when this happens.
public void onStatus(Status status) {
LOG.debug(status.getUser().getScreenName() + ": "
+ status.getText());
// Create a key based on the time and the message is the raw json of the tweet.
String key = String.valueOf(status.getCreatedAt().getTime());
String msg = DataObjectFactory.getRawJSON(status);
KeyedMessage<String, String> data = new KeyedMessage<String, String>(topic, key, msg);
producer.send(data);
}
// This listener will ignore everything except for new tweets
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {}
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {}
public void onScrubGeo(long userId, long upToStatusId) {}
public void onException(Exception ex) {
LOG.error("TwitterStreamException: {}", ex);
}
public void onStallWarning(StallWarning warning) {
LOG.warn("StallWarning: {}", warning);
}
};
LOG.debug("Setting up Twitter filter stream using the following keywords: {}",
(Object [])keywords);
// Set up the stream's listener (defined above),
twitterStream.addListener(listener);
// Create a filtered query and start the stream.
FilterQuery query = new FilterQuery().track(keywords);
twitterStream.filter(query);
}
示例5: main
import twitter4j.json.DataObjectFactory; //导入方法依赖的package包/类
public static void main(String[] args) throws TwitterException, IOException {
// Host and Port
MongoClient m = new MongoClient( "localhost" , 27017 );
// DB Name
DB db = m.getDB("Foo");
// Collection Name
final DBCollection coll = db.getCollection("Bar");
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true);
cb.setJSONStoreEnabled(true);
// Get them from twitter dev site
cb.setOAuthConsumerKey("");
cb.setOAuthConsumerSecret("");
cb.setOAuthAccessToken("");
cb.setOAuthAccessTokenSecret("");
TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
StatusListener listener = new StatusListener() {
@Override
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
//To change body of implemented methods use File | Settings | File Templates.
}
@Override
public void onScrubGeo(long l, long l2) {
//To change body of implemented methods use File | Settings | File Templates.
}
@Override
public void onStallWarning(StallWarning stallWarning) {
//To change body of implemented methods use File | Settings | File Templates.
}
@Override
public void onException(Exception e) {
//To change body of implemented methods use File | Settings | File Templates.
}
@Override
public void onTrackLimitationNotice(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatus(Status status) {
String json = DataObjectFactory.getRawJSON(status);
//System.out.println(json);
DBObject doc = (DBObject) JSON.parse(json);
coll.insert(doc);
}
};
FilterQuery fq = new FilterQuery();
// upto 400 CSV keywords,#hashtags
String tacking_strings[] = {"#android,app,#android app"};
fq.track(tacking_strings);
twitterStream.addListener(listener);
twitterStream.filter(fq);
}