我正在开发一个使用twitter4j API与Twitter交互的应用程序。此API是否为提供直接消息提供了事件监听器?
答案 0 :(得分:5)
请在以下代码中找到实施方案
请添加twitter4j-core-2.2.6.jar& twitter4j-stream-2.2.6.jar到您的构建路径。
package com.apt.java.test.twitterdemo;
import java.util.List;
import twitter4j.DirectMessage;
import twitter4j.Status;
import twitter4j.StatusDeletionNotice;
import twitter4j.StatusListener;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
import twitter4j.User;
import twitter4j.UserList;
import twitter4j.UserStreamListener;
import twitter4j.auth.AccessToken;
import twitter4j.conf.ConfigurationBuilder;
public class TwitterDemo {
private TwitterStream twitterStream;
private void initConfiguration(){
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("YOUR CONSUMER KEY")
.setOAuthConsumerSecret("YOUR CONSUMER SECRET");
TwitterStreamFactory twitterStreamFactory = new TwitterStreamFactory(cb.build());
twitterStream = twitterStreamFactory.getInstance(new AccessToken("YOUR ACCESS TOKEN", "YOUR ACCESS TOKEN SECRET"));
twitterStream.addListener(userStreamListener);
twitterStream.user();
}
UserStreamListener userStreamListener = new UserStreamListener() {
@Override
public void onException(Exception arg0) {
// TODO Auto-generated method stub
}
@Override
public void onTrackLimitationNotice(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatus(Status status) {
// TODO Auto-generated method stub
System.out.println(status.getText());
}
@Override
public void onScrubGeo(long arg0, long arg1) {
// TODO Auto-generated method stub
}
@Override
public void onDeletionNotice(StatusDeletionNotice arg0) {
// TODO Auto-generated method stub
}
@Override
public void onUserProfileUpdate(User arg0) {
// TODO Auto-generated method stub
}
@Override
public void onUserListUpdate(User arg0, UserList arg1) {
// TODO Auto-generated method stub
}
@Override
public void onUserListUnsubscription(User arg0, User arg1, UserList arg2) {
// TODO Auto-generated method stub
}
@Override
public void onUserListSubscription(User arg0, User arg1, UserList arg2) {
// TODO Auto-generated method stub
}
@Override
public void onUserListMemberDeletion(User arg0, User arg1, UserList arg2) {
// TODO Auto-generated method stub
}
@Override
public void onUserListMemberAddition(User arg0, User arg1, UserList arg2) {
// TODO Auto-generated method stub
}
@Override
public void onUserListDeletion(User arg0, UserList arg1) {
// TODO Auto-generated method stub
}
@Override
public void onUserListCreation(User arg0, UserList arg1) {
// TODO Auto-generated method stub
}
@Override
public void onUnfavorite(User arg0, User arg1, Status arg2) {
// TODO Auto-generated method stub
}
@Override
public void onUnblock(User arg0, User arg1) {
// TODO Auto-generated method stub
}
@Override
public void onRetweet(User arg0, User arg1, Status arg2) {
// TODO Auto-generated method stub
}
@Override
public void onFriendList(long[] arg0) {
// TODO Auto-generated method stub
}
@Override
public void onFollow(User arg0, User arg1) {
// TODO Auto-generated method stub
}
@Override
public void onFavorite(User arg0, User arg1, Status arg2) {
// TODO Auto-generated method stub
}
@Override
public void onDirectMessage(DirectMessage message) {
// TODO Auto-generated method stub
System.out.println(message.getText());
}
@Override
public void onDeletionNotice(long arg0, long arg1) {
// TODO Auto-generated method stub
}
@Override
public void onBlock(User arg0, User arg1) {
// TODO Auto-generated method stub
}
};
public static void main(String[] args){
TwitterDemo demo = new TwitterDemo();
demo.initConfiguration();
}
}