gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 20:12:43 26 4
gpt4 key购买 nike

它说 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com