How to change keywords on twitter stream api using twitter4j?

安稳与你 提交于 2019-12-01 17:38:51

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!