<?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; PullParser</title>
	<atom:link href="http://silveiraneto.net/tag/pullparser/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>JavaFX, Retrieving non XML/JSON data from clouds</title>
		<link>http://silveiraneto.net/2009/05/31/javafx-retrieving-non-xmljson-data-from-clouds/</link>
		<comments>http://silveiraneto.net/2009/05/31/javafx-retrieving-non-xmljson-data-from-clouds/#comments</comments>
		<pubDate>Sun, 31 May 2009 15:34:42 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[HttpRequest]]></category>
		<category><![CDATA[JavaFX]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[PullParser]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[Zembly]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=2631</guid>
		<description><![CDATA[Usuually on JavaFX we grab data using HttpRequest from external resources on formats like JSON or XML. I showed how to get it on the post Reading Twitter with JavaFX and how to parse it using PullParser on the post Parsing a XML sandwich with JavaFX. Another day I need to grab and interpret some [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><img class="size-full wp-image-2640 aligncenter" title="weather overcast icon from the Tango Project" src="http://silveiraneto.net/wp-content/uploads/2009/05/tango_weather-overcast.png" alt="tango weather overcast" width="200" height="200" /></p>
<p>Usuually on JavaFX we grab data using <a title="JAVAFX API" href="http://java.sun.com/javafx/1/docs/api/javafx.io.http/javafx.io.http.HttpRequest.html">HttpRequest</a> from external resources on formats like JSON or XML. I showed how to get it on the post <a title="Reading Twitter with JavaFX" href="http://silveiraneto.net/2009/01/04/reding-twitter-with-javafx/">Reading Twitter with JavaFX</a> and how to parse it using <a href="http://java.sun.com/javafx/1/docs/api/javafx.data.pull/javafx.data.pull.PullParser.html">PullParser</a> on the post <a title="Parsing XML sandwich with JavaFX" href="http://silveiraneto.net/2008/12/25/parsing-xml-sandwich-with-javafx/">Parsing a XML sandwich with JavaFX</a>.</p>
<p>Another day I need to grab and interpret some plain results, not in XML nor JSON, while consuming a REST service. In this case we don&#8217;t have a well structure data so the PullParser won&#8217;t help us.</p>
<p><strong>Example 1: Reading Raw Data</strong></p>
<p>In this example we&#8217;ll load a plain text file served in a remote location.</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;">var planetsRequest <span style="color: #339933;">=</span> HttpRequest <span style="color: #009900;">&#123;</span>
    location<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;http://silveiraneto.net/downloads/planets&quot;</span>;
    onInput<span style="color: #339933;">:</span> function<span style="color: #009900;">&#40;</span>stream<span style="color: #339933;">:</span> <span style="color: #003399;">InputStream</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        var buff <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedReader</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InputStreamReader</span><span style="color: #009900;">&#40;</span>stream<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
        var line <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span>;
        <span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>line <span style="color: #339933;">=</span> buff.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">!=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            println<span style="color: #009900;">&#40;</span>line<span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
planetsRequest.<span style="color: #006633;">enqueue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</pre>
</div>
</div>
<p>This will produce the output:</p>
<blockquote><p>Mercury<br />
Venus<br />
Earth<br />
Mars<br />
Jupiter<br />
Saturn<br />
Uranus<br />
Neptune</p></blockquote>
<p><strong>Example 2: </strong><strong>Discovering your IP Address</strong></p>
<p>In this example we&#8217;ll examine how to integrate a request of a remote data in a running graphical program.</p>
<p>The best way to know your real IP address is asking for a remote server to look which IP made that request. It&#8217;s like calling for a friend and asking him which number appeared in his mobile. =) This <a href="http://en.wikipedia.org/wiki/Server-side">server side</a> Python script prints the IP address of who requested the page.</p>
<div class="wp_syntax">
<div class="code">
<pre class="python python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Content-type: text/html&quot;</span>
<span style="color: #ff7700;font-weight:bold;">print</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">environ</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'REMOTE_ADDR'</span><span style="color: black;">&#93;</span></pre>
</div>
</div>
<p>In the client side, with JavaFX, we&#8217;ll load the remote value into a local variable. The <em>ip</em> is assigned with the value &#8220;&#8230;&#8221; and later the ipRequest will replace it with a String with the IP. The <em>bind</em> feature will automatically fix the GUI String text.</p>
<p>For the user he will see the ellipsis for a few seconds and so their IP.</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.stage.Stage</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.scene.Scene</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.scene.text.Text</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.io.http.HttpRequest</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.*</span>;
&nbsp;
var ip <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;...&quot;</span>;
&nbsp;
Stage <span style="color: #009900;">&#123;</span>
    title<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;What is my IP?&quot;</span> width<span style="color: #339933;">:</span> <span style="color: #cc66cc;">250</span> height<span style="color: #339933;">:</span> <span style="color: #cc66cc;">80</span>
    scene<span style="color: #339933;">:</span> Scene <span style="color: #009900;">&#123;</span>
        content<span style="color: #339933;">:</span> Text <span style="color: #009900;">&#123;</span>
            x<span style="color: #339933;">:</span> <span style="color: #cc66cc;">10</span>, y<span style="color: #339933;">:</span> <span style="color: #cc66cc;">30</span>
            content<span style="color: #339933;">:</span> bind <span style="color: #0000ff;">&quot;My IP is {ip}&quot;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
var ipRequest <span style="color: #339933;">=</span> HttpRequest <span style="color: #009900;">&#123;</span>
    location<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;http://silveiraneto.net/scripts/myip.py&quot;</span>;
    onInput<span style="color: #339933;">:</span> function<span style="color: #009900;">&#40;</span>stream<span style="color: #339933;">:</span> <span style="color: #003399;">InputStream</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        var buff <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedReader</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InputStreamReader</span><span style="color: #009900;">&#40;</span>stream<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
        ip <span style="color: #339933;">=</span> buff.<span style="color: #006633;">readLine</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>
ipRequest.<span style="color: #006633;">enqueue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</pre>
</div>
</div>
<p>You can try this <a href="http://silveiraneto.net/downloads/javafx/myip/MyIP.html">JavaFX applet here</a>.</p>
<p><strong>Example 3: </strong><strong>Reading Integer values</strong></p>
<p>Until now we handled just plain Strings. But in some cases you want to get number as non structured data. In this case you need to know previously which type the data is. In the case of a web service this probably will be described in a <a href="http://www.w3.org/TR/wsdl">WSDL file</a>.</p>
<p>Here I&#8217;m writing a very simple service script at <a href="http://zembly.com">Zembly</a>, a great platform for cloud computing. It&#8217;s called aplusb, it justs add the first parameter A to the second B.</p>
<div class="wp_syntax">
<div class="code">
<pre class="javascript javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>Parameters.<span style="color: #660066;">a</span> <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>amp;&amp;amp; <span style="color: #009900;">&#40;</span>Parameters.<span style="color: #660066;">b</span><span style="color: #339933;">!=</span> 0<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000066; font-weight: bold;">return</span> Parameters.<span style="color: #660066;">a</span><span style="color: #339933;">+</span>Parameters.<span style="color: #660066;">b</span>;
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p>The service is <a href="http://zembly.com/things/1827f696529d4e6f940c36e8e79bea1c#">published at Zembly here</a> where you can see more details on how to invoke it.</p>
<p>A simple way to invoke it on JavaFX and than getting the value as an Integer:</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.*</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.io.http.HttpRequest</span>;
&nbsp;
var a <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span>;
var b <span style="color: #339933;">=</span> <span style="color: #cc66cc;">200</span>;
var result <span style="color: #339933;">=</span> 0 on replace <span style="color: #009900;">&#123;</span>
    println<span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
var zemblyRequest <span style="color: #339933;">=</span> HttpRequest <span style="color: #009900;">&#123;</span>
    location<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;http://zembly.net/things/1827f696529d4e6f940c36e8e79bea1c;exec?a={a}&amp;amp;b={b}&quot;</span>;
    onInput<span style="color: #339933;">:</span> function<span style="color: #009900;">&#40;</span>stream<span style="color: #339933;">:</span> <span style="color: #003399;">InputStream</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        var buff <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedReader</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InputStreamReader</span><span style="color: #009900;">&#40;</span>stream<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
        result <span style="color: #339933;">=</span> <span style="color: #003399;">Integer</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>buff.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
zemblyRequest.<span style="color: #006633;">enqueue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</pre>
</div>
</div>
<p>The output will be:</p>
<blockquote><p>0<br />
300</p></blockquote>
<p>The first 0 is from the first assignment on the var result. The 300 is from the webservice itself.</p>
<p>The same approach can be used to convert the ASCII/Unicode result from the stream to the suitable type on a variable.</p>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2009/05/31/javafx-retrieving-non-xmljson-data-from-clouds/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

