我正在使用 twitter4j 从 twitter 获取按位置过滤的流,使用以下方法:
List<Location> locations = new ArrayList<>;
StatusesFilterEndpoint endpoint.locations(locations);
BasicClient client = new ClientBuilder()
.name(NAME)
.hosts(Constants.STREAM_HOST)
.endpoint(endpoint)
.authentication(auth)
.processor(new StringDelimitedProcessor(rowTweet))
.build();
client.connect();
while (isConnected()) {
String rowtweet = rowTweet.poll(5, TimeUnit.SECONDS);
if (rowtweet != null) {
Status status = DataObjectFactory.createStatus(rowtweet);
statusQueue.put(status);
}
}
这工作正常并根据位置过滤状态,我从这个位置获得了状态,但是我需要在运行时删除或添加位置,这可能吗?
我尝试使用添加位置,但出现以下异常:
public void addLocation(Location target) throws InterruptedException, TwitterException {
locations.add(target);
endpoint.locations(locations);
connect();
}
这个例外:
java.lang.IllegalStateException: There is already a connection thread running for TweetsByLocationStream, endpoint: /1.1/statuses/filter.json?delimited=length&stall_warnings=true
at com.twitter.hbc.httpclient.BasicClient.connect(BasicClient.java:98)
at com.newsry.streams.TweetsByLocationStream.connect(TweetsByLocationStream.java:77)
at com.newsry.streams.TweetsByLocationStream.addLocation(TweetsByLocationStream.java:129)
at com.newsry.engine.NewsryEngine.addLocation(NewsryEngine.java:183)
at com.newsry.streams.LocationStreamTest.main(LocationStreamTest.java:44)