<?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>Silveira Neto &#187; blogging</title>
	<atom:link href="http://silveiraneto.net/tag/blogging/feed/" rel="self" type="application/rss+xml" />
	<link>http://silveiraneto.net</link>
	<description>the world is a pixel</description>
	<lastBuildDate>Sun, 08 Jan 2012 05:17:57 +0000</lastBuildDate>
	<language>pt-br</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>Reading Twitter with JavaFX</title>
		<link>http://silveiraneto.net/2009/01/04/reding-twitter-with-javafx/</link>
		<comments>http://silveiraneto.net/2009/01/04/reding-twitter-with-javafx/#comments</comments>
		<pubDate>Sun, 04 Jan 2009 22:56:55 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[blogging]]></category>
		<category><![CDATA[JavaFX]]></category>
		<category><![CDATA[jfx]]></category>
		<category><![CDATA[microblogging]]></category>
		<category><![CDATA[openjfx]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[RIA]]></category>
		<category><![CDATA[Social Networking]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=2064</guid>
		<description><![CDATA[Twitter is a social network and micro-blogging service that allow you to create and read tweets, 140 characters text-based posts. It&#8217;s becoming a popular tool to keep in touch with your friends, coworkers, bloggers, etc. Here we&#8217;ll create a very simple application that show us tweets related with a given word. Twitter offers a very [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><img class="size-full wp-image-2073 aligncenter" title="twitter bird" src="http://silveiraneto.net/wp-content/uploads/2009/01/twitter-bird.gif" alt="twitter bird" width="250" height="184" /></p>
<p><a title="Twitter Micro Blogging Service" href="http://twitter.com">Twitter</a> is a social network and micro-blogging service that allow you to create and read tweets, 140 characters text-based posts. It&#8217;s becoming a popular tool to keep in touch with your friends, coworkers, bloggers, etc. Here we&#8217;ll create a very simple application that show us tweets related with a given word.</p>
<p>Twitter offers a very simple and powerfull <a href="http://apiwiki.twitter.com/">REST API</a> which supports <a rel="nofollow" href="http://en.wikipedia.org/wiki/XML"><span>XML</span></a>, <a rel="nofollow" href="http://en.wikipedia.org/wiki/Json"><span>JSON</span></a>, and the <a rel="nofollow" href="http://en.wikipedia.org/wiki/RSS"><span>RSS</span></a> and <a rel="nofollow" href="http://en.wikipedia.org/wiki/Atom_%28standard%29">Atom</a> formats. As we are aiming just to read tweets we&#8217;ll use just the Search API.</p>
<p>We do that in three steps:</p>
<ol>
<li>Query Tweets</li>
<li>Parser the Atom result</li>
<li>Show tweets into a GUI</li>
</ol>
<p>Let&#8217;s see them in the order of dependence beetween them.</p>
<p><strong>Displaying a Tweet</strong></p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;">var gradient <span style="color: #339933;">=</span> LinearGradient <span style="color: #009900;">&#123;</span>
    startX<span style="color: #339933;">:</span> <span style="color: #cc66cc;">0.0</span>,
    startY<span style="color: #339933;">:</span> <span style="color: #cc66cc;">0.0</span>,
    endX<span style="color: #339933;">:</span> <span style="color: #cc66cc;">0.0</span>,
    endY<span style="color: #339933;">:</span> <span style="color: #cc66cc;">150.0</span>
    proportional<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">false</span>
    stops<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>Stop <span style="color: #009900;">&#123;</span>offset<span style="color: #339933;">:</span> <span style="color: #cc66cc;">0.0</span> color<span style="color: #339933;">:</span> <span style="color: #003399;">Color</span>.<span style="color: #006633;">DARKGRAY</span> <span style="color: #009900;">&#125;</span>,
        Stop <span style="color: #009900;">&#123;</span> offset<span style="color: #339933;">:</span> <span style="color: #cc66cc;">1.0</span> color<span style="color: #339933;">:</span> <span style="color: #003399;">Color</span>.<span style="color: #006633;">BLACK</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Tweet <span style="color: #000000; font-weight: bold;">extends</span> CustomNode <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> var image<span style="color: #339933;">:</span> <span style="color: #003399;">Image</span>;
    <span style="color: #000000; font-weight: bold;">public</span> var username<span style="color: #339933;">:</span> <span style="color: #003399;">String</span>;
    <span style="color: #000000; font-weight: bold;">public</span> var message<span style="color: #339933;">:</span> <span style="color: #003399;">String</span>;
    <span style="color: #000000; font-weight: bold;">public</span> override function create<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> Node <span style="color: #009900;">&#123;</span>
        var txt <span style="color: #339933;">=</span> Text <span style="color: #009900;">&#123;</span>
            x<span style="color: #339933;">:</span> <span style="color: #cc66cc;">65</span>  y<span style="color: #339933;">:</span> <span style="color: #cc66cc;">35</span> wrappingWidth<span style="color: #339933;">:</span> <span style="color: #cc66cc;">150</span> fill<span style="color: #339933;">:</span> <span style="color: #003399;">Color</span>.<span style="color: #006633;">WHITE</span>
            content<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;{message}&quot;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #003399;">Group</span> <span style="color: #009900;">&#123;</span>
            content<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
                <span style="color: #003399;">Rectangle</span> <span style="color: #009900;">&#123;</span>
                    width<span style="color: #339933;">:</span> <span style="color: #cc66cc;">220</span> height<span style="color: #339933;">:</span> txt.<span style="color: #006633;">boundsInLocal</span>.<span style="color: #006633;">height</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">40</span>
                    arcHeight<span style="color: #339933;">:</span> <span style="color: #cc66cc;">10</span> arcWidth<span style="color: #339933;">:</span> <span style="color: #cc66cc;">10</span> fill<span style="color: #339933;">:</span> gradient
                <span style="color: #009900;">&#125;</span>,
                ImageView <span style="color: #009900;">&#123;</span>
                    x<span style="color: #339933;">:</span> <span style="color: #cc66cc;">5</span> y<span style="color: #339933;">:</span> <span style="color: #cc66cc;">20</span> image<span style="color: #339933;">:</span> image
                <span style="color: #009900;">&#125;</span>,
                Text <span style="color: #009900;">&#123;</span>
                    x<span style="color: #339933;">:</span> <span style="color: #cc66cc;">65</span>  y<span style="color: #339933;">:</span> <span style="color: #cc66cc;">20</span> fill<span style="color: #339933;">:</span> <span style="color: #003399;">Color</span>.<span style="color: #006633;">BLACK</span> content<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;{username} said&quot;</span>
                <span style="color: #009900;">&#125;</span>,
                txt
            <span style="color: #009900;">&#93;</span>
        <span style="color: #009900;">&#125;</span>;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p>For example, <a title="P4bl0's Tweet" href="http://twitter.com/p4bl0/statuses/1095717367">this tweet</a> would become:</p>
<p style="text-align: center;"><img class="size-full wp-image-2070 aligncenter" title="tweet example" src="http://silveiraneto.net/wp-content/uploads/2009/01/tweet_example.png" alt="tweet example" width="220" height="100" /></p>
<p><strong>Parsing ATOM result</strong></p>
<p>In <a title="XML Sandwich JavaFX " href="http://silveiraneto.net/2008/12/25/parsing-xml-sandwich-with-javafx/">my last post about JavaFX</a> I showed how to parse XML documents (and make sandwiches) with JavaFX. Here we&#8217;ll use the Atom format, but use any other would be almost the same. Parsing XML or JSON documents with JavaFX is both very simple using the <a title="JavaFX API" href="http://java.sun.com/javafx/1/docs/api/javafx.data.pull/javafx.data.pull.PullParser.html">javafx.data.pull.PullParser</a> class.</p>
<p>A query output is a Atom XML document with several information. We are interested only in the fields that holds the avatar image, the message and the user name.</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;">var tweets <span style="color: #339933;">=</span> VBox <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
def parser <span style="color: #339933;">=</span> PullParser <span style="color: #009900;">&#123;</span>
    var avatar;
    var firstname;
    var text;
    documentType<span style="color: #339933;">:</span> PullParser.<span style="color: #006633;">XML</span>;
&nbsp;
    onEvent<span style="color: #339933;">:</span> function<span style="color: #009900;">&#40;</span>event<span style="color: #339933;">:</span> <span style="color: #003399;">Event</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>event.<span style="color: #006633;">type</span> <span style="color: #339933;">==</span> PullParser.<span style="color: #006633;">START_ELEMENT</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>event.<span style="color: #006633;">qname</span>.<span style="color: #006633;">name</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;link&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>event.<span style="color: #006633;">getAttributeValue</span><span style="color: #009900;">&#40;</span>QName<span style="color: #009900;">&#123;</span>name<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;rel&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;image&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    avatar <span style="color: #339933;">=</span> event.<span style="color: #006633;">getAttributeValue</span><span style="color: #009900;">&#40;</span>QName<span style="color: #009900;">&#123;</span>name<span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;href&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>event.<span style="color: #006633;">type</span> <span style="color: #339933;">==</span> PullParser.<span style="color: #006633;">END_ELEMENT</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>event.<span style="color: #006633;">qname</span>.<span style="color: #006633;">name</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;title&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    text <span style="color: #339933;">=</span> event.<span style="color: #006633;">text</span>;
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>event.<span style="color: #006633;">qname</span>.<span style="color: #006633;">name</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span>and<span style="color: #009900;">&#40;</span>event.<span style="color: #006633;">level</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                var names<span style="color: #339933;">:</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> event.<span style="color: #006633;">text</span>.<span style="color: #006633;">split</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #009900;">&#41;</span>;
                firstname <span style="color: #339933;">=</span> names<span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span>;
                insert Tweet <span style="color: #009900;">&#123;</span>
                        image<span style="color: #339933;">:</span> <span style="color: #003399;">Image</span> <span style="color: #009900;">&#123;</span>
                            url<span style="color: #339933;">:</span> avatar
                        <span style="color: #009900;">&#125;</span>
                        message<span style="color: #339933;">:</span> text
                        username<span style="color: #339933;">:</span> firstname
                    <span style="color: #009900;">&#125;</span> into tweets.<span style="color: #006633;">content</span>;
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p><strong>Querying Tweets</strong></p>
<p>We can get Atom results through url queries like that:</p>
<ul>
<li>Tweets containing the word Beatles <a href="http://search.twitter.com/search.atom?q=Beatles">http://search.twitter.com/search.atom?q=Beatles</a></li>
<li>Tweets from the user Silveira <a href="http://search.twitter.com/search.atom?q=from%3ASilveira">http://search.twitter.com/search.atom?q=from%3ASilveira</a></li>
<li>Tweets to the user Silveira <a href="http://search.twitter.com/search.atom?q=to%3ASilveira">http://search.twitter.com/search.atom?q=to%3ASilveira</a></li>
<li>Tweets containing the hashtag #CEJUG <a href="http://search.twitter.com/search.atom?q=to%23CEJUG">http://search.twitter.com/search.atom?q=to%23CEJUG</a></li>
</ul>
<p>Notice that queries should be URL encoded. We will use a additional parameters <strong>&amp;rpp=4</strong> to receive<strong> </strong>only 4 results per page. To know more about search queries read the <a title="Twitter Search API" href="http://apiwiki.twitter.com/Search+API+Documentation">Search API Documentation</a>. We get these results as  <a title="Java API InputStream" href="http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStream.html">InputStreams</a> making asynchronous HTTP requests using the <a title="JavaFX API" href="http://java.sun.com/javafx/1/docs/api/javafx.io.http/javafx.io.http.HttpRequest.html">javafx.io.http.HttpRequest</a> class, which it&#8217;s perfect to invoke   RESTful Web Services.</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;">word <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Beatles&quot;</span>;
var request <span style="color: #339933;">=</span> HttpRequest <span style="color: #009900;">&#123;</span>
    location<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;http://search.twitter.com/search.atom?q={word}&amp;amp;rpp=4&quot;</span>;
    onInput<span style="color: #339933;">:</span> function<span style="color: #009900;">&#40;</span>stream<span style="color: #339933;">:</span> java.<span style="color: #006633;">io</span>.<span style="color: #003399;">InputStream</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        parser.<span style="color: #006633;">input</span> <span style="color: #339933;">=</span> stream;
        parser.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
request.<span style="color: #006633;">enqueue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</pre>
</div>
</div>
<p><strong>Conclusion</strong></p>
<p>Here is the application running for the word &#8220;House&#8221;.</p>
<p style="text-align: center;"><img class="size-full wp-image-2074 aligncenter" title="twitter with javafx" src="http://silveiraneto.net/wp-content/uploads/2009/01/twitter_with_javafx.png" alt="twitter with javafx" width="220" height="458" /></p>
<p>Is not a complete Twitter client, as it&#8217;s not intended to be, but can show you how to handle a simple asynchronous call and handle Twitter documents. There&#8217;s already a few beta JavaFX Twitter clients like <a title="Tweetbox at Google Code" href="http://code.google.com/p/tweetbox/">Tweetbox</a> and <a title="TwitterFX at Project Kenai" href="http://projectkenai.com/projects/twitterfx">Twitterfx</a> and certanly others will appears.</p>
<p><strong>Download</strong></p>
<p>Sources and Netbeans project, <a href="http://silveiraneto.net/downloads/fxtwitter.tar.bz2">fxtwitter.tar.bz2</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2009/01/04/reding-twitter-with-javafx/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

