Twitter4j:StatusListener 无法转换为 twitter4j.StreamListener

Twitter4j: StatusListener cannot be converted to twitter4j.StreamListener

我正在尝试使用 twitter4j 获取推文。我尝试了示例代码,我明白了为什么它不起作用 - 我无法转换 StatusListener。但我也无法解决我的问题。

有人知道如何解决这个问题吗?

import twitter4j.*;
import twitter4j.conf.ConfigurationBuilder;

import java.io.IOException;

public class Home
{
    public static void main (String args[]) throws TwitterException, IOException
    {
        ConfigurationBuilder cf = new ConfigurationBuilder();
        cf.setDebugEnabled(true)
                .setOAuthConsumerKey("")
                .setOAuthConsumerSecret("")
                .setOAuthAccessToken("")
                .setOAuthAccessTokenSecret("");

        StatusListener listener = new StatusListener(){
            public void onStatus(Status status) {
                System.out.println(status.getUser().getName() + " : " + status.getText());
            }
            public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {}
            public void onTrackLimitationNotice(int numberOfLimitedStatuses) {}
            public void onException(Exception ex) {
                ex.printStackTrace();
            }
        };
        TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
        twitterStream.addListener(listener);
        twitterStream.sample();
    }
}

您在实现 StatusListener

的匿名 class 中缺少两个方法实现

这应该是这样的:

StatusListener listener = new StatusListener() {

    public void onStatus(Status status) {
        System.out.println(status.getUser().getName() + " : " + status.getText());
    }

    public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {}
    public void onTrackLimitationNotice(int numberOfLimitedStatuses) {}

    @Override
    public void onScrubGeo(long l, long l1) {}

    @Override
    public void onStallWarning(StallWarning stallWarning) {}

    public void onException(Exception ex) {
        ex.printStackTrace();
    }
};