Posting Twitter Updates using Java

As you may or may not know, I’m working on a Java based Twitter client recently. Sending and getting data from Twitter is really, really simple. Here is how to do it in Java.

Let’s assume that we are trying to post an update to twitter with the following data:

String username = "someone@example.com";
String password = "password";
String status = "Look! I'm twittering using Java";

Twitter uses basic HTTP authentication which requires you to send a username:password pair encoded in base64:

String cridentials = new sun.misc.BASE64Encoder().encode((username + ":" + password).getBytes());

Now let’s create a HTTP connection:

URL url = new URL("http://twitter.com/statuses/update.xml");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);

Authentication is activated like so:

conn.setRequestProperty ("Authorization", "Basic " + cridentials);

Now we can send the update by simply writing to conn’s output stream:

OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
wr.close();

Finally, if you wand you can grab and print out the XML response sent back to you by Twitter server:

BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line, output = "";
while ((line = rd.readLine()) != null)
{
System.out.println(output);
}

That’s it - that’s all you need to do.

Related Posts:

  • Posting Twitter Updates via Curl
  • Problems with the DOMParser (s4s-elt-character Error)
  • Need a Project
  • Twitter is useless to me.
  • Confession
  • Twitter With Me!
  • 5 Twitter Improvements I Would Like to See
  • My Twitter
  • The Twitter Threading Problem
  • Jaiku

  • 7 Responses to “Posting Twitter Updates using Java”

    1. Gravatar Wikke BELGIUM Says:

      That seems easier than a database connection/query )

      Posted using Mozilla Firefox Mozilla Firefox 1.5.0.3 on Windows Windows XP
    2. Gravatar Luke UNITED STATES Says:

      It really is. All you are doing is sending a HTTP request and receiving a response. As I illustrated before, you can do the same thing just using curl.

      Posted using Mozilla Firefox Mozilla Firefox 2.0.0.3 on Windows Windows XP
    3. Gravatar grey UNITED KINGDOM Says:

      Thank you for using my image on your twitter page, but would you mind providing a link to either the page it came from (http://www.wellingtongrey.net/miscellanea/archive/2007-04-28–slashdot -flowchart.html) or the main site (http://www.wellingtongrey.net/) instead of just linking to the image directly?

      Thank you,

      -Grey

      Posted using Mozilla Firefox Mozilla Firefox 2.0.0.3 on Mac OS Mac OS X
    4. Gravatar Luke UNITED STATES Says:

      Sure thing boss. Link changed. )

      Btw, he is referring to this image.

      Posted using Mozilla Firefox Mozilla Firefox 2.0.0.3 on Windows Windows XP
    5. Gravatar ashish UNITED STATES Says:

      I have tried the code and in the response it keeps returning my last post instead of updating the new status. Could this be just twitter?

      Posted using Mozilla Firefox Mozilla Firefox 1.0.7 on RedHat Linux RedHat Linux
    6. Gravatar Luke UNITED STATES Says:

      Remember to flush your output stream. It worked for me when I tried it.

      Posted using Mozilla Firefox Mozilla Firefox 2.0.0.3 on Windows Windows XP
    7. Gravatar Terminally Incoherent » Blog Archive » Problems with the DOMParser (s4s-elt-character Error) UNITED STATES Says:

      […] Pretty straightforward stuff - in fact, you probably find the same few lines in just about every single DOMParser tutorial out there. The xmlStream is an InputStream instance object with the XML data. Where do I get it from? I pull it off the Twitter as I described here. I tested this code before, and got the XML to print out in the console so my InputStream is not the issue here. Every time I called the parse method I got few dozen errors like this: […]

      Posted using WordPress WordPress 2.0.5

    Leave a Reply

    XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <pre lang=""> <em> <i> <strike> <strong>

    [Quote selected]