1

我正在开发一个使用 twitter4j API 与 Twitter 交互的应用程序。此 API 是否为传入的直接消息提供事件侦听器?

4

1 回答 1

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();

    }
}
于 2012-09-07T19:19:03.170 回答