我正在关注教程(http://davidcrowley.me/?p=435),但我无法使代码生效。
我已经设置了我的Twitter应用程序,并拥有消费者密钥,消费者密钥,访问令牌和访问令牌密钥。 我相信问题在于我加载库的方式,所以解释如何加载twitter4j库会很棒。我不知道是否需要加载整个.zip?我一直在努力教自己,但我感到非常失败。
package com.crowley.simplestream;
import twitter4j.FilterQuery;
import twitter4j.Status;
import twitter4j.StatusDeletionNotice;
import twitter4j.StatusListener;
import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
import twitter4j.User;
import twitter4j.conf.ConfigurationBuilder;
public class SimpleStream {
public static void main(String[] args) {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true);
cb.setOAuthConsumerKey("****");
cb.setOAuthConsumerSecret("****");
cb.setOAuthAccessToken("****");
cb.setOAuthAccessTokenSecret("****");
TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
StatusListener listener = new StatusListener() {
@Override
public void onException(Exception arg0) {
// TODO Auto-generated method stub
}
@Override
public void onDeletionNotice(StatusDeletionNotice arg0) {
// TODO Auto-generated method stub
}
@Override
public void onScrubGeo(long arg0, long arg1) {
// TODO Auto-generated method stub
}
@Override
public void onStatus(Status status) {
User user = status.getUser();
// gets Username
String username = status.getUser().getScreenName();
System.out.println(username);
String profileLocation = user.getLocation();
System.out.println(profileLocation);
long tweetId = status.getId();
System.out.println(tweetId);
String content = status.getText();
System.out.println(content +"\n");
}
@Override
public void onTrackLimitationNotice(int arg0) {
// TODO Auto-generated method stub
}
};
FilterQuery fq = new FilterQuery();
String keywords[] = {"ireland"};
fq.track(keywords);
twitterStream.addListener(listener);
twitterStream.filter(fq);
}
}