如何使用 twitter4j 更改 twitter 流 api 上的关键字? [英] How to change keywords on twitter stream api using twitter4j?
问题描述
我正在使用 twitter4j 连接到 Stream API.
I am using twitter4j to connect to Stream API.
我从这篇文章中了解到,更改 Twitter 流过滤器关键字无需重新打开流,连接打开时无法更改关键字.我必须断开连接并更改过滤器谓词,然后重新连接.
I understand that from this post, Change Twitter stream filter keywords without re-opening stream, there is no way to change keywords while the connection is open. I have to disconnect and change the filter predicate and reconnect it.
我想知道是否有任何代码示例可以让我断开连接,更改关键字并重新连接?
I would like to know if there is any code sample that would allow me to disconnect it, change the keywords and reconnect it?
目前,我尝试在 onStatus() 下的 StatusListener 中执行此操作,在 X 时间过后,它将关键字更改为果汁".但是我没有办法关闭连接并重新连接到Stream API.
Currently, I tried to do this in the StatusListener under onStatus() where after an X amount of time has passed, it will change the keyword to "juice". But there is no method for me to close the connection and reconnect to Stream API.
if (diff>=timeLapse) {
StatusListener listener = createStatusListener();
track = "juice";
twitterStream = new TwitterStreamFactory().getInstance();
twitterStream.addListener(listener);
FilterQuery fq = new FilterQuery();
fq.track(new String[] {track});
startTime=System.currentTimeMillis();
twitterStream.filter(fq);
}
推荐答案
您需要先cleanUp() 流,然后使用filter(FilterQuery) 方法打开一个新的流来更改track term.
You need to cleanUp() the stream first and then open a new stream with filter(FilterQuery) method to change track terms.
这篇关于如何使用 twitter4j 更改 twitter 流 api 上的关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!