How to get sample from twitter stream and print to screen

旧城冷巷雨未停 提交于 2019-12-11 06:27:50

问题


I'm trying to use the twitter4j library to get a random sample from the twitter stream and print the username and text of the tweet.

I'm not sure I understand the correct terminology as far as OAuth goes. Is this something I need to get for my account or only if accessing other private accounts?

Here's my code:

package tweet1;

import java.io.IOException;

import twitter4j.Status;
import twitter4j.StatusDeletionNotice;
import twitter4j.StatusListener;
import twitter4j.TwitterException;
import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
import twitter4j.auth.BasicAuthorization;

public class Twit4jEx1 {

    /**
     * @param args
     */
        // TODO Auto-generated method stub
        public static void main(String[] args) throws TwitterException, IOException{
//          BasicAuthorization ba1 = new BasicAuthorization("userName", "pword");
            ConfigurationBuilder cb = new ConfigurationBuilder();

            StatusListener listener = new StatusListener(){
                public void onStatus(Status status) {
                    System.out.println(status.getUser().getName() + " : " + status.getText());
                }
                public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {}
                public void onTrackLimitationNotice(int numberOfLimitedStatuses) {}
                public void onException(Exception ex) {
                    ex.printStackTrace();
                }
                @Override
                public void onScrubGeo(long arg0, long arg1) {
                    // TODO Auto-generated method stub

                }
            };
            TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
            // sample() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously.
            twitterStream.sample();
        }   
}

ConfigurationBuilder class

package tweet1;

import twitter4j.Twitter;
import twitter4j.TwitterFactory;
//import twitter4j.conf.ConfigurationBuilder;

public class ConfigurationBuilder {
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true)
      .setOAuthConsumerKey("***")
      .setOAuthConsumerSecret("***")
      .setOAuthAccessToken("***")
      .setOAuthAccessTokenSecret("***");
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();
}

When I run Twit4jEx1 I get the following:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "setDebugEnabled", = expected after this token

    at tweet1.ConfigurationBuilder.<init>(ConfigurationBuilder.java:9)
    at tweet1.Twit4jEx1.main(Twit4jEx1.java:21)

Any suggestions?


回答1:


You class ConfigurationBuilder is trying to run code in an initialization block instead of in a constructor or method.

I think you meant to have something like:

public class MyConfigurationBuilder {
    private ConfigurationBuilder cb; 
    private Twitter twitter;

    public MyConfigurationBuilder() {
      cb = new ConfigurationBuilder();
      cb.setDebugEnabled(true)
        .setOAuthConsumerKey("***")
        .setOAuthConsumerSecret("***")
        .setOAuthAccessToken("***")
        .setOAuthAccessTokenSecret("***");
      TwitterFactory tf = new TwitterFactory(cb.build());
      twitter = tf.getInstance();
    }
}

And then you can add appropriate getters or something. Make sure you use MyConfigurationBuilder in your main funciton.



来源:https://stackoverflow.com/questions/12097247/how-to-get-sample-from-twitter-stream-and-print-to-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!