0

I'm using the twitter4j library in java to get a sample of random tweets from the public, however I'm only getting one tweet repeated over and over again.

public class FetchTweets {

private static String oAuthConsumerKey = "YOUR-CONSUMER-KEY-GOES-HERE";
private static String oAuthConsumerSecret = "YOUR-CONSUMER-SECRET-GOES-HERE";
private static String oAuthAccessToken = "YOUR-ACCESS-TOKEN-GOES-HERE";
private static String oAuthAccessTokenSecret = "YOUR-ACCESS-TOKEN-SECRET-GOES-HERE";

private static TwitterStream config() {


    ConfigurationBuilder configBuilder = new ConfigurationBuilder();
    configBuilder.setDebugEnabled(true)
    .setOAuthConsumerKey(oAuthConsumerKey)
    .setOAuthConsumerSecret(oAuthConsumerSecret)
    .setOAuthAccessToken(oAuthAccessToken)
    .setOAuthAccessTokenSecret(oAuthAccessTokenSecret);

    return new TwitterStreamFactory(configBuilder.build()).getInstance();

}

private static void getTweets(){

    TwitterStream twitterStream = config();

    StatusListener listener = new StatusListener(){

        @Override
        public void onException(Exception ex) {
            ex.printStackTrace();

        }

        @Override
        public void onStatus(Status status) {
            //System.out.println(status.getId() + " " + status.getText());
            try {
                writeTweetsToFile(status);
            } catch (InterruptedException | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            System.out.println("Status deletion notice " + 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);

        }

    };

    //TwitterStream twitterStream = config();
    twitterStream.addListener(listener);
    twitterStream.sample(); 

}
}

Here is a snippet of my code that streams the tweets

MWiesner
  • 8,868
  • 11
  • 36
  • 70
nillihc
  • 1
  • 1
  • Please share relevant code when asking a question. None of your code that you have provided can allow us to draw any conclusions of what you are doing wrong. (e.g. where is definition for method: `writeTweetsToFile()`?) – CraigR8806 Jan 13 '17 at 12:23
  • ok I edited my class above it now includes the config() method which just creates an instance of my TwitterStream with configuration parameters. I dont think writeTweetsToFile() has anything to do with the problem. I just write tweets to a file in that method – nillihc Jan 17 '17 at 13:19

0 Answers0