[英]Java Twitter4j addListener not defined for type twitterStream
[英]twitter4J TwitterStream not getting imported
我正在尝试对 twitter4J 使用以下流示例:
package twitter4j.examples.stream;
import twitter4j.*;
import java.util.ArrayList;
import java.util.Arrays;
/**
* <p>This is a code example of Twitter4J Streaming API - filter method support.<br>
* Usage: java twitter4j.examples.stream.PrintFilterStream [follow(comma separated numerical user ids)] [track(comma separated filter terms)]<br>
* </p>
*
* @author Yusuke Yamamoto - yusuke at mac.com
*/
public final class PrintFilterStream {
/**
* Main entry of this application.
*
* @param args follow(comma separated user ids) track(comma separated filter terms)
* @throws TwitterException when Twitter service or network is unavailable
*/
public static void main(String[] args) throws TwitterException {
if (args.length < 1) {
System.out.println("Usage: java twitter4j.examples.PrintFilterStream [follow(comma separated numerical user ids)] [track(comma separated filter terms)]");
System.exit(-1);
}
TwitterStream twitterStream = new TwitterStreamFactory().getInstance().addListener(new StatusListener() {
@Override
public void onStatus(Status status) {
System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
}
@Override
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
}
@Override
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
}
@Override
public void onScrubGeo(long userId, long upToStatusId) {
System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
}
@Override
public void onStallWarning(StallWarning warning) {
System.out.println("Got stall warning:" + warning);
}
@Override
public void onException(Exception ex) {
ex.printStackTrace();
}
});
ArrayList<Long> follow = new ArrayList<Long>();
ArrayList<String> track = new ArrayList<String>();
for (String arg : args) {
if (isNumericalArgument(arg)) {
for (String id : arg.split(",")) {
follow.add(Long.parseLong(id));
}
} else {
track.addAll(Arrays.asList(arg.split(",")));
}
}
long[] followArray = new long[follow.size()];
for (int i = 0; i < follow.size(); i++) {
followArray[i] = follow.get(i);
}
String[] trackArray = track.toArray(new String[track.size()]);
// filter() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously.
twitterStream.filter(new FilterQuery(0, followArray, trackArray));
}
private static boolean isNumericalArgument(String argument) {
String args[] = argument.split(",");
boolean isNumericalArgument = true;
for (String arg : args) {
try {
Integer.parseInt(arg);
} catch (NumberFormatException nfe) {
isNumericalArgument = false;
break;
}
}
return isNumericalArgument;
}
}
在这一部分TwitterStream twitterStream = new TwitterStreamFactory().getInstance().addListener(new StatusListener() {
但我的 IDE 从来没有为我提供TwitterStream和TwitterStreamFactory 的任何导入我知道两者都应该在我导入的 twitter4j 的通用包中将 gradle 带入这个 Spring 应用程序。
此外,IDE 一直让我导入: org.apache.logging.log4j.status.StatusListener
或ch.qos.logback.core.status.StatusListener
但从来没有StatusListener
我怎样才能克服这个问题?
我没有在文档中找到它,但似乎您需要导入twitter4j-stream
工件而不是twitter4j-core
工件。
在我的 POM 中用这个替换就可以了:
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-stream</artifactId>
<version>4.0.7</version>
</dependency>
后者包含在前者中,但我在他们的示例项目中看到它们都包含在内。
您可以在Maven Central 中查看所有可用的工件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.