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的示例代码。
每次我尝试运行此程序时,我都会不断得到
“用法:java twitter4j.examples.PrintFilterStream [关注(用逗号分隔的数字用户ID)] [跟踪(用逗号分隔的过滤条件)]”
甚至我添加了track array。并用分隔,所以它可以拆分并添加到列表索引中。
还是我想念用法?
任何帮助表示赞赏。
public static void main(String[] args)
您的winning
类(请遵循命名约定,并以大写字母开头类名称),将命令行参数作为字符串数组接受 String[] args
if (args.length > 1) {
有了上面的代码,这种情况显然变得正确了,因为您提到了看到sysout “用法:java twitter4j.examples.PrintFilterStream [关注(逗号分隔的数字用户ID)] [track(逗号分隔的过滤条件)]”在输出和程序出口。
因此,您必须确保仅将一个参数作为输入传递给程序,因此args.length仅保留1
查看代码以后,您可以使用args将其与,分隔。因此,您对该程序的输入必须类似于
“ 1,2,3,4,5,6”
“ a,b,c,d,e,f”
您从命令行调用的程序应类似于:
java winning "1,2,3,4,5,6"
供参考和学习命令行参数 Command args
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句