How to change keywords on twitter stream api using twitter4j?

前端 未结 2 1151
南笙
南笙 2021-01-18 05:55

I am using twitter4j to connect to Stream API.

I understand that from this post, Change Twitter stream filter keywords without re-opening stream, there is no way to

相关标签:
2条回答
  • 2021-01-18 06:21

    You can do this by simply calling Filter(query) again, no need to call cleanUp() as calling filter(query) does this for you. Here's how I do this, and there is no need to stop/restart the stream !

    private TwitterStream twitterStream;
    
    private void filterTwitterStream() {
    
        if (conditionToStopStreaming) {
            if (null != twitterStream) {
                twitterStream.shutdown();
                twitterStream = null;
            }
        } else {
            if (twitterStream == null) {
                twitterStream = new TwitterStreamFactory(getConfiguration()).getInstance();
                twitterStream.addListener(getListener());
            }
    
            FilterQuery qry = new FilterQuery();
    
            String[] keywords = {......}
            qry.track(keywords);
    
            twitterStream.filter(qry);
        }
    }
    

    Where getConfiguration() returns my Configuration object and getListener() returns my defined StatusListener() object

    0 讨论(0)
  • 2021-01-18 06:38

    You need to cleanUp() the stream first and then open a new stream with filter(FilterQuery) method to change track terms.

    0 讨论(0)
提交回复
热议问题