<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Threadbare Canvas Productions &#187; Twitter</title>
	<atom:link href="http://threadbarecanvas.com/category/java-web/twitter/feed/" rel="self" type="application/rss+xml" />
	<link>http://threadbarecanvas.com</link>
	<description>Online Journal of James Hogan the Web Developer</description>
	<lastBuildDate>Fri, 06 Jan 2012 09:59:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Twitter4j Twitter OAuth Example</title>
		<link>http://threadbarecanvas.com/java/twitter4j-twitter-oauth-example/</link>
		<comments>http://threadbarecanvas.com/java/twitter4j-twitter-oauth-example/#comments</comments>
		<pubDate>Mon, 08 Aug 2011 22:16:15 +0000</pubDate>
		<dc:creator>Siriquelle</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[OAuth]]></category>
		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://threadbarecanvas.com/?p=222</guid>
		<description><![CDATA[Today I&#8217;m going to show you some sample code regarding how to send a tweet to twitter using twitter4j and OAuth. The following code example is a command line twitter client that authenticates a user with oAuth and allows them to post a tweet. All you need to to create a project in your favourite [...]]]></description>
			<content:encoded><![CDATA[<p>Today I&#8217;m going to show you some sample code regarding how to send a tweet to twitter using twitter4j and OAuth. The following code example is a command line twitter client that authenticates a user with oAuth and allows them to post a tweet.</p>
<p>All you need to to create a project in your favourite IDE.. for ease of use. Add the twitter4j library to your build path. Compile and run. If you run into any problems shoot them into the comments and I&#8217;ll lend a hand. Happy hacking.</p>
<p><span class="Apple-style-span" style="font-family: Consolas, Monaco, monospace; font-size: 12px; line-height: 18px; white-space: pre;">package com.auth;</span></p>
<pre class="brush:java">import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.User;
import twitter4j.http.AccessToken;
import twitter4j.http.RequestToken;

public class Main {

    public static void main(String args[])
    {
        try
        {
            // The factory instance is re-useable and thread safe.
            Twitter twitter = new TwitterFactory().getInstance();
            twitter.setOAuthConsumer("pESR4xRk4glcGKMCryvdMQ", "ddu3r7IRp0I1luSFOW2iLwKP5pYTY03AZrAjJAVaj8");

            RequestToken requestToken = twitter.getOAuthRequestToken();
            AccessToken accessToken = null;
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            while (null == accessToken)
            {
                System.out.println("Open the following URL and grant access to your account:");
                System.out.println(requestToken.getAuthorizationURL());
                System.out.print("Enter the PIN(if aviailable) or just hit enter.[PIN]:");
                String pin = null;
                try
                {
                    pin = br.readLine();
                } catch (IOException ex)
                {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
                try
                {
                    if (pin.length() &gt; 0)
                    {
                        accessToken = twitter.getOAuthAccessToken(requestToken, pin);
                    } else
                    {
                        accessToken = twitter.getOAuthAccessToken();
                    }
                } catch (TwitterException te)
                {
                    if (401 == te.getStatusCode())
                    {
                        System.out.println("Unable to get the access token.");
                    } else
                    {
                        te.printStackTrace();
                    }
                }
            }
            //persist to the accessToken for future reference.
            storeAccessToken(twitter.verifyCredentials(), accessToken);
            Status status = twitter.updateStatus(args[0]);
            System.out.println("Successfully updated the status to [" + status.getText() + "].");
            System.exit(0);
        } catch (TwitterException ex)
        {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static void storeAccessToken(User user, AccessToken accessToken)
    {
        System.out.println(user.getScreenName());
    }
}</pre>
<p><map name='google_ad_map_222_d0b0fa17a4b5b092'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/222?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_222_d0b0fa17a4b5b092' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=222&amp;url= http%3A%2F%2Fthreadbarecanvas.com%2Fjava%2Ftwitter4j-twitter-oauth-example%2F' /></p>]]></content:encoded>
			<wfw:commentRss>http://threadbarecanvas.com/java/twitter4j-twitter-oauth-example/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>One Way Tweet a Java App</title>
		<link>http://threadbarecanvas.com/java/one-way-tweet-a-java-app/</link>
		<comments>http://threadbarecanvas.com/java/one-way-tweet-a-java-app/#comments</comments>
		<pubDate>Fri, 31 Jul 2009 09:33:31 +0000</pubDate>
		<dc:creator>Siriquelle</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Twitter]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[twitter api]]></category>

		<guid isPermaLink="false">http://threadbarecanvas.com/?p=115</guid>
		<description><![CDATA[One way tweet is a sending application for twitter. In this tutorial you will see/learn how to send stuff to twitter by writing a java desktop UI. You will create a new project in NetBeans and choose java application, and create a new package called org.me.owt. Then, create a new java source file and call it [...]]]></description>
			<content:encoded><![CDATA[<p>One way tweet is a sending application for twitter. In this tutorial you will see/learn how to send stuff to twitter by writing a java desktop UI. You will create a new project in NetBeans and choose java application, and create a new package called org.me.owt. </p>
<p>Then, create a new java source file and call it main.java, and another called tweet.java. Two simple classes and you&#8217;ve got yourself a twitter client. The end result will look like this:<br />
<a href="http://threadbarecanvas.com/wp-content/uploads/2009/07/owt.png"><img src="http://threadbarecanvas.com/wp-content/uploads/2009/07/owt.png" alt="One Way Tweet" title="One Way Tweet" width="309" height="312" class="aligncenter size-full wp-image-116" /></a></p>
<pre class="brush:java">
/*
 * Main.java
 *
 * Created on 23-Jul-2009, 11:57:04
 */
package org.me.owt;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author Siriquelle
 */
public class Main extends javax.swing.JFrame {

    private JButton button;

    /** Creates new form Main*/
    public Main() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        usernamelabel = new java.awt.Label();
        username = new java.awt.TextField();
        passwordlabel = new java.awt.Label();
        password = new java.awt.TextField();
        status = new java.awt.TextArea();
        tweet = new java.awt.Button();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("One Way Tweet");
        setBackground(new java.awt.Color(204, 255, 255));
        setBounds(new java.awt.Rectangle(0, 0, 0, 0));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setForeground(new java.awt.Color(204, 255, 255));
        setName("One Way Tweet"); // NOI18N
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(204, 255, 255));
        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setAlignmentX(getWidth() / 2);
        jPanel1.setAlignmentY(getHeight() / 2);

        usernamelabel.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        usernamelabel.setForeground(new java.awt.Color(0, 102, 102));
        usernamelabel.setName("usernameLabel"); // NOI18N
        usernamelabel.setText("Username");

        username.setForeground(new java.awt.Color(51, 51, 51));
        username.setName("username"); // NOI18N
        username.addFocusListener(new java.awt.event.FocusAdapter() {

            public void focusGained(java.awt.event.FocusEvent evt) {
                Main.this.focusGained(evt);
            }

            public void focusLost(java.awt.event.FocusEvent evt) {
                Main.this.focusLost(evt);
            }
        });

        passwordlabel.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        passwordlabel.setForeground(new java.awt.Color(0, 102, 102));
        passwordlabel.setName("passwordLabel"); // NOI18N
        passwordlabel.setText("Password");

        password.setEchoChar('*');
        password.setForeground(new java.awt.Color(51, 51, 51));
        password.setName("password"); // NOI18N
        password.addFocusListener(new java.awt.event.FocusAdapter() {

            public void focusGained(java.awt.event.FocusEvent evt) {
                Main.this.focusGained(evt);
            }

            public void focusLost(java.awt.event.FocusEvent evt) {
                Main.this.focusLost(evt);
            }
        });

        status.setForeground(new java.awt.Color(51, 51, 51));
        status.setName("status"); // NOI18N
        status.addFocusListener(new java.awt.event.FocusAdapter() {

            @Override
            public void focusGained(java.awt.event.FocusEvent evt) {
                Main.this.focusGained(evt);
            }

            @Override
            public void focusLost(java.awt.event.FocusEvent evt) {
                Main.this.focusLost(evt);
            }
        });

        tweet.setActionCommand("tweet");
        tweet.setBackground(new java.awt.Color(153, 255, 255));
        tweet.setFont(new java.awt.Font("Impact", 1, 48)); // NOI18N
        tweet.setForeground(new java.awt.Color(0, 102, 102));
        tweet.setLabel("tweet");
        tweet.setName("tweet"); // NOI18N
        tweet.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tweetMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(status, javax.swing.GroupLayout.PREFERRED_SIZE, 266, javax.swing.GroupLayout.PREFERRED_SIZE).addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false).addComponent(passwordlabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(usernamelabel, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE))).addComponent(tweet, javax.swing.GroupLayout.PREFERRED_SIZE, 266, javax.swing.GroupLayout.PREFERRED_SIZE)).addContainerGap()));
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addComponent(usernamelabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(passwordlabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(status, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(tweet, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap()));

        usernamelabel.getAccessibleContext().setAccessibleName("usernameLabel");
        username.getAccessibleContext().setAccessibleName("username");
        password.getAccessibleContext().setAccessibleName("password");
        status.getAccessibleContext().setAccessibleName("status");
        tweet.getAccessibleContext().setAccessibleName("tweet");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));

        getAccessibleContext().setAccessibleName("One Way Tweet");
        getAccessibleContext().setAccessibleDescription("One Way Tweet");

        pack();
    }// </editor-fold>

    private void tweetMouseClicked(java.awt.event.MouseEvent evt) {
        Tweet tw = new Tweet(username.getText(), password.getText(), status.getText());
        if (tw.sendTweet())
        {
            this.ShowMessageDialog("Tweet Win");
        } else
        {
            this.ShowMessageDialog("Tweet Fail");
        }
    }

    private void focusGained(java.awt.event.FocusEvent evt) {
        evt.getComponent().setBackground(new java.awt.Color(255, 255, 215));
    }

    private void focusLost(java.awt.event.FocusEvent evt) {
        evt.getComponent().setBackground(new java.awt.Color(255, 255, 255));
    }

    public void ShowMessageDialog(String msg) {
        final JFrame frame = new JFrame(msg);
        button = new JButton("OK");
        button.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseClicked(java.awt.event.MouseEvent evt) {
                frame.dispose();
            }
        });

        JPanel panel = new JPanel();
        panel.add(button);
        panel.add(button);
        frame.add(panel);
        frame.setSize(220, 80);
        frame.setLocation(220, 80);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Main().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JPanel jPanel1;
    private java.awt.TextField password;
    private java.awt.Label passwordlabel;
    private java.awt.TextArea status;
    private java.awt.Button tweet;
    private java.awt.TextField username;
    private java.awt.Label usernamelabel;
    // End of variables declaration
}
</pre>
<p>The following code is a class that handles the tweet. create a new java source file in the org.me.owt package and call it tweet.java. It&#8217;s methods are used to create and send the tweet and handle the response from the twitter api:</p>
<pre class="brush:java">
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.me.owt;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

/**
 *
 * @author Siriquelle
 */
public class Tweet {

    private String twitteruser;
    private String twitterpass;
    private String status;
    private String response;

    public Tweet(String u, String p, String s) {
        twitteruser = u;
        twitterpass = p;
        status = s;
    }

    public boolean sendTweet() {
        try
        {
            String encodedData = URLEncoder.encode(status, "UTF-8");

            String credentials = twitteruser + ":" + twitterpass;
            String encodecredentials = new sun.misc.BASE64Encoder().encode(credentials.getBytes());

            URLConnection connection = null;
            URL url = new URL("http://twitter.com/statuses/update.xml");
            connection = url.openConnection();
            connection.setRequestProperty("Authorization", "Basic " + encodecredentials);
            connection.setDoOutput(true);

            OutputStreamWriter output = new OutputStreamWriter(connection.getOutputStream());
            output.write("status=" + encodedData);
            output.close();

            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String decodedString;
            while ((decodedString = in.readLine()) != null)
            {
                response = decodedString;
            }
            in.close();
            return true;
        } catch (Exception e)
        {
            response = e.toString();
            return false;
        }
    }
}
</pre>
<p>And thats the code, right click your project and choose run. After you examine this code you&#8217;ll discover how, with a little parsing you can turn this application into a fully fledged thwirl competitor. :) If you&#8217;ve any questions I&#8217;d love a go at answering.</p>
<p>This application was created in netbeans using the swing UI builder. In a future post I will describe the process to building an application using the NetBeans IDE and it&#8217;s UI builders in design view.</p>
<p><map name='google_ad_map_115_d0b0fa17a4b5b092'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/115?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_115_d0b0fa17a4b5b092' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=115&amp;url= http%3A%2F%2Fthreadbarecanvas.com%2Fjava%2Fone-way-tweet-a-java-app%2F' /></p>]]></content:encoded>
			<wfw:commentRss>http://threadbarecanvas.com/java/one-way-tweet-a-java-app/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Universal Translator for your Tweets</title>
		<link>http://threadbarecanvas.com/java-web/universal-translator-for-your-tweets/</link>
		<comments>http://threadbarecanvas.com/java-web/universal-translator-for-your-tweets/#comments</comments>
		<pubDate>Tue, 21 Jul 2009 10:21:19 +0000</pubDate>
		<dc:creator>Siriquelle</dc:creator>
				<category><![CDATA[Java Web]]></category>
		<category><![CDATA[Twitter]]></category>
		<category><![CDATA[google translate]]></category>
		<category><![CDATA[netbeans]]></category>
		<category><![CDATA[twitter api]]></category>

		<guid isPermaLink="false">http://threadbarecanvas.com/?p=98</guid>
		<description><![CDATA[I thought this was going to be easy&#8230; SO much for preconceptions. Today, I&#8217;m going to give you the code to create a twitter translation App using, HTML, CSS, JavaScript and Java, the Twitter API and the Google Translate API. In a future tutorial I will demonstrate cross domain Ajax to get this Application to [...]]]></description>
			<content:encoded><![CDATA[<p>I thought this was going to be easy&#8230; SO much for preconceptions. Today, I&#8217;m going to give you the code to create a twitter translation App using, HTML, CSS, JavaScript and Java, the Twitter API and the Google Translate API. In a future tutorial I will demonstrate cross domain Ajax to get this Application to work totally on the client side but in the mean time you will need to have a Java enabled server to execute the server-side <abbr title="Java Server Page">JSP</abbr> that requests the API for twitter.</p>
<p>As you type your tweet the the JavaScript will analyze your keystrokes and check for when a space is entered assuming you are done typing a word, at this point the text is passed to Google for translation. When your done typing hit tweet and it&#8217;s off to twitter with your tweet.</p>
<p>This is what your app will look like when it&#8217;s finished:</p>
<p><a href="http://threadbarecanvas.com/wp-content/uploads/2009/07/translateApp.png"><img class="aligncenter size-full wp-image-99" title="translateApp" src="http://threadbarecanvas.com/wp-content/uploads/2009/07/translateApp.png" alt="translateApp" width="399" height="395" /></a></p>
<p style="text-align: center; font-size: 24px;"><strong><a href="http://threadbarecanvas.com/wp-content/uploads/2009/07/twitterLang.zip" target="_blank">Source</a><br />
</strong></p>
<p>The Folder structure you will need is similar to all other projects. You can download the source folder for this project and open it in NetBeans to get a better idea of how the end result will look. There are no special libraries you need to get this going just java.io.* and java.net.* .</p>
<p>This is the HTML you will need, (/index.html):</p>
<pre class="brush:html">&lt;!DOCTYPE html&gt;

&lt;html&gt;
    &lt;head&gt;
        &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
        &lt;link rel="Stylesheet" media="screen" href="assets/css/style.css" &gt;
        &lt;title&gt;Twitter Universal Translate&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;div id="content"&gt;
            &lt;div class="head"&gt;Twitter Multilanguage&lt;/div&gt;

            &lt;form&gt;
                &lt;label class="textlabel" for="username"&gt;Username&lt;/label&gt;
                &lt;input class="textinput" type="text" id="username" name="username" value=""&gt;

                &lt;label class="textlabel" for="password"&gt;Password&lt;/label&gt;
                &lt;input class="textinput" type="password" id="password" name="password" value=""&gt;

                &lt;label class="textlabel" for="lang"&gt;Translate To&lt;/label&gt;
                &lt;select class="select" id="lang" name="lang" onclick="translateTwo()" &gt;
                    &lt;option value="en"&gt;English&lt;/option&gt;
                    &lt;option value="fr"&gt;French&lt;/option&gt;
                    &lt;option value="it"&gt;Italian&lt;/option&gt;
                    &lt;option value="de"&gt;German&lt;/option&gt;
                &lt;/select&gt;

                &lt;label class="textlabel" for="textarea"&gt;Tweet&lt;/label&gt;
                &lt;textarea id="textarea" class="textarea" name="textarea" cols="5" rows="5" onblur="translateTwo()" onkeydown="translate(event)"&gt;&lt;/textarea&gt;

                &lt;input class="textinput" type="button" value="Tweet" onclick="callLoad()"/&gt;
            &lt;/form&gt;
            &lt;div class="infobar"&gt;“Translate your tweet into a chosen language as you type.”&lt;/div&gt;
            &lt;span id="status"&gt;&lt;/span&gt;
        &lt;/div&gt;

        &lt;script type="text/javascript" src="http://www.google.com/jsapi"&gt;&lt;/script&gt;
        &lt;script type="text/javascript" src="assets/js/jscript.js"&gt;&lt;/script&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre>
<p>This is the CSS you will need, (/assets/css/style.css):</p>
<pre class="brush:css">* {
    margin: 0;
    padding: 0;
}

/*///////////////////////////////////////////////////*/

#content{
    color:#333;
    width:350px;
    margin:100px auto;
    border:15px inset #66ffff;
    padding:10px;
    font-family:sans-serif;
    -moz-border-radius:10px;
}

.textlabel,textinput{
    display:block;
}

.textlabel{
    font-weight:bold;
}

.textlabel,.textinput, .textarea,.select{
    width:200px;
}

.textinput, .textarea,.select{
    padding:1px;
    margin-bottom:5px;
}

.textinput:focus, .textarea:focus{
    background-color:#ffffcc;
}

.head{
    font-size:32px;
    font-family:sans-serif;
    font-weight:bold;
    margin-bottom:5px;
}

.infobar{
    width:120px;
    float:right;
    position:relative;
    top:-140px;
    left:0px;
    padding: 3px;
    border:5px #ffff99 solid;
    background-color:#ffffcc;
    color:#003333;
    font-size:14px;
    font-style:oblique;
    text-align:center;
}</pre>
<p>This is the JavaScript you will need, (/assets/js/jscript.js):</p>
<pre class="brush:jscript">google.load("language", "1");

function translate(event){
    var textarea = document.getElementById("textarea");
    var lang = document.getElementById("lang");

    if(event.keyCode == 32){
        google.language.translate(textarea.value, "", lang.value, function(result) {
            if (!result.error) {
                var container = document.getElementById("textarea");
                container.value = result.translation + " ";
            }
        });
    }
}

function translateTwo(){
    var textarea = document.getElementById("textarea");
    var lang = document.getElementById("lang");
    google.language.translate(textarea.value, "", lang.value, function(result) {
        if (!result.error) {
            var container = document.getElementById("textarea");
            container.value = result.translation + " ";
        }
    });
}

function load(url, callback) {
    var xhr = new XMLHttpRequest();

    var username = document.getElementById("username").value;
    var password = document.getElementById("password").value;
    var status = document.getElementById("textarea").value;
    var auth = "tweet.jsp?username="+username+"&amp;password="+password+"&amp;status="+status;

    xhr.onreadystatechange = function() {
        if((xhr.readyState &lt; 4) || xhr.status !== 200) return;
        callback(xhr);
    };
    try{
        xhr.open('GET', auth, true);
        xhr.send();
    }catch(e){
        document.write(e);
    }
}

function callLoad(){
    load('tweet.jsp', function(xhr) {
        var joke = document.getElementById("status");
        var textarea = document.getElementById("textarea");
        joke.innerHTML = '<strong>OK</strong>';
        textarea.value = '';
    });
}</pre>
<p>This is the Java you will need, (/tweet.jsp):</p>
<pre class="brush:java">&lt;%@page import="java.net.*, java.io.*"%&gt;
&lt;%
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String status = request.getParameter("status");
        try {
            String twitteruser = username;
            String twitterpass = password;
            String encodedData = URLEncoder.encode(status, "UTF-8");

            String credentials = twitteruser + ":" + twitterpass;
            String encodecredentials = new sun.misc.BASE64Encoder().encode(credentials.getBytes());

            URLConnection connection = null;
            URL url = new URL("http://twitter.com/statuses/update.xml");
            connection = url.openConnection();
            connection.setRequestProperty("Authorization", "Basic " + encodecredentials);
            connection.setDoOutput(true);

            OutputStreamWriter output = new OutputStreamWriter(connection.getOutputStream());
            output.write("status=" + encodedData);
            output.close();

            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String decodedString;
            while ((decodedString = in.readLine()) != null) {
                out.println(decodedString);
            }
            in.close();

        } catch (Exception e) {
            System.out.println(e.toString());
        }

%&gt;</pre>
<p>Ideally a Servlet would be best suited to processing the response from twitter but for this project it&#8217;s OK. And there you go you can now tweet in multiple languages like James Bond might. yay!</p>
<p><map name='google_ad_map_98_d0b0fa17a4b5b092'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/98?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_98_d0b0fa17a4b5b092' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=98&amp;url= http%3A%2F%2Fthreadbarecanvas.com%2Fjava-web%2Funiversal-translator-for-your-tweets%2F' /></p>]]></content:encoded>
			<wfw:commentRss>http://threadbarecanvas.com/java-web/universal-translator-for-your-tweets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

