import twitter4j.FilterQuery;
import twitter4j.StallWarning;
import twitter4j.Status;
import twitter4j.StatusDeletionNotice;
import twitter4j.StatusListener;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
import twitter4j.conf.ConfigurationBuilder;
import java.util.ArrayList;
import java.util.Arrays;
public class winning {
public static ConfigurationBuilder cb;
public static TwitterFactory tf;
public static twitter4j.Twitter tw;
public static void main(String[] args) throws TwitterException {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("")
.setOAuthConsumerSecret("")
.setOAuthAccessToken("-")
.setOAuthAccessTokenSecret("");
tf= new TwitterFactory(cb.build());
tw= tf.getInstance();
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);
}
StatusListener listener = 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();
}
};
TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
twitterStream.addListener(listener);
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;
}
}
这是来自twitter4j的示例代码。
每次我试着跑这个我一直在
&#34;用法:java twitter4j.examples.PrintFilterStream [follow(逗号分隔的数字用户ID)] [track(以逗号分隔的过滤条款)]&#34;
即使我添加了track array并将其分开,因此可以将其拆分并添加到列表索引中。
还是我错过了解用法?
感谢任何帮助。
答案 0 :(得分:0)
public static void main(String[] args)
你winning
类(请遵循命名约定并使用大写字母开始类名),接受命令行参数作为字符串 String[] args
的数组
if (args.length > 1) {
你有上面的代码,这个条件显然变成了现实,因为你提到你看到sysout &#34;用法:java twitter4j.examples.PrintFilterStream [follow(逗号分隔的数字用户ID)] [跟踪(逗号分隔的过滤条款)]&#34; 输出和程序退出。
因此,您必须确保仅将 一个 参数作为程序的输入传递,因此args.length仅保留 1 < / p>
查看您的代码,稍后使用 args 将其拆分为,。 因此,您对程序的输入必须类似于
&#34; 1,2,3,4,5,6-&#34;
&#34; A,B,C,d,E,F&#34;
从命令行调用程序应该类似于:
java winning "1,2,3,4,5,6"
供您参考和学习 Command line arguments Command args