title: Simple (Twitter + Streaming API + Java + OAuth) example
body: <p>In my quest to create a simple Java program to extract tweets from Twitter's streaming API, I have modified this (<a href="http://cotdp.com/dl/TwitterConsumer.java" rel="nofollow noreferrer">http://cotdp.com/dl/TwitterConsumer.java</a>) code snippet to work with the OAuth method. The result is the below code, which when executed, throws a Connection Refused Exception.</p>
  <p>I am aware of <a href="http://twitter4j.org/en/index.html" rel="nofollow noreferrer">Twitter4J</a> however I want to create a program that relies least on other APIs.</p>
  <p>I have done my research and it looks like the oauth.signpost library is suitable for Twitter's streaming API. I have also ensured my authentication details are correct. My Twitter Access level is 'Read-only'.</p>
  <p>I couldn't find a simple Java example that shows how to use the streaming API without relying on e.g. Twitter4j.</p>
  <pre class="lang-java prettyprint-override"><code>    import java.io.BufferedReader;
      import java.io.File;
      import java.io.FileWriter;
      import java.io.IOException;
      import java.io.InputStreamReader;
      
      import org.apache.http.HttpResponse;
      import org.apache.http.auth.AuthScope;
      import org.apache.http.auth.UsernamePasswordCredentials;
      import org.apache.http.client.methods.HttpGet;
      import org.apache.http.impl.client.DefaultHttpClient;
      
      import oauth.signpost.OAuthConsumer;
      import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
      
      /**
       * A hacky little class illustrating how to receive and store Twitter streams
       * for later analysis, requires Apache Commons HTTP Client 4+. Stores the data
       * in 64MB long JSON files.
       * 
       * Usage:
       * 
       * TwitterConsumer t = new TwitterConsumer(&quot;username&quot;, &quot;password&quot;,
       *      &quot;http://stream.twitter.com/1/statuses/sample.json&quot;, &quot;sample&quot;);
       * t.start();
       */
      public class TwitterConsumer extends Thread {
          //
          static String STORAGE_DIR = &quot;/tmp&quot;;
          static long BYTES_PER_FILE = 64 * 1024 * 1024;
          //
          public long Messages = 0;
          public long Bytes = 0;
          public long Timestamp = 0;
          
          private String accessToken = &quot;&quot;;
          private String accessSecret = &quot;&quot;;
          private String consumerKey = &quot;&quot;;
          private String consumerSecret = &quot;&quot;; 
            
          private String feedUrl;
          private String filePrefix;
          boolean isRunning = true;
          File file = null;
          FileWriter fw = null;
          long bytesWritten = 0;
          
          public static void main(String[] args) {
              TwitterConsumer t = new TwitterConsumer(
                  &quot;XXX&quot;, 
                  &quot;XXX&quot;,
                  &quot;XXX&quot;,
                  &quot;XXX&quot;,
                  &quot;http://stream.twitter.com/1/statuses/sample.json&quot;, &quot;sample&quot;);
              t.start();
          }
      
          public TwitterConsumer(String accessToken, String accessSecret, String consumerKey, String consumerSecret, String url, String prefix) {
              this.accessToken = accessToken;
              this.accessSecret = accessSecret;
              this.consumerKey = consumerKey;
              this.consumerSecret = consumerSecret;
              feedUrl = url;
              filePrefix = prefix;
              Timestamp = System.currentTimeMillis();
          }
      
          /**
           * @throws IOException
           */
          private void rotateFile() throws IOException {
              // Handle the existing file
              if (fw != null)
                  fw.close();
              // Create the next file
              file = new File(STORAGE_DIR, filePrefix + &quot;-&quot;
                      + System.currentTimeMillis() + &quot;.json&quot;);
              bytesWritten = 0;
              fw = new FileWriter(file);
              System.out.println(&quot;Writing to &quot; + file.getAbsolutePath());
          }
      
          /**
           * @see java.lang.Thread#run()
           */
          public void run() {
              // Open the initial file
              try { rotateFile(); } catch (IOException e) { e.printStackTrace(); return; }
              // Run loop
              while (isRunning) {
                  try {
                  
                      OAuthConsumer consumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret);
                      consumer.setTokenWithSecret(accessToken, accessSecret);
                      HttpGet request = new HttpGet(feedUrl);
                      consumer.sign(request);
                  
                      DefaultHttpClient client = new DefaultHttpClient();
                      HttpResponse response = client.execute(request);
                      BufferedReader reader = new BufferedReader(
                              new InputStreamReader(response.getEntity().getContent()));
                      while (true) {
                          String line = reader.readLine();
                          if (line == null)
                              break;
                          if (line.length() &gt; 0) {
                              if (bytesWritten + line.length() + 1 &gt; BYTES_PER_FILE)
                                  rotateFile();
                              fw.write(line + &quot;\n&quot;);
                              bytesWritten += line.length() + 1;
                              Messages++;
                              Bytes += line.length() + 1;
                          }
                      }
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
                  System.out.println(&quot;Sleeping before reconnect...&quot;);
                  try { Thread.sleep(15000); } catch (Exception e) { }
              }
          }
      }
      }
  </code></pre>