1

#test我需要使用 twitter4j在所有带有特定主题标签(比如说)的新推文上更新我的 java 应用程序。由于避免对 twitter REST API 进行连续轮询,我将使用 Twitter Stream API。

运行此代码,我期望:

  1. 运行代码
  2. 打开浏览器,转到 Twitter 并发布包含以下内容的推文#test
  3. 仅在我的应用程序上查看带有特定哈希码的推文的打印

但是..我在我的应用程序中看不到任何东西..(见编辑)

有人可以给我一些建议吗?

ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
  .setOAuthConsumerKey(CONSUMER_KEY)
  .setOAuthConsumerSecret(CONSUMER_SECRET)
  .setOAuthAccessToken(ACCESS_TOKEN)
  .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET);

TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();

StatusListener listener = new StatusListener() {
    @Override
    public void onStatus(Status status) {
        System.out.println("@" + status.getUser().getScreenName() + " - " + status.getUser().getId() + " - " + status.getText());
    }
    @Override
    public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
        System.out.println("Post Deletado:" + statusDeletionNotice.getStatusId());
    }
    @Override
    public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
        System.err.println("Limitação:" + numberOfLimitedStatuses);
    }
    @Override
    public void onScrubGeo(long userId, long upToStatusId) {
        System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
    }
    @Override
    public void onStallWarning(StallWarning warning) {
        System.out.println("Got stall warning:" + warning);
    }
    @Override
    public void onException(Exception ex) {
        ex.printStackTrace();
    }
};

List<String> queries = new ArrayList<String>();
queries.add("#test");

twitterStream.addListener(listener);
twitterStream.firehose(20);

String[] trackQueries = (String[]) queries.toArray(new String[queries.size()]);

FilterQuery filterQuery = new FilterQuery();
twitterStream.filter(filterQuery.track(trackQueries));

编辑:

在修复了一些错误后,我正在阅读 sys.out 中的一些内容,但是.. 在一些错误之后似乎是随机的东西:

[Tue Jul 02 14:58:30 CEST 2013]Establishing connection.
[Tue Jul 02 14:58:30 CEST 2013]Establishing connection.
[Tue Jul 02 14:58:31 CEST 2013]Connection established.
[Tue Jul 02 14:58:31 CEST 2013]Receiving status stream.
[Tue Jul 02 14:58:31 CEST 2013]Connection established.
[Tue Jul 02 14:58:31 CEST 2013]Receiving status stream.
[Tue Jul 02 14:58:31 CEST 2013]Stream closed.
[Tue Jul 02 14:58:31 CEST 2013]Stream closed.
[Tue Jul 02 14:58:31 CEST 2013]Waiting for 250 milliseconds
Stream closed.
Relevant discussions can be found on the Internet at:
    http://www.google.co.jp/search?q=a8fd061d or
    http://www.google.co.jp/search?q=00070a0c
TwitterException{exceptionCode=[a8fd061d-00070a0c a8fd061d-0007099d], statusCode=-1, message=null, code=-1, retryAfter=-1, rateLimitStatus=null, version=3.0.3}
    at twitter4j.StatusStreamBase.handleNextElement(StatusStreamBase.java:199)
    at twitter4j.StatusStreamImpl.next(StatusStreamImpl.java:57)
    at twitter4j.TwitterStreamImpl$TwitterStreamConsumer.run(TwitterStreamImpl.java:478)
Caused by: java.io.IOException: the end of the stream has been reached
    at twitter4j.StatusStreamBase.handleNextElement(StatusStreamBase.java:88)
    ... 2 more
Stream closed.
Relevant discussions can be found on the Internet at:
    http://www.google.co.jp/search?q=a8fd061d or
    http://www.google.co.jp/search?q=00070a0c
TwitterException{exceptionCode=[a8fd061d-00070a0c a8fd061d-0007099d], statusCode=-1, message=null, code=-1, retryAfter=-1, rateLimitStatus=null, version=3.0.3}
    at twitter4j.StatusStreamBase.handleNextElement(StatusStreamBase.java:199)
    at twitter4j.StatusStreamImpl.next(StatusStreamImpl.java:57)
    at twitter4j.TwitterStreamImpl$TwitterStreamConsumer.run(TwitterStreamImpl.java:478)
Caused by: java.io.IOException: the end of the stream has been reached
    at twitter4j.StatusStreamBase.handleNextElement(StatusStreamBase.java:88)
    ... 2 more
[Tue Jul 02 14:58:31 CEST 2013]Unhandled event: {"disconnect":{"code":7,"stream_name":"urbanspirit5-statuses1734930","reason":"admin logout"}}
[Tue Jul 02 14:58:32 CEST 2013]Establishing connection.
[Tue Jul 02 14:58:33 CEST 2013]Connection established.
[Tue Jul 02 14:58:33 CEST 2013]Receiving status stream.

然后是一些像这样的随机推文:

@markinhos_jm - 300044892 - CRAZY PARTY AGUARDEM!!!
@bindibba7 - 511548205 - ??????? .. ??????? = ?? ??? ????
@Tamirmdty - 1521605059 - Salah ngetik
@PAOLOves_you - 222755754 - Hirap ah. Text, Chat FB at Tweet? SIYET! #MedyoAZARitu
@agokichi_mmmm - 793252368 - ???DVD??????
4

1 回答 1

0

只有通过经销商选择的公司才能访问 twitter firehose。因此,除非您是这些公司之一,否则您希望使用sample()而不是firehose. 不过,您应该会看到一些日志。您确定您的记录器设置正确吗?

编辑: 阅读文档的这一部分并添加一个有效的 twitter4j.properties 文件。请注意,您必须在 twitter 上注册才能获得访问令牌。一旦你有了一个令牌并填写了配置文件,你就可以将它包含在你的类路径中。

于 2013-07-02T12:02:05.230 回答