<?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>GWT Site &#187; Using GWT</title>
	<atom:link href="http://www.gwtsite.com/category/using-gwt/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gwtsite.com</link>
	<description>Learning about the Google Web Toolkit (GWT)</description>
	<lastBuildDate>Sun, 14 Feb 2010 19:43:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>GWT-Voices demo</title>
		<link>http://www.gwtsite.com/gwt-voices-demo/</link>
		<comments>http://www.gwtsite.com/gwt-voices-demo/#comments</comments>
		<pubDate>Thu, 06 Mar 2008 15:45:57 +0000</pubDate>
		<dc:creator>Chris Fong</dc:creator>
				<category><![CDATA[Using GWT]]></category>

		<guid isPermaLink="false">http://www.gwtsite.com/gwt-voices-demo/</guid>
		<description><![CDATA[


I just put up a new GWT demo inspired by Jonathan Coulton&#8217;s appearance on the TWiT 133 podcast last week.  I was going to write something up on how to use the gwt-voices library, but it is actually so simple to use, that there is really nothing new for me to explain.  The [...]<BR/><MAP name="bdv_RSS_Ad_060308034557"><AREA alt="Feed Ads By BidVertiser.com" shape="poly" coords="0,0,467,0,467,45,315,45,315,59,0,59" href="http://secure.bidvertiser.com/performance/bdv_rss_rd.dbm?pid=106624&amp;bid=269792&amp;PHS=060308034557&amp;click=1" target="_blank" /><AREA alt="Feed Ads By BidVertiser.com" shape="rect" coords="315,45,467,59" href="http://www.bidvertiser.com/bdv/bidvertiser/bdv_ref.dbm?Ref_PID=106624&amp;Ref_Option=main&amp;source=60972798" target="_blank" /></MAP><P><a href="http://secure.bidvertiser.com/performance/bdv_rss_rd.dbm?pid=106624&amp;bid=269792&amp;PHS=060308034557&amp;click=1" target="_blank"><IMG src="http://bdv.bidvertiser.com/BidVertiser.dbm?pid=106624&amp;bid=269792&amp;PHS=060308034557&amp;rssimage=1&amp;rSRC=2" border="0" usemap="#bdv_RSS_Ad_060308034557" /></a></P>]]></description>
			<content:encoded><![CDATA[<div class="right_image">
<a href="http://www.gwtsite.com/demos/musicplayer/MusicPlayer.php"><img border="0" src="http://www.gwtsite.com/images/musicplayer.jpg" alt="GWT Music Playert" /></a>
</div>
<p>I just put up a new <a href="http://www.gwtsite.com/demos/musicplayer/MusicPlayer.php">GWT demo</a> inspired by <a href="http://www.jonathancoulton.com">Jonathan Coulton&#8217;s</a> appearance on the <a href="http://twit.tv/133">TWiT 133</a> podcast last week.  I was going to write something up on how to use the <a href="http://code.google.com/p/gwt-voices/">gwt-voices</a> library, but it is actually so simple to use, that there is really nothing new for me to explain.  The only thing that was missing which I wanted to add, was a way to pause the songs. Currently the only options right now are play and stop.  </p>
<p>The widgets I used are from the <a href="http://www.gwt-ext.com">GWT-Ext</a> widget library.  Sanjiv has really done a nice job with the new 2.0 release.   </p>
<p><strong>Update:</strong> Thanks to Sanjiv for helping me fix the issue I had with IE.</p>
<p><!--adsense--></p>
<BR/><MAP name="bdv_RSS_Ad_060308034557"><AREA alt="Feed Ads By BidVertiser.com" shape="poly" coords="0,0,467,0,467,45,315,45,315,59,0,59" href="http://secure.bidvertiser.com/performance/bdv_rss_rd.dbm?pid=106624&amp;bid=269792&amp;PHS=060308034557&amp;click=1" target="_blank" /><AREA alt="Feed Ads By BidVertiser.com" shape="rect" coords="315,45,467,59" href="http://www.bidvertiser.com/bdv/bidvertiser/bdv_ref.dbm?Ref_PID=106624&amp;Ref_Option=main&amp;source=60972798" target="_blank" /></MAP><P><a href="http://secure.bidvertiser.com/performance/bdv_rss_rd.dbm?pid=106624&amp;bid=269792&amp;PHS=060308034557&amp;click=1" target="_blank"><IMG src="http://bdv.bidvertiser.com/BidVertiser.dbm?pid=106624&amp;bid=269792&amp;PHS=060308034557&amp;rssimage=1&amp;rSRC=2" border="0" usemap="#bdv_RSS_Ad_060308034557" /></a></P>]]></content:encoded>
			<wfw:commentRss>http://www.gwtsite.com/gwt-voices-demo/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Create an RSS app using the GWT History Mechanism</title>
		<link>http://www.gwtsite.com/create-an-rss-app-using-the-gwt-history-mechanism/</link>
		<comments>http://www.gwtsite.com/create-an-rss-app-using-the-gwt-history-mechanism/#comments</comments>
		<pubDate>Sun, 24 Feb 2008 15:37:10 +0000</pubDate>
		<dc:creator>Chris Fong</dc:creator>
				<category><![CDATA[Using GWT]]></category>

		<guid isPermaLink="false">http://www.gwtsite.com/create-an-rss-app-using-the-gwt-history-mechanism/</guid>
		<description><![CDATA[Over at developerlife, Nazmul Idris has posted a new tutorial titled, GWT Tutorial &#8211; Using History mechanism to create an RSS reader app.
This tutorial is meant to show you how to write an application that uses history. The Managing History and Hyperlinks tutorial gives you the background information you need, and shows you how to [...]<BR/><MAP name="bdv_RSS_Ad_240208033710"><AREA alt="Feed Ads By BidVertiser.com" shape="poly" coords="0,0,467,0,467,45,315,45,315,59,0,59" href="http://secure.bidvertiser.com/performance/bdv_rss_rd.dbm?pid=106624&amp;bid=269792&amp;PHS=240208033710&amp;click=1" target="_blank" /><AREA alt="Feed Ads By BidVertiser.com" shape="rect" coords="315,45,467,59" href="http://www.bidvertiser.com/bdv/bidvertiser/bdv_ref.dbm?Ref_PID=106624&amp;Ref_Option=main&amp;source=60972798" target="_blank" /></MAP><P><a href="http://secure.bidvertiser.com/performance/bdv_rss_rd.dbm?pid=106624&amp;bid=269792&amp;PHS=240208033710&amp;click=1" target="_blank"><IMG src="http://bdv.bidvertiser.com/BidVertiser.dbm?pid=106624&amp;bid=269792&amp;PHS=240208033710&amp;rssimage=1&amp;rSRC=2" border="0" usemap="#bdv_RSS_Ad_240208033710" /></a></P>]]></description>
			<content:encoded><![CDATA[<p>Over at developerlife, Nazmul Idris has posted a new tutorial titled, <a href="http://developerlife.com/theblog/?p=358">GWT Tutorial &#8211; Using History mechanism to create an RSS reader app</a>.</p>
<blockquote><p>This tutorial is meant to show you how to write an application that uses history. The Managing History and Hyperlinks tutorial gives you the <a href="http://www.integrascan.com"><a href="http://www.integrascan.com">background information</a></a> you need, and shows you how to &#8220;think&#8221; about History when building your GWT app. This tutorial takes the next step and shows you a sample application that uses history to load an RSS feed. There are other ways to pass parameters to GWT apps that donâ€™t involve using the History mechanism. However, the application in this tutorial will take this approach.</p></blockquote>
<p><img style="margin-left:5px" src="http://www.gwtsite.com/images/gwt_rss_reader.jpg" alt="developer life rss reader" /><br />
There are a bunch of useful techniques in this article, so check it out.</p>
<p><!--adsense--></p>
<BR/><MAP name="bdv_RSS_Ad_240208033710"><AREA alt="Feed Ads By BidVertiser.com" shape="poly" coords="0,0,467,0,467,45,315,45,315,59,0,59" href="http://secure.bidvertiser.com/performance/bdv_rss_rd.dbm?pid=106624&amp;bid=269792&amp;PHS=240208033710&amp;click=1" target="_blank" /><AREA alt="Feed Ads By BidVertiser.com" shape="rect" coords="315,45,467,59" href="http://www.bidvertiser.com/bdv/bidvertiser/bdv_ref.dbm?Ref_PID=106624&amp;Ref_Option=main&amp;source=60972798" target="_blank" /></MAP><P><a href="http://secure.bidvertiser.com/performance/bdv_rss_rd.dbm?pid=106624&amp;bid=269792&amp;PHS=240208033710&amp;click=1" target="_blank"><IMG src="http://bdv.bidvertiser.com/BidVertiser.dbm?pid=106624&amp;bid=269792&amp;PHS=240208033710&amp;rssimage=1&amp;rSRC=2" border="0" usemap="#bdv_RSS_Ad_240208033710" /></a></P>]]></content:encoded>
			<wfw:commentRss>http://www.gwtsite.com/create-an-rss-app-using-the-gwt-history-mechanism/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Working with PHP in GWT hosted mode</title>
		<link>http://www.gwtsite.com/working-with-php-in-gwt-hosted-mode/</link>
		<comments>http://www.gwtsite.com/working-with-php-in-gwt-hosted-mode/#comments</comments>
		<pubDate>Wed, 20 Feb 2008 06:53:15 +0000</pubDate>
		<dc:creator>Chris Fong</dc:creator>
				<category><![CDATA[Using GWT]]></category>

		<guid isPermaLink="false">http://www.gwtsite.com/working-with-php-in-gwt-hosted-mode/</guid>
		<description><![CDATA[
		   

Normally, when developing your GWT applications in hosted mode, the internal tomcat server will be used to serve up your application.  However, if want to talk to a server-side php script running on your own local web server, you will soon run into issues with the Single Origin Policy, since hosted [...]<BR/><MAP name="bdv_RSS_Ad_200208065315"><AREA alt="Feed Ads By BidVertiser.com" shape="poly" coords="0,0,467,0,467,45,315,45,315,59,0,59" href="http://secure.bidvertiser.com/performance/bdv_rss_rd.dbm?pid=106624&amp;bid=269792&amp;PHS=200208065315&amp;click=1" target="_blank" /><AREA alt="Feed Ads By BidVertiser.com" shape="rect" coords="315,45,467,59" href="http://www.bidvertiser.com/bdv/bidvertiser/bdv_ref.dbm?Ref_PID=106624&amp;Ref_Option=main&amp;source=60972798" target="_blank" /></MAP><P><a href="http://secure.bidvertiser.com/performance/bdv_rss_rd.dbm?pid=106624&amp;bid=269792&amp;PHS=200208065315&amp;click=1" target="_blank"><IMG src="http://bdv.bidvertiser.com/BidVertiser.dbm?pid=106624&amp;bid=269792&amp;PHS=200208065315&amp;rssimage=1&amp;rSRC=2" border="0" usemap="#bdv_RSS_Ad_200208065315" /></a></P>]]></description>
			<content:encoded><![CDATA[<div id="dzone_vote_widget">
		   <script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script>
</div>
<p>Normally, when developing your GWT applications in hosted mode, the internal tomcat server will be used to serve up your application.  However, if want to talk to a server-side php script running on your own local web server, you will soon run into issues with the <a href="http://code.google.com/support/bin/answer.py?answer=55198">Single Origin Policy</a>, since hosted mode runs on one port and the php script is running on another. In this tutorial, I will show you how I got around by running my GWT application and the php script on the same local web server.  To demonstrate this, I will use the GWT FileUpload widget with a php backend. </p>
<h4>Getting started</h4>
<p>This tutorial assumes you already have a local web server running that can handle php. I am personally using Apache via <a href="http://www.mamp.info/en/index.php">MAMP</a> on OS X.  <a href="http://www.apachefriends.org/en/xampp.html">XAMPP</a> is also another easy to setup alternative for Windows.</p>
<p>To start, I created a project called FileUploader and imported it into my IDE (Eclipse).  The code for the FileUploader module is directly taken from the example code in the FileUpload widget javadoc.  </p>
<p>The only modification I made was to the setAction method of the FormPanel object.</p>
<p>(<a target="_blank" href="http://code.google.com/p/gwtsite/source/browse/trunk/FileUploader/src/com/gwtsite/client/FileUploader.java"><em>com.gwtsite.client.FileUploader.java</em></a>)</p>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">final</span> FormPanel form = <span style="color: #000000; font-weight: bold;">new</span> FormPanel<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
form.<span style="color: #006600;">setAction</span><span style="color: #66cc66;">&#40;</span>GWT.<span style="color: #006600;">getModuleBaseURL</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">&quot;upload.php&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span></pre></div></div>

<p>Now when the user hits submit on the form, it will call the php script called <code>upload.php</code> to handle the file upload.</p>
<h4>Using -noserver</h4>
<p>To tell hosted mode not to use the internal tomcat server, we will pass the <code>-noserver</code> argument to the GWTShell.  But first we have to perform the following steps:</p>
<p>First, notice that the url of my application is: <code>http://localhost/com.gwtsite.FileUploader/Fileuploader.html</code></p>
<p>So I created a folder <code>called com.gwtsite.FileUploader</code> in my web server&#8217;s web documents folder.  Next, I compiled my application and copied the following files to this new folder:</p>
<ul>
<li>FileUploader.html</li>
<li>gwt.js</li>
<li>hosted.html</li>
<li>history.html</li>
<li>com.gwtsite.FileUploader.nocache.js</li>
</ul>
<p>Then for the program arguments, i set the <code>-noserver</code> parameter and since my web server is running on port 80 I also set the <code>-port</code> parameter to 80.</p>
<p><img src="http://www.gwtsite.com/images/fileuploader.jpg" alt="fileuploader parameters" /></p>
<h4>Finishing up</h4>
<p>The upload.php script for handling the upload was taken from the <a href="http://us3.php.net/features.file-upload">online php manual</a>.  I placed it in the same <code>com.gwtsite.FileUploader</code> folder on my web server. All it does is move the uploaded file from the temp location to the specified directory. Notice that <code>uploadFormElement</code> matches the name set on the GWT FileUpload widget.</p>
<p>(<a target="_blank" href="http://code.google.com/p/gwtsite/source/browse/trunk/FileUploader/src/com/gwtsite/public/upload.php"><em>upload.php</em></a>)</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #ff0000">$uploaddir</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'./'</span><span style="color: #66cc66;">;</span>
<span style="color: #ff0000">$uploadfile</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000">$uploaddir</span> <span style="color: #66cc66;">.</span> <span style="color: #000066;">basename</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$_FILES</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'uploadFormElement'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'name'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #000066;">echo</span> <span style="color: #ff0000;">'&lt;pre&gt;'</span><span style="color: #66cc66;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066;">move_uploaded_file</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$_FILES</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'uploadFormElement'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'tmp_name'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000">$uploadfile</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;File is valid, and was successfully uploaded.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">;</span>
<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;Possible file upload attack!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">;</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">...</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Once this is all wired up, I can now start up hosted mode from within my IDE and successfully upload files from within my GWT application.</p>
<h4>References</h4>
<ul>
<li><a href="http://code.google.com/support/bin/answer.py?answer=55200">How do I use my own server in hosted mode instead of GWT&#8217;s built-in Tomcat instance? </a></li>
<li><a href="http://code.google.com/support/bin/answer.py?answer=55198">What is the Single Origin Policy, and how does it affect GWT? </a></li>
<li><a href="http://us3.php.net/features.file-upload">Handling File Uploads</a></li>
</ul>
<p><!--adsense--></p>
<BR/><MAP name="bdv_RSS_Ad_200208065315"><AREA alt="Feed Ads By BidVertiser.com" shape="poly" coords="0,0,467,0,467,45,315,45,315,59,0,59" href="http://secure.bidvertiser.com/performance/bdv_rss_rd.dbm?pid=106624&amp;bid=269792&amp;PHS=200208065315&amp;click=1" target="_blank" /><AREA alt="Feed Ads By BidVertiser.com" shape="rect" coords="315,45,467,59" href="http://www.bidvertiser.com/bdv/bidvertiser/bdv_ref.dbm?Ref_PID=106624&amp;Ref_Option=main&amp;source=60972798" target="_blank" /></MAP><P><a href="http://secure.bidvertiser.com/performance/bdv_rss_rd.dbm?pid=106624&amp;bid=269792&amp;PHS=200208065315&amp;click=1" target="_blank"><IMG src="http://bdv.bidvertiser.com/BidVertiser.dbm?pid=106624&amp;bid=269792&amp;PHS=200208065315&amp;rssimage=1&amp;rSRC=2" border="0" usemap="#bdv_RSS_Ad_200208065315" /></a></P>]]></content:encoded>
			<wfw:commentRss>http://www.gwtsite.com/working-with-php-in-gwt-hosted-mode/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Dragging and dropping with gwt-dnd</title>
		<link>http://www.gwtsite.com/dragging-and-dropping-with-gwt-dnd/</link>
		<comments>http://www.gwtsite.com/dragging-and-dropping-with-gwt-dnd/#comments</comments>
		<pubDate>Thu, 14 Feb 2008 07:58:29 +0000</pubDate>
		<dc:creator>Chris Fong</dc:creator>
				<category><![CDATA[Using GWT]]></category>

		<guid isPermaLink="false">http://www.gwtsite.com/dragging-and-dropping-with-gwt-dnd/</guid>
		<description><![CDATA[
		   




Today, we are going to take a look at adding drag-and-drop to our GWT applications.  I&#8217;ve seen quite a few solutions for adding drag-and-drop, including a few tutorials that show how roll your own solution.  But why reinvent the wheel when there is a perfectly good drag and drop library [...]<BR/><MAP name="bdv_RSS_Ad_140208075829"><AREA alt="Feed Ads By BidVertiser.com" shape="poly" coords="0,0,467,0,467,45,315,45,315,59,0,59" href="http://secure.bidvertiser.com/performance/bdv_rss_rd.dbm?pid=106624&amp;bid=269792&amp;PHS=140208075829&amp;click=1" target="_blank" /><AREA alt="Feed Ads By BidVertiser.com" shape="rect" coords="315,45,467,59" href="http://www.bidvertiser.com/bdv/bidvertiser/bdv_ref.dbm?Ref_PID=106624&amp;Ref_Option=main&amp;source=60972798" target="_blank" /></MAP><P><a href="http://secure.bidvertiser.com/performance/bdv_rss_rd.dbm?pid=106624&amp;bid=269792&amp;PHS=140208075829&amp;click=1" target="_blank"><IMG src="http://bdv.bidvertiser.com/BidVertiser.dbm?pid=106624&amp;bid=269792&amp;PHS=140208075829&amp;rssimage=1&amp;rSRC=2" border="0" usemap="#bdv_RSS_Ad_140208075829" /></a></P>]]></description>
			<content:encoded><![CDATA[<div id="dzone_vote_widget">
		   <script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script>
</div>
<div style="float:right;margin-left:25px">
<!--adsense#med_rectangle-->
</div>
<p>Today, we are going to take a look at adding drag-and-drop to our GWT applications.  I&#8217;ve seen quite a few solutions for adding drag-and-drop, including a few tutorials that show how roll your own solution.  But why reinvent the wheel when there is a perfectly good drag and drop library like <a href="http://code.google.com/p/gwt-dnd">gwt-dnd</a>?  This library by Fred Sauer provides a whole host of cool features.  I took some time to play with it this past week and I will show you how to get started using the library by creating a simple shopping cart example.</p>
<p>To install, download the latest gwt-dnd jar file, add it to your build path and add the following line to your GWT application module file.</p>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #66cc66;">&lt;</span>inherits name=<span style="color: #ff0000;">'com.allen_sauer.gwt.dnd.gwt-dnd'</span>/<span style="color: #66cc66;">&gt;</span></pre></div></div>

<p>In this demo, we&#8217;re going to allow users to drop books into a shopping cart.  You can take a sneak peak of what I&#8217;m going to build  <a target="_blank" href="http://gwtsite.com/demos/ShoppingCart/ShoppingCartDemo.html">here</a>. The first domain object I will create is the Book class.  </p>
<p>(<a href="http://code.google.com/p/gwtsite/source/browse/trunk/ShoppingCart/src/com/gwtsite/dnd/client/Book.java"><em>com.gwtsite.dnd.client.Book</em></a>)</p>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #aaaadd; font-weight: bold;">Book</span> <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #aaaadd; font-weight: bold;">Composite</span> <span style="color: #000000; font-weight: bold;">implements</span> SourcesMouseEvents <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #aaaadd; font-weight: bold;">String</span> title<span style="color: #66cc66;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #aaaadd; font-weight: bold;">String</span> imgUrl<span style="color: #66cc66;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #aaaadd; font-weight: bold;">BigDecimal</span> price<span style="color: #66cc66;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #aaaadd; font-weight: bold;">Image</span> bookImage<span style="color: #66cc66;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> VerticalPanel mainPanel<span style="color: #66cc66;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #aaaadd; font-weight: bold;">Book</span><span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span> title, <span style="color: #aaaadd; font-weight: bold;">BigDecimal</span> price, <span style="color: #aaaadd; font-weight: bold;">String</span> imgUrl<span style="color: #66cc66;">&#41;</span>  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">title</span> = title<span style="color: #66cc66;">;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">price</span> = price<span style="color: #66cc66;">;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">imgUrl</span> = imgUrl<span style="color: #66cc66;">;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">mainPanel</span> = <span style="color: #000000; font-weight: bold;">new</span> VerticalPanel<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
    initWidget<span style="color: #66cc66;">&#40;</span>mainPanel<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
    bookImage = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">Image</span><span style="color: #66cc66;">&#40;</span>imgUrl<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
    mainPanel.<span style="color: #006600;">setHorizontalAlignment</span><span style="color: #66cc66;">&#40;</span>HasHorizontalAlignment.<span style="color: #006600;">ALIGN_CENTER</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
    mainPanel.<span style="color: #006600;">add</span><span style="color: #66cc66;">&#40;</span>bookImage<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
    mainPanel.<span style="color: #006600;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">Label</span><span style="color: #66cc66;">&#40;</span>title<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
    mainPanel.<span style="color: #006600;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">Label</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;$&quot;</span>+price<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
    mainPanel.<span style="color: #006600;">addStyleName</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;book&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> addMouseListener<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">MouseListener</span> listener<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      bookImage.<span style="color: #006600;">addMouseListener</span><span style="color: #66cc66;">&#40;</span>listener<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
   <span style="color: #66cc66;">&#125;</span>
   ...
  <span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Book is a GWT composite widget that will display the book&#8217;s title, image, and price.  Notice that it implements the <code>SourcesMouseEvents</code> interface.  This is required to make the book draggable.  In my implementation, I attach a <code>MouseListener</code> to the image field because the GWT Image widget already implements this interface and it will allow users to use the image as a drag handle to move the book around.</p>
<p>Dragging is handled by implementing the <code>DragController</code> interface. In this demo I am extending <code>PickupDragController</code>, which allows me to move the books around a specified boundary panel.  By default, the dragged widget will disappear when dropped on the drop target.  I don&#8217;t want this behavior so I&#8217;ll use a drag proxy instead.  This means I can drag a copy of the book&#8217;s image and leave the original book widget alone.  To achieve this, I set <code>setBehaviorDragProxy()</code> to true and override <code>PickupDragController</code>&#8217;s <code>newDragProxy()</code> method. </p>
<p>(<a href="http://code.google.com/p/gwtsite/source/browse/trunk/ShoppingCart/src/com/gwtsite/dnd/client/ShoppingCartDemo.java"><em>com.gwtsite.dnd.client.ShoppingCartDemo</em></a>)</p>

<div class="wp_syntax"><div class="code"><pre class="java">PickupDragController dragController = <span style="color: #000000; font-weight: bold;">new</span> PickupDragController<span style="color: #66cc66;">&#40;</span>containingPanel, <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">protected</span> Widget newDragProxy<span style="color: #66cc66;">&#40;</span>DragContext context<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    AbsolutePanel container = <span style="color: #000000; font-weight: bold;">new</span> AbsolutePanel<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
    DOM.<span style="color: #006600;">setStyleAttribute</span><span style="color: #66cc66;">&#40;</span>container.<span style="color: #006600;">getElement</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #ff0000;">&quot;overflow&quot;</span>, <span style="color: #ff0000;">&quot;visible&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">Iterator</span> iterator = context.<span style="color: #006600;">selectedWidgets</span>.<span style="color: #006600;">iterator</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span> iterator.<span style="color: #006600;">hasNext</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      Widget widget = <span style="color: #66cc66;">&#40;</span>Widget<span style="color: #66cc66;">&#41;</span> iterator.<span style="color: #006600;">next</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
      <span style="color: #aaaadd; font-weight: bold;">Book</span> book = <span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">Book</span><span style="color: #66cc66;">&#41;</span>widget<span style="color: #66cc66;">;</span>
      container.<span style="color: #006600;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">Image</span><span style="color: #66cc66;">&#40;</span>book.<span style="color: #006600;">getImageUrl</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">return</span> container<span style="color: #66cc66;">;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">;</span></pre></div></div>

<p>Then I make Book widgets draggable by calling <code>makeDraggable</code> on the drag controller.  Here, we pass to the method the book object (the draggable widget) and its image (the drag handle).</p>
<p>(<a href="http://code.google.com/p/gwtsite/source/browse/trunk/ShoppingCart/src/com/gwtsite/dnd/client/ShoppingCartDemo.java"><em>com.gwtsite.dnd.client.ShoppingCartDemo</em></a>)</p>

<div class="wp_syntax"><div class="code"><pre class="java">FlowPanel flowPanel = <span style="color: #000000; font-weight: bold;">new</span> FlowPanel<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
flowPanel.<span style="color: #006600;">addStyleName</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;flowPanel&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> i = <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">;</span> i <span style="color: #66cc66;">&lt;</span> books.<span style="color: #006600;">length</span><span style="color: #66cc66;">;</span> i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #aaaadd; font-weight: bold;">Book</span> book = books<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">;</span>
  dragController.<span style="color: #006600;">makeDraggable</span><span style="color: #66cc66;">&#40;</span>book, book.<span style="color: #006600;">getImage</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
  flowPanel.<span style="color: #006600;">add</span><span style="color: #66cc66;">&#40;</span>book<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>For the drop part of the demo, I created a <a href="http://code.google.com/p/gwtsite/source/browse/trunk/ShoppingCart/src/com/gwtsite/dnd/client/ShoppingCart.java">ShoppingCart</a> object which serves as the drop target.  Dropping is handled by a <code>DropController</code> object.  The library comes with several implementations, and I chose to extend the <code>SimpleDropController</code> class.  </p>
<p>(<a href="http://code.google.com/p/gwtsite/source/browse/trunk/ShoppingCart/src/com/gwtsite/dnd/client/CartDropController.java"><em>com.gwtsite.dnd.client.CartDropController</em></a>)</p>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CartDropController <span style="color: #000000; font-weight: bold;">extends</span> SimpleDropController <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> ShoppingCart cart<span style="color: #66cc66;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> CartDropController<span style="color: #66cc66;">&#40;</span>Widget dropTarget<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">super</span><span style="color: #66cc66;">&#40;</span>dropTarget<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
    cart = <span style="color: #66cc66;">&#40;</span>ShoppingCart<span style="color: #66cc66;">&#41;</span>dropTarget<span style="color: #66cc66;">;</span>
  <span style="color: #66cc66;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> onDrop<span style="color: #66cc66;">&#40;</span>DragContext context<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006600;">onDrop</span><span style="color: #66cc66;">&#40;</span>context<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
    <span style="color: #aaaadd; font-weight: bold;">Book</span> book = <span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">Book</span><span style="color: #66cc66;">&#41;</span>context.<span style="color: #006600;">draggable</span><span style="color: #66cc66;">;</span>
    cart.<span style="color: #006600;">add</span><span style="color: #66cc66;">&#40;</span>book<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
  <span style="color: #66cc66;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> onEnter<span style="color: #66cc66;">&#40;</span>DragContext context<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006600;">onEnter</span><span style="color: #66cc66;">&#40;</span>context<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
    cart.<span style="color: #006600;">addStyleName</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;enterCart&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
  <span style="color: #66cc66;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> onLeave<span style="color: #66cc66;">&#40;</span>DragContext context<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006600;">onLeave</span><span style="color: #66cc66;">&#40;</span>context<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
    cart.<span style="color: #006600;">removeStyleName</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;enterCart&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
  <span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>This controller accepts a GWT widget (the drop target) in its constructor and I had to implement the behavior for the following events: <code>onDrop</code>, <code>onEnter</code>, <code>onLeave</code>, and <code>onPreviewDrop</code>. The implementations of <code>onEnter</code>, and <code>onLeave</code> are simple.  Here, I just add and remove a css class which can be used to give a visual indicator when the the book gets dragged over the cart.  The <code>onDrop</code> method is used to add the dragged book object to the shopping cart and updating its display. I don&#8217;t use <code>onPreviewDrop</code> here, but it can be used to cancel a drop if some requirement isn&#8217;t met by throwing a <code>VetoDragException</code>.</p>
<p>Finally, to tie it all together, we need to register the drag controller with the drop controller like so:</p>
<p>(<a href="http://code.google.com/p/gwtsite/source/browse/trunk/ShoppingCart/src/com/gwtsite/dnd/client/ShoppingCartDemo.java"><em>com.gwtsite.dnd.client.ShoppingCartDemo</em></a>)</p>

<div class="wp_syntax"><div class="code"><pre class="java">ShoppingCart cart = <span style="color: #000000; font-weight: bold;">new</span> ShoppingCart<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
CartDropController dropController = <span style="color: #000000; font-weight: bold;">new</span> CartDropController<span style="color: #66cc66;">&#40;</span>cart<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
dragController.<span style="color: #006600;">registerDropController</span><span style="color: #66cc66;">&#40;</span>dropController<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span></pre></div></div>

<p>This is the <a href="http://gwtsite.com/demos/ShoppingCart/ShoppingCartDemo.html">demo</a> in action.  Hopefully this will get you started with using the gwt-dnd library.  The <a href="http://code.google.com/p/gwt-dnd">gwt-dnd</a> project page has several demos to show what the library is capable of.  I&#8217;ve only just touched the surface of what can be done, and there&#8217;s a lot of functionality out of the box that you can play with. So take a look around!  </p>
<BR/><MAP name="bdv_RSS_Ad_140208075829"><AREA alt="Feed Ads By BidVertiser.com" shape="poly" coords="0,0,467,0,467,45,315,45,315,59,0,59" href="http://secure.bidvertiser.com/performance/bdv_rss_rd.dbm?pid=106624&amp;bid=269792&amp;PHS=140208075829&amp;click=1" target="_blank" /><AREA alt="Feed Ads By BidVertiser.com" shape="rect" coords="315,45,467,59" href="http://www.bidvertiser.com/bdv/bidvertiser/bdv_ref.dbm?Ref_PID=106624&amp;Ref_Option=main&amp;source=60972798" target="_blank" /></MAP><P><a href="http://secure.bidvertiser.com/performance/bdv_rss_rd.dbm?pid=106624&amp;bid=269792&amp;PHS=140208075829&amp;click=1" target="_blank"><IMG src="http://bdv.bidvertiser.com/BidVertiser.dbm?pid=106624&amp;bid=269792&amp;PHS=140208075829&amp;rssimage=1&amp;rSRC=2" border="0" usemap="#bdv_RSS_Ad_140208075829" /></a></P>]]></content:encoded>
			<wfw:commentRss>http://www.gwtsite.com/dragging-and-dropping-with-gwt-dnd/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>How to access Web Services with GWT</title>
		<link>http://www.gwtsite.com/how-to-access-web-services-with-gwt/</link>
		<comments>http://www.gwtsite.com/how-to-access-web-services-with-gwt/#comments</comments>
		<pubDate>Wed, 06 Feb 2008 15:10:43 +0000</pubDate>
		<dc:creator>Chris Fong</dc:creator>
				<category><![CDATA[Using GWT]]></category>

		<guid isPermaLink="false">http://www.gwtsite.com/how-to-access-web-services-with-gwt/</guid>
		<description><![CDATA[
		   




There are a lot of new and interesting web services popping up on the web these days, and you may want to make use them in your GWT applications.  In this short guide, I will show you how to call these public apis from within your GWT app.
The main difficulty when [...]]]></description>
			<content:encoded><![CDATA[<div id="dzone_vote_widget">
		   <script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script>
</div>
<div style="float:right;margin: 0 10px 0 10px" id="ads">
<iframe src="http://rcm.amazon.com/e/cm?t=bogleg-20&#038;o=1&#038;p=8&#038;l=as1&#038;asins=0321501969&#038;fc1=000000&#038;IS2=1&#038;lt1=_blank&#038;lc1=0000FF&#038;bc1=FFFFFF&#038;bg1=FFFFFF&#038;f=ifr" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
</div>
<p>There are a lot of new and interesting web services popping up on the web these days, and you may want to make use them in your GWT applications.  In this short guide, I will show you how to call these public apis from within your GWT app.</p>
<p>The main difficulty when trying to talk to some web service on another server is getting past your web browser&#8217;s Same-Origin Policy.  This basically says that you may only make calls to the same domain as the page you are on.  This is good for security reasons, but inconvenient for you as a developer as it eliminates the use of GWT&#8217;s HTTP library functions to achieve what we want to do.  One way to get around this is to call a web service through a javascript tag  which bypasses this problem.  In his book, <a href="http://www.gwtsite.com/google-web-toolkit-applications-review/">Google Web Toolkit Applications</a>, Ryan Dewsbury actually explains this technique in more detail and provides a class called <a href="http://www.gwtsite.com/code/webservices/JSONRequest.java">JSONRequest</a> which handles all the hard work for us.  <a href="http://www.json.org">JSON</a> is one of the more popular data formats, so most web services support it.  Lets leverage Ryan&#8217;s code and take a quick look at how it works.</p>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> JSONRequest <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #993333;">void</span> get<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span> url, JSONRequestHandler handler<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #aaaadd; font-weight: bold;">String</span> callbackName = <span style="color: #ff0000;">&quot;JSONCallback&quot;</span>+handler.<span style="color: #006600;">hashCode</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
    get<span style="color: #66cc66;">&#40;</span> url+callbackName, callbackName, handler <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
  <span style="color: #66cc66;">&#125;</span>	
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #993333;">void</span> get<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span> url, <span style="color: #aaaadd; font-weight: bold;">String</span> callbackName, JSONRequestHandler handler <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    createCallbackFunction<span style="color: #66cc66;">&#40;</span> handler, callbackName <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
    addScript<span style="color: #66cc66;">&#40;</span>url<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
  <span style="color: #66cc66;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">native</span> <span style="color: #993333;">void</span> addScript<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span> url<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">/*-{
    var scr = document.createElement(&quot;script&quot;);
    scr.setAttribute(&quot;language&quot;, &quot;JavaScript&quot;);
    scr.setAttribute(&quot;src&quot;, url);
    document.getElementsByTagName(&quot;body&quot;)[0].appendChild(scr);
  }-*/</span><span style="color: #66cc66;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">native</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #993333;">void</span> createCallbackFunction<span style="color: #66cc66;">&#40;</span> JSONRequestHandler obj, <span style="color: #aaaadd; font-weight: bold;">String</span> callbackName<span style="color: #66cc66;">&#41;</span><span style="color: #808080; font-style: italic;">/*-{
    tmpcallback = function(j) {
      obj.@com.gwtsite.client.util.JSONRequestHandler::onRequestComplete(Lcom/google/gwt/core/client/JavaScriptObject;)(j);
    };
    eval( &quot;window.&quot; + callbackName + &quot;=tmpcallback&quot; );
  }-*/</span><span style="color: #66cc66;">;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>To make our request we call the <code>get</code> method with the web service url, and an implementation of the <a href="http://www.gwtsite.com/code/webservices/JSONRequestHandler.java">JSONRequestHandler</a> interface.  This interface has one method called <code>onRequestComplete(String json)</code>.  This is where you&#8217;ll handle the JSON formatted data once it comes back from the server.  When calling a service from within a <code>script</code> tag, we need to specify the name of a callback function in the request.  Most services let you specify the name yourself, so the first <code>get</code> method generates a callback name for you.  The <code>createCallback</code> method is a JSNI method that simply calls your <code>JSONRequestHandler</code> implementation when the call returns via the callback name. Note, if you use this class, to make sure and change the package name for the <code>JSONRequestHandler</code> call to the correct location. Finally, the <code>get</code> method will call the <code>addScript</code> function which is responsible for embedding the <code>&lt;script&gt;</code> tag on your page and setting its <code>src</code> attribute to the web service url.  </p>
<p>Now that I&#8217;ve described the technique to make the calls, you&#8217;ll need to find some APIs to use it with.  When looking at an API specification, make sure it has a parameter to return results in JSON format, and that it supports a callback parameter.  Here are some example web service APIs to start with.  </p>
<p><a href="http://code.google.com/apis/youtube/overview.html">Youtube GData API</a> &#8211; Get a list of my uploaded videos.</p>

<div class="wp_syntax"><div class="code"><pre class="java">JSONRequest.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">&quot;http://gdata.youtube.com/feeds/api/users/bogleg/uploads?alt=json-in-script&amp;callback=&quot;</span>, 
  <span style="color: #000000; font-weight: bold;">new</span> JSONRequestHandler<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> ... <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span></pre></div></div>

<p><a href="http://code.google.com/apis/socialgraph/">Google Social Graph API</a> &#8211; Get the social graph of bradfitz.com.</p>

<div class="wp_syntax"><div class="code"><pre class="java">JSONRequest.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">&quot;http://socialgraph.apis.google.com/lookup?&amp;q=bradfitz.com&amp;pretty=1&amp;fme=true&amp;callback=&quot;</span>, 
  <span style="color: #000000; font-weight: bold;">new</span> JSONRequestHandler<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> ... <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span></pre></div></div>

<p><a href="http://apidoc.digg.com/">Digg API</a> &#8211; Get the top stories from Digg.</p>

<div class="wp_syntax"><div class="code"><pre class="java">JSONRequest.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">&quot;http://services.digg.com/stories/top?appkey=http%3A%2F%2Fgwtsite.com&amp;type=javascript&amp;callback=&quot;</span>, 
    <span style="color: #000000; font-weight: bold;">new</span> JSONRequestHandler<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> ... <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span></pre></div></div>

<p><a href="http://www.geonames.org/export/">GeoNames webservice</a> &#8211; Get a zip-code location.</p>

<div class="wp_syntax"><div class="code"><pre class="java">JSONRequest.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">&quot;http://www.geonames.org/postalCodeLookupJSON?postalcode=94566&amp;country=US&amp;callback=&quot;</span>, 
  <span style="color: #000000; font-weight: bold;">new</span> JSONRequestHandler<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> ... <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span></pre></div></div>

<p>I hope you found this short tutorial useful.  If you want to learn more about GWT, make sure to <a href="http://feeds.feedburner.com/GwtSite">subscribe to my rss feed</a>.</p>
<p><img src="http://bdv.bidvertiser.com/bidvertiser/images/rss/pixel.gif?bdv_ver_code=BDV-999762-BDV"/><br />
<!--adsense--></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gwtsite.com/how-to-access-web-services-with-gwt/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
	</channel>
</rss>
