0

我正在使用 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)
4

1 回答 1

0

您只需要通过添加或删除您想要的位置来停止TwitterStream并更改您的位置。FilterQuery然后你需要重新启动TwitterStream. 它几乎是运行时的,但它可以工作(它在非常小的时间间隔内完成,所以你不会丢失twitter4j流媒体结果)。

于 2018-02-28T22:20:14.103 回答