流过滤器示例代码使用问题

用户名
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。并用分隔,所以它可以拆分并添加到列表索引中。

还是我想念用法?

任何帮助表示赞赏。

用户1933888
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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用列表流过滤器与for循环

来自分类Dev

Scala流过滤器行为

来自分类Dev

空检查流过滤器

来自分类Dev

Java 8 数组流过滤器

来自分类Dev

DirectShow示例过滤器:如何使用?

来自分类Dev

使用对象过滤器模块的问题

来自分类Dev

如何计算流过滤器中的匹配项?

来自分类Dev

在Java Lambda流过滤器中外部化谓词

来自分类Dev

流过滤器java中的if-else

来自分类Dev

Java 8迭代器流过滤器NoSuchElementException

来自分类Dev

流过滤器正则表达式

来自分类Dev

椭圆过滤器代码

来自分类Dev

Tableau API JavaScript过滤器示例

来自分类Dev

AngularJs双向排序过滤器示例?

来自分类Dev

使用strstarts过滤器以Java代码编写查询SPARQL

来自分类Dev

使用过滤器后重新激活代码

来自分类Dev

使用skimage导入过滤器时遇到问题

来自分类Dev

使用knockout.js 过滤器绑定的问题

来自分类Dev

DAX:使用多个过滤器计算总和的问题

来自分类Dev

如何使用 awk 解决日期过滤器的问题

来自分类Dev

尝试使用 Vanillajavascript 创建搜索框/过滤器的问题

来自分类Dev

Kafka 流过滤问题

来自分类Dev

关于交叉过滤器上的航空公司示例的基本问题

来自分类Dev

找出Java 8流过滤器是否已过滤某些内容的绝佳方法

来自分类Dev

1个列表的流过滤器基于另一个列表

来自分类Dev

在Reactor 2.0中处理流过滤器的正确方法是什么?

来自分类Dev

将谓词或函数用作Java流过滤器有什么区别?

来自分类Dev

如何处理对象不能满足在Java 8流过滤器

来自分类Dev

为什么不能对Java流过滤器应用谓词?