java - twitter4j中的Stream Closed错误如何解决

标签 java credentials twitter4j

它说 Stream Closed 错误

public class StreamAPI {

    public static void main(String[] args) {


        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true);
        cb.setOAuthConsumerKey("xxxx");
        cb.setOAuthConsumerSecret("xxx");
        cb.setOAuthAccessToken("xx-xx");
        cb.setOAuthAccessTokenSecret("xxx");
        cb.setUseSSL(true);
        cb.setUserStreamRepliesAllEnabled(true);


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

        AccessToken accestoken = new AccessToken("xx-xx", "xxx");

        twitterStream.setOAuthAccessToken(accestoken);
        StatusListener listener = new StatusListener() {

            public void onStatus(Status status) {
                System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
            }

            public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
                System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
            }

            public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
                System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
            }

            public void onScrubGeo(long userId, long upToStatusId) {
                System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
            }

            @Override
            public void onStallWarning(StallWarning stallWarning) {
                //To change body of implemented methods use File | Settings | File Templates.
            }

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

        FilterQuery fq = new FilterQuery();
        String keywords[] = {"France", "Germany"};

        fq.track(keywords);

        twitterStream.addListener(listener);
        twitterStream.filter(fq);

    }

}

我遇到了错误

   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

但相同的配置适用于 TwitterAPI

最佳答案

Twitter 给出了 following reasons why you may be disconnected :

  • A client establishes too many connections with the same credentials. When this occurs, the oldest connection will be terminated. This means you have to be careful not to run two reconnecting clients in parallel with the same credentials, or else they will take turns disconnecting each other.
  • A client stops reading data suddenly. If the rate of Tweets being read off of the stream drops suddenly, the connection will be closed.
  • A client reads data too slowly. Every streaming connection is backed by a queue of messages to be sent to the client. If this queue grows too large over time, the connection will be closed.
  • A streaming server is restarted. This is usually related to a code deploy and is not very frequent.
  • Twitter's network configuration changes. These events are extremely rare, and would represent load balancer restarts or network reconfigurations, for example.

您应该收到关于为什么您断开连接的信息,but not always :

Streams may be shut down for a variety of reasons. The streaming API will attempt to deliver a message indicating why a stream was closed. Note that if the disconnect was due to network issues or a client reading too slowly, it is possible that this message will not be received.

我的想法是,也许您读取数据的速度不够快 - 虽然我不能肯定地说。尝试实现 onStallWarning 以查看您是否收到任何失速警告,例如:

@Override
public void onStallWarning(StallWarning stallWarning) {
    System.out.println(stallWarning);
}

关于java - twitter4j中的Stream Closed错误如何解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17335342/

相关文章:

java - 使用 BufferedReader 转到文件中的下一行

java - 具有两个在 Pop 条件下失败的堆栈的 minstack 的实现

java - 推特搜索 API 始终返回空地理位置

java - 由于mapred.temp.dir,Hadoop 测试 BenchmarkThroughput 崩溃并出现 NullPointerException

powershell - 在 powershell 中使用备用凭据读取远程注册表项

windows - 获取windows代理用户名/密码

vue.js - 单击按钮后如何在 Nuxt 中使用私有(private) API key 发送请求?

java - 使用 Twitter4J 和 OAuth 访问 Twitter Stream API

Java - setFileFilter 问题

java - 我需要使用 HERE SDK 路由排除指向路线的点