<?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>Programming Tips &#38;  Tutorials</title>
	<atom:link href="http://ericosgood.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://ericosgood.com</link>
	<description></description>
	<lastBuildDate>Tue, 17 Apr 2012 07:10:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>MetaPlayground &#8211; an Isosurface and Marching Cubes Demo</title>
		<link>http://ericosgood.com/prog/grapics/metaplayground-an-isosurface-and-marching-cubes-demo/</link>
		<comments>http://ericosgood.com/prog/grapics/metaplayground-an-isosurface-and-marching-cubes-demo/#comments</comments>
		<pubDate>Thu, 17 Mar 2011 23:42:31 +0000</pubDate>
		<dc:creator>eosgood</dc:creator>
				<category><![CDATA[Computer Graphics]]></category>

		<guid isPermaLink="false">http://ericosgood.com/?p=349</guid>
		<description><![CDATA[The goal of this project was to implement marching cubes, GLUI and output the extracted surface as a mesh file.  <a href="http://ericosgood.com/prog/grapics/metaplayground-an-isosurface-and-marching-cubes-demo/"><div class="read-more">Read more &#8250;</div><!-- end of .read-more --></a>]]></description>
			<content:encoded><![CDATA[<div class="AWD_facebook_likebutton "><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fericosgood.com%2Fprog%2Fgrapics%2Fmetaplayground-an-isosurface-and-marching-cubes-demo%2F&amp;send=false&amp;layout=standard&amp;width=300&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=&amp;height=35&ref=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:300px; height:35px;" allowTransparency="true"></iframe></div><h3><span class="Apple-style-span" style="font-size: 18px; line-height: 21px;"><strong>Description</strong></span></h3>
<p>The goal of this project was to implement marching cubes, GLUI and output the extracted surface as a mesh file. This was accomplished using OpenGL, the Marching Cubes Algorithm, GLUT and GLUI. Currently, the user is able to create Metaballs (spheres) and then using the GLUI &#8211; change the voxelization density, light color, scale, rotate and translate the spheres.<br />
<span id="more-349"></span></p>
<p><span class="Apple-style-span" style="font-size: 18px; line-height: 21px;"><strong>Project Details</strong></span></p>
<p>The first attempt to implement marching cubes was a complete failure due to the way the threshold values were calculated. The new method pre computes the iso value at each vertex before drawing the surface. Each surface on the canvas is represented by a center, radius and associated implicit equation.</p>
<h4><strong>References </strong></h4>
<p><a href="http://paulbourke.net/geometry/polygonise/">Paul Bourke&#8217;s Site </a></p>
<p>Paul Rademacher&#8217;s GLUI Tutorials</p>
<h4>MetaPlayground Demo</h4>
<p>[kml_flashembed publishmethod="static" fversion="8.0.0" movie="http://ericosgood.com/wp-content/uploads/2011/03/playground_demo.swf" width="1024" height="768" targetclass="flashmovie"]</p>
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>
<p>[/kml_flashembed]</p>
<h4>Opening the &#8220;.m&#8221; File from the last video in a Mesh Parsing Program</h4>
<p>[kml_flashembed publishmethod="static" fversion="8.0.0" movie="http://ericosgood.com/wp-content/uploads/2011/03/mesh_output_demo.swf" width="1024" height="768" targetclass="flashmovie"]</p>
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>
<p>[/kml_flashembed]</p>
<div class="AWD_facebook_likebutton "><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fericosgood.com%2Fprog%2Fgrapics%2Fmetaplayground-an-isosurface-and-marching-cubes-demo%2F&amp;send=false&amp;layout=standard&amp;width=300&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=&amp;height=35&ref=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:300px; height:35px;" allowTransparency="true"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://ericosgood.com/prog/grapics/metaplayground-an-isosurface-and-marching-cubes-demo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Request a Tutorial</title>
		<link>http://ericosgood.com/prog/android/android-request-a-tutorial/</link>
		<comments>http://ericosgood.com/prog/android/android-request-a-tutorial/#comments</comments>
		<pubDate>Fri, 04 Mar 2011 02:37:16 +0000</pubDate>
		<dc:creator>eosgood</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[android app]]></category>
		<category><![CDATA[android dev]]></category>
		<category><![CDATA[android tutorial]]></category>

		<guid isPermaLink="false">http://ericosgood.com/?p=305</guid>
		<description><![CDATA[Request an Android Tutorial by leaving me a comment on this post and I will create one! &#8211;Eric]]></description>
			<content:encoded><![CDATA[<div class="AWD_facebook_likebutton "><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fericosgood.com%2Fprog%2Fandroid%2Fandroid-request-a-tutorial%2F&amp;send=false&amp;layout=standard&amp;width=300&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=&amp;height=35&ref=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:300px; height:35px;" allowTransparency="true"></iframe></div><p>Request an Android Tutorial by leaving me a comment on this post and I will create one! </p>
<p>&#8211;Eric </p>
<div class="AWD_facebook_likebutton "><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fericosgood.com%2Fprog%2Fandroid%2Fandroid-request-a-tutorial%2F&amp;send=false&amp;layout=standard&amp;width=300&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=&amp;height=35&ref=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:300px; height:35px;" allowTransparency="true"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://ericosgood.com/prog/android/android-request-a-tutorial/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Adding Facebook &#8220;Like&#8221; Buttons to WordPress</title>
		<link>http://ericosgood.com/tech/adding-facebook-like-wordpress/</link>
		<comments>http://ericosgood.com/tech/adding-facebook-like-wordpress/#comments</comments>
		<pubDate>Mon, 28 Feb 2011 18:39:36 +0000</pubDate>
		<dc:creator>eosgood</dc:creator>
				<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[facebook like]]></category>
		<category><![CDATA[linkedin]]></category>
		<category><![CDATA[social media]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[wordpress plugin]]></category>

		<guid isPermaLink="false">http://ericosgood.com/?p=294</guid>
		<description><![CDATA[Allowing your users to "Like" your wordpress posts on Facebook is just a few clicks away. This tutorial shows you how to add this functionality by installing a new plugin. <a href="http://ericosgood.com/tech/adding-facebook-like-wordpress/"><div class="read-more">Read more &#8250;</div><!-- end of .read-more --></a>]]></description>
			<content:encoded><![CDATA[<div class="AWD_facebook_likebutton "><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fericosgood.com%2Ftech%2Fadding-facebook-like-wordpress%2F&amp;send=false&amp;layout=standard&amp;width=300&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=&amp;height=35&ref=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:300px; height:35px;" allowTransparency="true"></iframe></div><p>Allowing your users to &#8220;Like&#8221; your wordpress posts on Facebook is just a few clicks away. This tutorial shows you how to add this functionality by installing a new plugin.<span id="more-294"></span> I have tried a few different ones and found that the quickest plugin is the <a href="http://wordpress.org/extend/plugins/like/"> Like Plugin </a> either download it or go to Dashboard-&gt;Plugins-&gt;Add New-&gt;Search and search for like. There is also the <a href="http://wordpress.org/extend/plugins/facebook-social-plugins/"> Facebook Social Plugins </a> which uses widgets and has more functionality but takes more time to setup.</p>
<div class="AWD_facebook_likebutton "><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fericosgood.com%2Ftech%2Fadding-facebook-like-wordpress%2F&amp;send=false&amp;layout=standard&amp;width=300&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=&amp;height=35&ref=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:300px; height:35px;" allowTransparency="true"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://ericosgood.com/tech/adding-facebook-like-wordpress/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AppWidget Tutorial</title>
		<link>http://ericosgood.com/prog/android-appwidget-tutorial/</link>
		<comments>http://ericosgood.com/prog/android-appwidget-tutorial/#comments</comments>
		<pubDate>Mon, 28 Feb 2011 04:56:13 +0000</pubDate>
		<dc:creator>eosgood</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[android sdk]]></category>
		<category><![CDATA[appwidget]]></category>
		<category><![CDATA[eclipse]]></category>

		<guid isPermaLink="false">http://ericosgood.com/?p=231</guid>
		<description><![CDATA[AppWidget Overview Most applications on Android have to be launched with a launcher, but classes extending the AppWidgetProvider class will be shown among your available widgets.  This tutorial loosely follows the AppWidget Guide &#160; How Widgets Work AppWidgets need the following<span class="ellipsis">&#8230;</span> <a href="http://ericosgood.com/prog/android-appwidget-tutorial/"><div class="read-more">Read more &#8250;</div><!-- end of .read-more --></a>]]></description>
			<content:encoded><![CDATA[<div class="AWD_facebook_likebutton "><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fericosgood.com%2Fprog%2Fandroid-appwidget-tutorial%2F&amp;send=false&amp;layout=standard&amp;width=300&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=&amp;height=35&ref=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:300px; height:35px;" allowTransparency="true"></iframe></div><h3 style="text-align: center;">AppWidget Overview</h3>
<p>Most applications on Android have to be launched with a launcher, but classes extending the AppWidgetProvider class will be shown among your available widgets.  This tutorial loosely follows the <a href="http://developer.android.com/guide/topics/appwidgets/index.html">AppWidget Guide</a></p>
<p>&nbsp;</p>
<h3 style="text-align: center;"><span id="more-231"></span></h3>
<h3 style="text-align: center;">How Widgets Work</h3>
<p>AppWidgets need the following to work:</p>
<ul>
<li><strong>AppWidgetProviderInfo</strong> object: This object describes the metadata for an AppWidget; things like the layout, update frequency and the WppWidgetProvider class. This will be an xml file you create later.</li>
<li><strong>AppWidgetProvder</strong> class: Defines the basic methods that allow you to interface with the AppWidget though broadcast events for when the AppWidget is updated,enabled,disabled and deleted.</li>
<li><strong>AppWidgetConfiguration </strong>activity: OPTIONAL activity that runs when the user adds your AppWidget, it allows them to modify settings at create-time.</li>
</ul>
<h3>Part 1: Setup</h3>
<p>Create a new Android project with no activity in eclipse (this tutorial assumes you have already setup eclipse and android if you haven&#8217;t visit: <a href="http://developer.android.com/guide/developing/index.html"> Developing Android with Eclipse</a></p>
<h3>Part 2: AppWidgetProviderInfo</h3>
<p>Now that we have created a new project, go to file-&gt;new-&gt;other..-&gt;Android-&gt;Android XML File and create the AppWidgetProviderInfo file. <img src="http://ericosgood.com/wp-content/uploads/2011/02/provider_info.png" alt="provider_info" /> Now that we have created the xml file, edit its info: <img src="http://ericosgood.com/wp-content/uploads/2011/02/provider_info2.png" alt="provider_info2" /></p>
<h3>Part 3: AppWidget Layout</h3>
<p>You must define an initial layout for the AppWidget with an xml file saved in res/layout. The only caveat is that AppWidget are based on RemoteViews and therefore only support a few layout classes, namely the following:</p>
<ul>
<li>FrameLayout</li>
<li>LinearLayout</li>
<li>RelativeLayout</li>
</ul>
<p>and the following widget classes:</p>
<ul>
<li>AnalogClock</li>
<li>Button</li>
<li>Chronometer</li>
<li>ImageButton</li>
<li>ImageView</li>
<li>ProgressBar</li>
<li>TextView</li>
</ul>
<p>We will next define a simple file, widget_layout.xml by creating a new file in eclipse: file-&gt;new-&gt;file:<br />
<img src="http://ericosgood.com/wp-content/uploads/2011/02/widget_layout.png" alt="widget_layout" /><br />
Right click the file in eclipse and select Open With -&gt; XML Editor and enter the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;?</span>xml version<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1.0&quot;</span> encoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;utf-8&quot;</span><span style="color: #339933;">?&amp;</span>gt<span style="color: #339933;">;</span>
<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>LinearLayout
	xmlns<span style="color: #339933;">:</span>android<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://schemas.android.com/apk/res/android&quot;</span>
	android<span style="color: #339933;">:</span>layout_width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;wrap_content&quot;</span>
	android<span style="color: #339933;">:</span>layout_height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;wrap_content&quot;</span>
	android<span style="color: #339933;">:</span>orientation<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;vertical&quot;</span><span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>TextView
		android<span style="color: #339933;">:</span>id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;@+id/date&quot;</span>
		android<span style="color: #339933;">:</span>layout_width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;wrap_content&quot;</span>
		android<span style="color: #339933;">:</span>layout_height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;wrap_content&quot;</span><span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>
	<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;/</span>TextView<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>
	<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>TextView
		android<span style="color: #339933;">:</span>id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;@id/event&quot;</span>
		android<span style="color: #339933;">:</span>layout_width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;wrap_content&quot;</span>
		android<span style="color: #339933;">:</span>layout_height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;wrap_content&quot;</span><span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>
	<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;/</span>TextView<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>
<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;/</span>LinearLayout<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span></pre></div></div>

<p>This will setup our layout to have a date textView and an event textView &#8211; simple initial layout.</p>
<h3>Part 3: Extending the AppWidgetProvider Class</h3>
<p>AppWidgetProvider extends the BroadcastReciever class to handle AppWidget broadcasts. It only receives broadcasts related to the AppWidget such as when the AppWidget is updated, deleted,enabled or disabled. First, let&#8217;s create our own AppWidgetProvider, notice how it extends AppWidgetProvider:<br />
<img src="http://ericosgood.com/wp-content/uploads/2011/02/appwidgetprovdider.png" alt="appwidget_provider" /> Here is the code to extend the AppWidgetProvider:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.eosgood.itsdue.widget</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.text.SimpleDateFormat</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Calendar</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.appwidget.AppWidgetManager</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.appwidget.AppWidgetProvider</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.content.Context</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.RemoteViews</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ItsdueAppWidgetProvider <span style="color: #000000; font-weight: bold;">extends</span> AppWidgetProvider <span style="color: #009900;">&#123;</span>
&nbsp;
	<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;">final</span> <span style="color: #003399;">String</span> DATE_FORMAT_NOW <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;yyyy-MM-dd HH:mm:ss&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onUpdate<span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span> context, AppWidgetManager appWidgetManager, <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> appWidgetIds<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> N <span style="color: #339933;">=</span> appWidgetIds.<span style="color: #006633;">length</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// loop through all app widgets the user has enabled</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>N<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        	<span style="color: #000066; font-weight: bold;">int</span> widgetId <span style="color: #339933;">=</span> appWidgetIds<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
        	<span style="color: #666666; font-style: italic;">// get the current date</span>
        	<span style="color: #003399;">String</span> current <span style="color: #339933;">=</span> now<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        	<span style="color: #666666; font-style: italic;">// get our view so we can edit the time</span>
        	RemoteViews views <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> RemoteViews<span style="color: #009900;">&#40;</span>context.<span style="color: #006633;">getPackageName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,
					R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">initial_layout</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        	views.<span style="color: #006633;">setTextViewText</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">date</span>, current<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        	<span style="color: #666666; font-style: italic;">// download an event from gmail and set it up (to come...)</span>
        	<span style="color: #666666; font-style: italic;">// for now well just make a dummy one</span>
        	views.<span style="color: #006633;">setTextViewText</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">event</span>, <span style="color: #0000ff;">&quot;Some sweet Event text!!!!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        	<span style="color: #666666; font-style: italic;">// update the widget</span>
        	appWidgetManager.<span style="color: #006633;">updateAppWidget</span><span style="color: #009900;">&#40;</span>widgetId, views<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> now<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">Calendar</span> cal <span style="color: #339933;">=</span> <span style="color: #003399;">Calendar</span>.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">SimpleDateFormat</span> sdf <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">SimpleDateFormat</span><span style="color: #009900;">&#40;</span>DATE_FORMAT_NOW<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> sdf.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span>cal.<span style="color: #006633;">getTime</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: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
<h3>Part 4: Updating the Manifest</h3>
<p>We need to update our manifest to reflect how our appwidget works. Open AndroidManifest.xml by right-clicking it and Open With-&gt;XML Editor.<br />
Add the following code between the tags:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #ddbb00;">&amp;lt;</span>receiver android:name=&quot;ItsdueAppWidgetProvider&quot;<span style="color: #ddbb00;">&amp;gt;</span>
			<span style="color: #ddbb00;">&amp;lt;</span>intent-filter<span style="color: #ddbb00;">&amp;gt;</span>
				<span style="color: #ddbb00;">&amp;lt;</span>action android:name=&quot;android.appwidget.action.APPWIDGET_UPDATE&quot; /<span style="color: #ddbb00;">&amp;gt;</span>
			<span style="color: #ddbb00;">&amp;lt;</span>/intent-filter<span style="color: #ddbb00;">&amp;gt;</span>
			<span style="color: #ddbb00;">&amp;lt;</span>meta-data android:name=&quot;android.appwidget.provider&quot;
				android:resource=&quot;@xml/widget_info&quot; /<span style="color: #ddbb00;">&amp;gt;</span>
		<span style="color: #ddbb00;">&amp;lt;</span>/receiver<span style="color: #ddbb00;">&amp;gt;</span></pre></div></div>

<p>&nbsp;</p>
<h3>Part 5: Adding Your Widget</h3>
<p>Navigate to a free screen on your phone and long-click and add your widget &#8211; you&#8217;re done you have successfully created your first app widget</p>
<div class="AWD_facebook_likebutton "><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fericosgood.com%2Fprog%2Fandroid-appwidget-tutorial%2F&amp;send=false&amp;layout=standard&amp;width=300&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=&amp;height=35&ref=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:300px; height:35px;" allowTransparency="true"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://ericosgood.com/prog/android-appwidget-tutorial/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>CodeIgniter PHP Framework</title>
		<link>http://ericosgood.com/tech/codeigniter-php-framework/</link>
		<comments>http://ericosgood.com/tech/codeigniter-php-framework/#comments</comments>
		<pubDate>Tue, 15 Feb 2011 18:29:25 +0000</pubDate>
		<dc:creator>eosgood</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[dynamic web development]]></category>
		<category><![CDATA[linkedin]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php framework]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[video tutorial]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://ericosgood.com/?p=183</guid>
		<description><![CDATA[Why you should use the CodeIngiter Framework <a href="http://ericosgood.com/tech/codeigniter-php-framework/"><div class="read-more">Read more &#8250;</div><!-- end of .read-more --></a>]]></description>
			<content:encoded><![CDATA[<div class="AWD_facebook_likebutton "><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fericosgood.com%2Ftech%2Fcodeigniter-php-framework%2F&amp;send=false&amp;layout=standard&amp;width=300&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=&amp;height=35&ref=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:300px; height:35px;" allowTransparency="true"></iframe></div><p>After doing some SEO Work for a friend, I came across a great PHP framework for rapid dynamic website dev: codeIgniter. What&#8217;s great about it is that not only does it have a great framework but there is a complete tutorial on creating a CMS from scratch using codeIngiter. If you have some spare time over the weekend and are interested in learning how to do PHP web dev, check out this tutorial: <a href="http://net.tutsplus.com/articles/news/codeigniter-from-scratch-day-1/">CodeIgniter Tutorial </a></p>
<div class="AWD_facebook_likebutton "><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fericosgood.com%2Ftech%2Fcodeigniter-php-framework%2F&amp;send=false&amp;layout=standard&amp;width=300&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=&amp;height=35&ref=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:300px; height:35px;" allowTransparency="true"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://ericosgood.com/tech/codeigniter-php-framework/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How-to set the username for Facebook page or Fan Page</title>
		<link>http://ericosgood.com/tech/how-to-set-the-username-for-facebook-page-or-fan-page/</link>
		<comments>http://ericosgood.com/tech/how-to-set-the-username-for-facebook-page-or-fan-page/#comments</comments>
		<pubDate>Wed, 02 Feb 2011 20:45:12 +0000</pubDate>
		<dc:creator>eosgood</dc:creator>
				<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[facebook fan page]]></category>
		<category><![CDATA[facebook username]]></category>
		<category><![CDATA[linkedin]]></category>

		<guid isPermaLink="false">http://ericosgood.com/?p=173</guid>
		<description><![CDATA[Tired of having your fan page&#8217;s url set to facebook.com/pages/fan-page-name/39012903290? Or having your facebook page&#8217;s url be facebook.com/12302390? There is a simple solution to this, log on facebook and go to facebook.com/username it may ask you to verify your identity<span class="ellipsis">&#8230;</span> <a href="http://ericosgood.com/tech/how-to-set-the-username-for-facebook-page-or-fan-page/"><div class="read-more">Read more &#8250;</div><!-- end of .read-more --></a>]]></description>
			<content:encoded><![CDATA[<div class="AWD_facebook_likebutton "><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fericosgood.com%2Ftech%2Fhow-to-set-the-username-for-facebook-page-or-fan-page%2F&amp;send=false&amp;layout=standard&amp;width=300&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=&amp;height=35&ref=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:300px; height:35px;" allowTransparency="true"></iframe></div><p>Tired of having your fan page&#8217;s url set to facebook.com/pages/fan-page-name/39012903290? Or having your facebook page&#8217;s url be facebook.com/12302390?</p>
<p><span id="more-173"></span></p>
<p>There is a simple solution to this, log on facebook and go to <a href="http://www.facebook.com/username">facebook.com/username</a> it may ask you to verify your identity by sending you a text but, once you have done that you will arrive at this page:<img src="http://ericosgood.com/wp-content/uploads/2011/02/Screen-shot-2011-02-02-at-12.41.09-PM.png" alt="http://ericosgood.com/wp-content/uploads/2011/02/Screen-shot-2011-02-02-at-12.41.09-PM.png" /></p>
<p>From this page you can set your page&#8217;s url and the url for fan pages you may be the owner of.</p>
<div class="AWD_facebook_likebutton "><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fericosgood.com%2Ftech%2Fhow-to-set-the-username-for-facebook-page-or-fan-page%2F&amp;send=false&amp;layout=standard&amp;width=300&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=&amp;height=35&ref=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:300px; height:35px;" allowTransparency="true"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://ericosgood.com/tech/how-to-set-the-username-for-facebook-page-or-fan-page/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Facebook Android SDK Tutorial</title>
		<link>http://ericosgood.com/prog/facebook-android-sdk-tutorial/</link>
		<comments>http://ericosgood.com/prog/facebook-android-sdk-tutorial/#comments</comments>
		<pubDate>Wed, 02 Feb 2011 18:45:57 +0000</pubDate>
		<dc:creator>eosgood</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[android facebook]]></category>
		<category><![CDATA[android sdk]]></category>
		<category><![CDATA[eclipse android sdk]]></category>
		<category><![CDATA[eclipse facebook android]]></category>
		<category><![CDATA[facebook authorize]]></category>
		<category><![CDATA[facebook-android-sdk]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[linkedin]]></category>

		<guid isPermaLink="false">http://ericosgood.com/?p=169</guid>
		<description><![CDATA[Tutorial for adding Facebook functionality to your Android app using the Facebook Android SDK <a href="http://ericosgood.com/prog/facebook-android-sdk-tutorial/"><div class="read-more">Read more &#8250;</div><!-- end of .read-more --></a>]]></description>
			<content:encoded><![CDATA[<div class="AWD_facebook_likebutton "><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fericosgood.com%2Fprog%2Ffacebook-android-sdk-tutorial%2F&amp;send=false&amp;layout=standard&amp;width=300&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=&amp;height=35&ref=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:300px; height:35px;" allowTransparency="true"></iframe></div><h3 style="text-align: center;"><strong>Overview</strong></h3>
<p>This tutorial assumes you have done some preliminary setup:</p>
<ul>
<li>Installed the <a href="http://developer.android.com/sdk/index.html">Android SDK</a> &amp; the <a href="http://developer.android.com/sdk/eclipse-adt.html">Eclipse Plugin</a></li>
<li>Downloaded the <a href="https://github.com/facebook/facebook-android-sdk">facebook-android-sdk </a></li>
<li>Created a <a title="Facebook App" href="http://www.facebook.com/developers/" target="_blank">Facebook App</a></li>
</ul>
<p><span id="more-169"></span></p>
<p>If you have not done this yet, follow the <a href="http://developers.facebook.com/docs/guides/mobile/#android"> Facebook Android Developer Guide</a> Once you have done the preliminary setup, you need to export your app&#8217;s signature with the following keytool command:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore
| openssl sha1 -binary
| openssl base64</pre></div></div>

<p>this keytool command generates a string the must registered in the <a href="http://www.facebook.com/developers/">Mobile and Devices</a> section of your app.</p>
<p><a name="part1"></a></p>
<h3 style="text-align: center;"><strong>Part 1: Creating a new facebook-android-sdk Eclipse Project</strong></h3>
<p>Create a new Android Project in eclipse: </p>
<p><img src="http://ericosgood.com/wp-content/uploads/2012/04/create_screen_fb_android_tutorial.png" alt="new_project" /> </p>
<p>Now that we have created a new Android Project, we need to reference the facebook project we setup during the <a href="http://developers.facebook.com/docs/guides/mobile/#android"> Facebook Android Developer Guide</a> by going right-clicking your Android Project and going to Properties->Android->Add..</p>
<p> <img src="http://ericosgood.com/wp-content/uploads/2012/04/fb_lib_android_tutorial.png" alt="build_path" /></p>
<p><a name="part2"></a></p>
<h3 style="text-align: center;"><strong>Part 2: Project Setup</strong></h3>
<p><a name="part2.1"></a></p>
<h4>1. Update the Manifest</h4>
<p>First we need to update the AndroidManifest.xml file to allow for internet access, otherwise this will not work. Add the following code between the application tags:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>uses<span style="color: #339933;">-</span>permission android<span style="color: #339933;">:</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;android.permission.INTERNET&quot;</span> <span style="color: #339933;">/&gt;</span></pre></div></div>

<p><img src="http://ericosgood.com/wp-content/uploads/2012/04/manifest_android_tutorial.png" alt="android_manifest" /><br />
<a name="part2.2"></a></p>
<h4>2. Instance Variables</h4>
<p>Next we need to add some instance variables to our activity:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.eosgood.tutorials</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.facebook.android.AsyncFacebookRunner</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.facebook.android.Facebook</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.Activity</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.TextView</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FacebookTutorialActivity <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// application id from facebook.com/developers</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;">final</span> <span style="color: #003399;">String</span> APP_ID <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;your_id_here&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// log tag for any log.x statements</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;">final</span> <span style="color: #003399;">String</span> TAG <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;FACEBOOK CONNECT&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// permissions array</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> PERMS <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;user_events&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// facebook vars</span>
	<span style="color: #000000; font-weight: bold;">private</span> Facebook mFacebook<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> AsyncFacebookRunner mAsyncRunner<span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// id text view</span>
	<span style="color: #000000; font-weight: bold;">private</span> TextView mText<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/** Called when the activity is first created. */</span>
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		setContentView<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">main</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<ul>
<li>APP_ID can be retrieved from <a href="http://www.facebook.com/developers/">Your App Page</a></li>
<li>TAG is for debug statement tagging</li>
<li>PERMS is a string array of the permissions you are requesting</li>
<li>mText is will display messages to the user</li>
</ul>
<h3 style="text-align: center;"><strong>Part 3: Setting up the Layout</strong></h3>
<p>Before we can start using the facebook sdk, we need to create a simple layout. I chose to do it dynamically but you could do it in xml, your choice. Here is the java code for the initLayout() function:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> initLayout<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	LinearLayout rootView <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LinearLayout<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getApplicationContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	rootView.<span style="color: #006633;">setOrientation</span><span style="color: #009900;">&#40;</span>LinearLayout.<span style="color: #006633;">VERTICAL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">mText</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TextView<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getApplicationContext</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: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">mText</span>.<span style="color: #006633;">setLayoutParams</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> LayoutParams<span style="color: #009900;">&#40;</span>LayoutParams.<span style="color: #006633;">WRAP_CONTENT</span>,
              LayoutParams.<span style="color: #006633;">WRAP_CONTENT</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	rootView.<span style="color: #006633;">addView</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">mText</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">setContentView</span><span style="color: #009900;">&#40;</span>rootView<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This code adds creates a LinearLayout and adds our mText TextView to it and then sets it as our Activity&#8217;s ContentView.</p>
<h3 style="text-align: center;"><strong>Part 4: Filling in onCreate()</strong></h3>
<p>Now that we have setup our instance variables and a have simple layout its time use them:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #008000; font-style: italic; font-weight: bold;">/** Called when the activity is first created. */</span>
@Override
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>APP_ID <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> APP_ID.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  	<span style="color: #003399;">Util</span>.<span style="color: #006633;">showAlert</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, <span style="color: #0000ff;">&quot;Warning&quot;</span>, <span style="color: #0000ff;">&quot;Facebook Applicaton ID must be &quot;</span>
  			<span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;specified before running&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// setup the content view</span>
  initLayout<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  mText.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Login Please&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// setup the facebook session</span>
  mFacebook <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Facebook<span style="color: #009900;">&#40;</span>APP_ID<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  mAsyncRunner <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AsyncFacebookRunner<span style="color: #009900;">&#40;</span>mFacebook<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This code will setup the layout, set the text for our mText TextView and initialize the facebook variables (what we really care about). YOU MUST SET YOUR APP_ID OTHERWISE NOTHING WILL WORK.</p>
<h3 style="text-align: center;"><strong>Part 5: Creating the Options Menu</strong></h3>
<p>Since this is just a simple tutorial I chose to use the options menu for login/logout and retrieving the userID and later, events. Setting up the options menu comes in three parts: onPrepareOptionsMenu, onCreateOptionsMenu and onOptionsItemSelected. For this to work, we are going to have to add some more instance variables:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><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;">final</span> <span style="color: #000066; font-weight: bold;">int</span> LOGIN <span style="color: #339933;">=</span> <span style="color: #003399;">Menu</span>.<span style="color: #006633;">FIRST</span><span style="color: #339933;">;</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;">final</span> <span style="color: #000066; font-weight: bold;">int</span> GET_EVENTS <span style="color: #339933;">=</span> <span style="color: #003399;">Menu</span>.<span style="color: #006633;">FIRST</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</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;">final</span> <span style="color: #000066; font-weight: bold;">int</span> GET_ID <span style="color: #339933;">=</span> <span style="color: #003399;">Menu</span>.<span style="color: #006633;">FIRST</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span></pre></div></div>

<h4>5.1 onCreateOptionsMenu</h4>
<p>Since this only gets called once, its pretty simple to create an options menu (the menu that shows when you press the menu button):</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Override
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> onCreateOptionsMenu<span style="color: #009900;">&#40;</span><span style="color: #003399;">Menu</span> menu<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreateOptionsMenu</span><span style="color: #009900;">&#40;</span>menu<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	menu.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Menu</span>.<span style="color: #006633;">NONE</span>, LOGIN, <span style="color: #003399;">Menu</span>.<span style="color: #006633;">NONE</span>, <span style="color: #0000ff;">&quot;Login Text&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	menu.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Menu</span>.<span style="color: #006633;">NONE</span>, GET_EVENTS, <span style="color: #003399;">Menu</span>.<span style="color: #006633;">NONE</span>, <span style="color: #0000ff;">&quot;Get Events&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	menu.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Menu</span>.<span style="color: #006633;">NONE</span>, GET_ID, <span style="color: #003399;">Menu</span>.<span style="color: #006633;">NONE</span>, <span style="color: #0000ff;">&quot;Get UserID&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h4>5.2 onPrepareOptionsMenu</h4>
<p>This function will be called every time the menu button is pressed and is a bit more complicated:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Override
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> onPrepareOptionsMenu<span style="color: #009900;">&#40;</span><span style="color: #003399;">Menu</span> menu<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003399;">MenuItem</span> loginItem <span style="color: #339933;">=</span> menu.<span style="color: #006633;">findItem</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Menu</span>.<span style="color: #006633;">FIRST</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003399;">MenuItem</span> getID <span style="color: #339933;">=</span> menu.<span style="color: #006633;">findItem</span><span style="color: #009900;">&#40;</span>GET_ID<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>mFacebook.<span style="color: #006633;">isSessionValid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		loginItem.<span style="color: #006633;">setTitle</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Logout&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		getID.<span style="color: #006633;">setEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
		loginItem.<span style="color: #006633;">setTitle</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Login&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		getID.<span style="color: #006633;">setEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	loginItem.<span style="color: #006633;">setEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onPrepareOptionsMenu</span><span style="color: #009900;">&#40;</span>menu<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h4>5.3 onOptionsItemSelected</h4>
<p>Whenever an item is pressed in the options menu, this function will be called. This is where we begin to call the facebook variables!</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Override
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> onOptionsItemSelected<span style="color: #009900;">&#40;</span><span style="color: #003399;">MenuItem</span> item<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">int</span> itemId <span style="color: #339933;">=</span> item.<span style="color: #006633;">getItemId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>itemId<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">case</span> LOGIN<span style="color: #339933;">:</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>mFacebook.<span style="color: #006633;">isSessionValid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			mText.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Logging out...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			AsyncFacebookRunner asyncRunner <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AsyncFacebookRunner<span style="color: #009900;">&#40;</span>
					mFacebook<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			asyncRunner.<span style="color: #006633;">logout</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, <span style="color: #000000; font-weight: bold;">new</span> LogoutRequestListener<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: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			mFacebook.<span style="color: #006633;">authorize</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, PERMS, <span style="color: #000000; font-weight: bold;">new</span> LoginDialogListener<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: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">case</span> GET_ID<span style="color: #339933;">:</span>
		mAsyncRunner.<span style="color: #006633;">request</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;me&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> IDRequestListener<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: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">case</span> GET_EVENTS<span style="color: #339933;">:</span>
		mAsyncRunner.<span style="color: #006633;">request</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;me/events&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> EventRequestListener<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: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">default</span><span style="color: #339933;">:</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So, if the Login/Logout button is pressed in the login state, mFacebook attempts to authorize using the permissions we specified and then posting its results to LoginDialogListener which is a private class of our Activity.</p>
<h3 style="text-align: center;"><strong>Part 6: Implementing the DialogListener Interface</strong></h3>
<p>You could implement this interface by creating an inline class but I think the code is much more readable if you create a private inner class eg</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">class</span> LoginDialogListener <span style="color: #000000; font-weight: bold;">implements</span> DialogListener</pre></div></div>

<p>This is a very simple class that simply changes the text of our mText instance variable also, after onComplete is called, mFAcebook.isSessionValid() will now return true. Here is the code for LoginDialogListener:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">class</span> LoginDialogListener <span style="color: #000000; font-weight: bold;">implements</span> DialogListener <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onComplete<span style="color: #009900;">&#40;</span>Bundle values<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		mText.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Facebook login successful. Press Menu...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onFacebookError<span style="color: #009900;">&#40;</span>FacebookError e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// TODO Auto-generated method stub</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onError<span style="color: #009900;">&#40;</span>DialogError e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// TODO Auto-generated method stub</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCancel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// TODO Auto-generated method stub</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I should probably have some Log.d(TAG, e.getLocalizedMessage()) statements but I&#8217;m a lazy programmer&#8230;</p>
<h3 style="text-align: center;"><strong>Part 7: Implementing the RequestListener Interface</strong></h3>
<h4>7.1 Logging Out</h4>
<p>Again you could just do this inline. This private class:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">class</span> LogoutRequestListener <span style="color: #000000; font-weight: bold;">implements</span> RequestListener</pre></div></div>

<p>takes care of the logout process. For this to work, we are going to have to add a Handler instance variable to our class:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> Handler mHandler <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Handler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">class</span> LogoutRequestListener <span style="color: #000000; font-weight: bold;">implements</span> RequestListener <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onComplete<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> response, <span style="color: #003399;">Object</span> state<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// Dispatch on its own thread</span>
		mHandler.<span style="color: #006633;">post</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Runnable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				mText.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Logged out&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onIOException<span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e, <span style="color: #003399;">Object</span> state<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// TODO Auto-generated method stub</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onFileNotFoundException<span style="color: #009900;">&#40;</span><span style="color: #003399;">FileNotFoundException</span> e,
			<span style="color: #003399;">Object</span> state<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// TODO Auto-generated method stub</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onMalformedURLException<span style="color: #009900;">&#40;</span><span style="color: #003399;">MalformedURLException</span> e,
			<span style="color: #003399;">Object</span> state<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// TODO Auto-generated method stub</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onFacebookError<span style="color: #009900;">&#40;</span>FacebookError e, <span style="color: #003399;">Object</span> state<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// TODO Auto-generated method stub</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Since the logout process may take some time, a new thread is dispatched to run and then update the ui when it completes. If this is not done, the app can freeze up since the Android OS stops any apps that take more than 5sec to respond.</p>
<h4>7.2 Downloading the UserID</h4>
<p>Finally, how to download the UserID.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">case</span> GET_ID<span style="color: #339933;">:</span>
  mAsyncRunner.<span style="color: #006633;">request</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;me&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> IDRequestListener<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: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span></pre></div></div>

<p>is the code that uses our final private class:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">class</span> IDRequestListener <span style="color: #000000; font-weight: bold;">implements</span> RequestListener</pre></div></div>

<p>again the onComplete() method is key here:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">class</span> IDRequestListener <span style="color: #000000; font-weight: bold;">implements</span> RequestListener <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onComplete<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> response, <span style="color: #003399;">Object</span> state<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  	<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
  		<span style="color: #666666; font-style: italic;">// process the response here: executed in background thread</span>
  		Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG, <span style="color: #0000ff;">&quot;Response: &quot;</span> <span style="color: #339933;">+</span> response.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  		JSONObject json <span style="color: #339933;">=</span> <span style="color: #003399;">Util</span>.<span style="color: #006633;">parseJson</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  		<span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> id <span style="color: #339933;">=</span> json.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  		<span style="color: #666666; font-style: italic;">// then post the processed result back to the UI thread</span>
  		<span style="color: #666666; font-style: italic;">// if we do not do this, an runtime exception will be generated</span>
  		<span style="color: #666666; font-style: italic;">// e.g. &quot;CalledFromWrongThreadException: Only the original</span>
  		<span style="color: #666666; font-style: italic;">// thread that created a view hierarchy can touch its views.&quot;</span>
  		FacebookTutorialActivity.<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">runOnUiThread</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Runnable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  			<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  				userID <span style="color: #339933;">=</span> id<span style="color: #339933;">;</span>
  				mText.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello there, &quot;</span> <span style="color: #339933;">+</span> id <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  			<span style="color: #009900;">&#125;</span>
  		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  	<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>JSONException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  		Log.<span style="color: #006633;">w</span><span style="color: #009900;">&#40;</span>TAG, <span style="color: #0000ff;">&quot;JSON Error in response&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  	<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>FacebookError e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  		Log.<span style="color: #006633;">w</span><span style="color: #009900;">&#40;</span>TAG, <span style="color: #0000ff;">&quot;Facebook Error: &quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #006633;">getMessage</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: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onIOException<span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e, <span style="color: #003399;">Object</span> state<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  	<span style="color: #666666; font-style: italic;">// TODO Auto-generated method stub</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onFileNotFoundException<span style="color: #009900;">&#40;</span><span style="color: #003399;">FileNotFoundException</span> e,
  		<span style="color: #003399;">Object</span> state<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  	<span style="color: #666666; font-style: italic;">// TODO Auto-generated method stub</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onMalformedURLException<span style="color: #009900;">&#40;</span><span style="color: #003399;">MalformedURLException</span> e,
  		<span style="color: #003399;">Object</span> state<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  	<span style="color: #666666; font-style: italic;">// TODO Auto-generated method stub</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onFacebookError<span style="color: #009900;">&#40;</span>FacebookError e, <span style="color: #003399;">Object</span> state<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  	<span style="color: #666666; font-style: italic;">// TODO Auto-generated method stub</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h4>7.3 Downloading the User&#8217;s Events</h4>
<p>First, we need to create a class for viewing these events:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.eosgood.tutorials</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FbEvent <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> id<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> title<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> startTime<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> endTime<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> location<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> FbEvent<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> _id, <span style="color: #003399;">String</span> _title, <span style="color: #003399;">String</span> _sT, <span style="color: #003399;">String</span> _eT, <span style="color: #003399;">String</span> _loc<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">id</span> <span style="color: #339933;">=</span> _id<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">title</span> <span style="color: #339933;">=</span> _title<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">startTime</span> <span style="color: #339933;">=</span> _sT<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">endTime</span> <span style="color: #339933;">=</span> _eT<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">location</span> <span style="color: #339933;">=</span> _loc<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> id<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getTitle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> title<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getStartTime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> startTime<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getEndTime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> endTime<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getLocation<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> location<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now we can display info about the user&#8217;s events we need to request all the user&#8217;s event from the api using our last private class:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">class</span> EventRequestListener <span style="color: #000000; font-weight: bold;">implements</span> RequestListener</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">class</span> EventRequestListener <span style="color: #000000; font-weight: bold;">implements</span> RequestListener <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onComplete<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> response, <span style="color: #003399;">Object</span> state<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// process the response here: executed in background thread</span>
			Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG, <span style="color: #0000ff;">&quot;Response: &quot;</span> <span style="color: #339933;">+</span> response.<span style="color: #006633;">toString</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: #000000; font-weight: bold;">final</span> JSONObject json <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONObject<span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			JSONArray d <span style="color: #339933;">=</span> json.<span style="color: #006633;">getJSONArray</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;data&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> d.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				JSONObject event <span style="color: #339933;">=</span> d.<span style="color: #006633;">getJSONObject</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				FbEvent newEvent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FbEvent<span style="color: #009900;">&#40;</span>event.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span>,
						event.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span>,
						event.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;start_time&quot;</span><span style="color: #009900;">&#41;</span>,
						event.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;end_time&quot;</span><span style="color: #009900;">&#41;</span>,
						event.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;location&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				events.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>newEvent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// then post the processed result back to the UI thread</span>
			<span style="color: #666666; font-style: italic;">// if we do not do this, an runtime exception will be generated</span>
			<span style="color: #666666; font-style: italic;">// e.g. &quot;CalledFromWrongThreadException: Only the original</span>
			<span style="color: #666666; font-style: italic;">// thread that created a view hierarchy can touch its views.&quot;</span>
			FacebookTutorialActivity.<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">runOnUiThread</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Runnable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>FbEvent event <span style="color: #339933;">:</span> events<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
						TextView view <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TextView<span style="color: #009900;">&#40;</span>
								getApplicationContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
						view.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>event.<span style="color: #006633;">getTitle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
						view.<span style="color: #006633;">setTextSize</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
						eventLayout.<span style="color: #006633;">addView</span><span style="color: #009900;">&#40;</span>view<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>JSONException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			Log.<span style="color: #006633;">w</span><span style="color: #009900;">&#40;</span>TAG, <span style="color: #0000ff;">&quot;JSON Error in response&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onIOException<span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e, <span style="color: #003399;">Object</span> state<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// TODO Auto-generated method stub</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onFileNotFoundException<span style="color: #009900;">&#40;</span><span style="color: #003399;">FileNotFoundException</span> e,
			<span style="color: #003399;">Object</span> state<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// TODO Auto-generated method stub</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onMalformedURLException<span style="color: #009900;">&#40;</span><span style="color: #003399;">MalformedURLException</span> e,
			<span style="color: #003399;">Object</span> state<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// TODO Auto-generated method stub</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onFacebookError<span style="color: #009900;">&#40;</span>FacebookError e, <span style="color: #003399;">Object</span> state<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// TODO Auto-generated method stub</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This class requests all the events for the user and then adds to the rootView in a asynchronous fashion.</p>
<h3 style="text-align: center;"><strong>The END</strong></h3>
<p>Feel free to clone / fork this code on <a title="github" href="https://github.com/eosgood/Facebook-Android-SDK-Tutorial" target="_blank">github</a></p>
<div class="AWD_facebook_likebutton "><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fericosgood.com%2Fprog%2Ffacebook-android-sdk-tutorial%2F&amp;send=false&amp;layout=standard&amp;width=300&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=&amp;height=35&ref=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:300px; height:35px;" allowTransparency="true"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://ericosgood.com/prog/facebook-android-sdk-tutorial/feed/</wfw:commentRss>
		<slash:comments>47</slash:comments>
		</item>
		<item>
		<title>Android Application Null Pointer Exception in Eclipse IDE</title>
		<link>http://ericosgood.com/prog/android/android-application-null-pointer-exception-in-eclipse-ide/</link>
		<comments>http://ericosgood.com/prog/android/android-application-null-pointer-exception-in-eclipse-ide/#comments</comments>
		<pubDate>Mon, 31 Jan 2011 08:35:46 +0000</pubDate>
		<dc:creator>eosgood</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[eclipse ide]]></category>
		<category><![CDATA[linkedin]]></category>
		<category><![CDATA[nullpointerexcpetion]]></category>

		<guid isPermaLink="false">http://ericosgood.com/?p=155</guid>
		<description><![CDATA[This problem probably cost me about 2hrs. There is a bug in version &#60;9.0 of the Android SDK that will cause Eclipse to give the dreaded NullPointerException for Android Application and Android Unit Tests &#60;/lame&#62;. However, there is a simple<span class="ellipsis">&#8230;</span> <a href="http://ericosgood.com/prog/android/android-application-null-pointer-exception-in-eclipse-ide/"><div class="read-more">Read more &#8250;</div><!-- end of .read-more --></a>]]></description>
			<content:encoded><![CDATA[<div class="AWD_facebook_likebutton "><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fericosgood.com%2Fprog%2Fandroid%2Fandroid-application-null-pointer-exception-in-eclipse-ide%2F&amp;send=false&amp;layout=standard&amp;width=300&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=&amp;height=35&ref=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:300px; height:35px;" allowTransparency="true"></iframe></div><p>This problem probably cost me about 2hrs. There is a bug in version &lt;9.0 of the Android SDK that will cause Eclipse to give the dreaded NullPointerException for Android Application and Android Unit Tests &lt;/lame&gt;.</p>
<p>However, there is a simple solution to this problem:</p>
<p><strong>Help-&gt;check for updates</strong>.</p>
<p>It will tell you that you can update to SDK v9.0: do it, and you are good to go! You can edit your Run Configs and Test Configs without getting cryptic error messages anymore.</p>
<div class="AWD_facebook_likebutton "><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fericosgood.com%2Fprog%2Fandroid%2Fandroid-application-null-pointer-exception-in-eclipse-ide%2F&amp;send=false&amp;layout=standard&amp;width=300&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=&amp;height=35&ref=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:300px; height:35px;" allowTransparency="true"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://ericosgood.com/prog/android/android-application-null-pointer-exception-in-eclipse-ide/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adding REST Webservice Functionality</title>
		<link>http://ericosgood.com/prog/rest-webservice-android/</link>
		<comments>http://ericosgood.com/prog/rest-webservice-android/#comments</comments>
		<pubDate>Sun, 30 Jan 2011 23:39:00 +0000</pubDate>
		<dc:creator>eosgood</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[android sdk]]></category>
		<category><![CDATA[android sdk webservice]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[linkedin]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[REST webservice]]></category>

		<guid isPermaLink="false">http://ericosgood.com/?p=131</guid>
		<description><![CDATA[For one of my current projects, bpstats.com, I needed to learn how to interface with a REST webservice from an Android application. Here is a tutorial for creating a generic REST webservice client for the Android SDK.  First, create a<span class="ellipsis">&#8230;</span> <a href="http://ericosgood.com/prog/rest-webservice-android/"><div class="read-more">Read more &#8250;</div><!-- end of .read-more --></a>]]></description>
			<content:encoded><![CDATA[<div class="AWD_facebook_likebutton "><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fericosgood.com%2Fprog%2Frest-webservice-android%2F&amp;send=false&amp;layout=standard&amp;width=300&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=&amp;height=35&ref=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:300px; height:35px;" allowTransparency="true"></iframe></div><p>For one of my current projects, bpstats.com, I needed to learn how to interface with a REST webservice from an Android application.</p>
<p>Here is a tutorial for creating a generic REST webservice client for the Android SDK. <span id="more-131"></span></p>
<p>First, create a new Android project and related Android Test Project via Eclipse File-&gt;New Project-&gt;Android Project</p>
<p>This will automatically create a project for the RestClient and a project for unitTesting</p>
<p>//PRO TIP: Let eclipse create the project and the test project for you: it will save you major headaches later trying to update the xml config files</p>
<p>//PRO TIP: Make sure the manifest file in your project, not the test project, has the following line in between the &lt;manifest&gt; &lt;/manifest&gt; tags:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #ddbb00;">&amp;lt;</span>uses-permission android:name=&quot;android.permission.INTERNET&quot;<span style="color: #ddbb00;">&amp;gt;&amp;lt;</span>/uses-permission<span style="color: #ddbb00;">&amp;gt;</span></pre></div></div>

<p>This will save you a major headache later (if you don&#8217;t have this line, you get a socket exception)</p>
<p>Now that the manifests are up to date, its time to create a RestClient and a RestClientTest:</p>
<p>Here is my code for a generic RestClient with only the GET method available and all exceptions thrown for debugging purposes (this will be important for unitTesting)</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.eosgood.rest</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.BufferedReader</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.InputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.InputStreamReader</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.Reader</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.StringWriter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.UnsupportedEncodingException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.Writer</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.URLEncoder</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.HashMap</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Iterator</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Map.Entry</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.Header</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.HttpEntity</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.HttpResponse</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.client.ClientProtocolException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.client.HttpClient</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.client.methods.HttpGet</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.client.methods.HttpPost</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.entity.StringEntity</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.impl.client.DefaultHttpClient</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.json.JSONArray</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.json.JSONException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.json.JSONObject</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.util.Log</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RestClient <span style="color: #009900;">&#123;</span>
&nbsp;
	<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;">final</span> <span style="color: #003399;">String</span> TAG <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;RestClient&quot;</span><span style="color: #339933;">;</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;">final</span> <span style="color: #000066; font-weight: bold;">int</span> POST <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</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;">final</span> <span style="color: #000066; font-weight: bold;">int</span> GET <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</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;">final</span> <span style="color: #000066; font-weight: bold;">int</span> JSON <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</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;">final</span> <span style="color: #000066; font-weight: bold;">int</span> XML <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> url<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> responsePhrase<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> response<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> JSONArray recvObj<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> respCode<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">HashMap</span> params<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">HashMap</span> headers<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> RestClient<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> restUrl<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">url</span> <span style="color: #339933;">=</span> restUrl<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">params</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">HashMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">headers</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">HashMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getResponse<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">response</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getErrorMsg<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">responsePhrase</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getResponseCode<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">respCode</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> addParam<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> key, <span style="color: #003399;">String</span> value<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">params</span>.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>key, value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> addHeader<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> key, <span style="color: #003399;">String</span> value<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">headers</span>.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>key, value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> buildParams<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">UnsupportedEncodingException</span> <span style="color: #009900;">&#123;</span>
		Iterator<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> it <span style="color: #339933;">=</span> params.<span style="color: #006633;">entrySet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">String</span> res <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;?&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>it.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			Entry pair <span style="color: #339933;">=</span> it.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #003399;">String</span> add <span style="color: #339933;">=</span> pair.<span style="color: #006633;">getKey</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=&quot;</span>
					<span style="color: #339933;">+</span> <span style="color: #003399;">URLEncoder</span>.<span style="color: #006633;">encode</span><span style="color: #009900;">&#40;</span>pair.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>params.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				res <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot;&amp;amp;&quot;</span> <span style="color: #339933;">+</span> add<span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
				res <span style="color: #339933;">+=</span> add<span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">return</span> res<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> convertStreamToString<span style="color: #009900;">&#40;</span><span style="color: #003399;">InputStream</span> is<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span>  <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>is <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>
			<span style="color: #003399;">Writer</span> writer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> buffer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1024</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #003399;">Reader</span> reader <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>is,
						<span style="color: #0000ff;">&quot;UTF-8&quot;</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;">int</span> n<span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>n <span style="color: #339933;">=</span> reader.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span>buffer<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					writer.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>buffer, <span style="color: #cc66cc;">0</span>, n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">UnsupportedEncodingException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #666666; font-style: italic;">// TODO Auto-generated catch block</span>
				e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span>
				is.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000000; font-weight: bold;">return</span> writer.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> sendHttpPost<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ClientProtocolException, <span style="color: #003399;">IOException</span><span style="color: #009900;">&#123;</span>
		HttpPost httpPostRequest <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HttpPost<span style="color: #009900;">&#40;</span>url <span style="color: #339933;">+</span> buildParams<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// add headers</span>
		Iterator<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> it <span style="color: #339933;">=</span> headers.<span style="color: #006633;">entrySet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>it.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			Entry header <span style="color: #339933;">=</span> it.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			httpPostRequest.<span style="color: #006633;">addHeader</span><span style="color: #009900;">&#40;</span>header.<span style="color: #006633;">getKey</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, header.<span style="color: #006633;">getValue</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: #009900;">&#125;</span>
&nbsp;
		HttpClient client <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DefaultHttpClient<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		HttpResponse resp<span style="color: #339933;">;</span>
&nbsp;
		resp <span style="color: #339933;">=</span> client.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span>httpPostRequest<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">respCode</span> <span style="color: #339933;">=</span> resp.<span style="color: #006633;">getStatusLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getStatusCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Log.<span style="color: #006633;">i</span><span style="color: #009900;">&#40;</span>TAG, <span style="color: #0000ff;">&quot;response code: &quot;</span> <span style="color: #339933;">+</span> getResponseCode<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: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">responsePhrase</span> <span style="color: #339933;">=</span> resp.<span style="color: #006633;">getStatusLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getReasonPhrase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Log.<span style="color: #006633;">i</span><span style="color: #009900;">&#40;</span>TAG, <span style="color: #0000ff;">&quot;error msg: &quot;</span> <span style="color: #339933;">+</span> getErrorMsg<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		HttpEntity entity <span style="color: #339933;">=</span> resp.<span style="color: #006633;">getEntity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>entity <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>
			<span style="color: #003399;">InputStream</span> is <span style="color: #339933;">=</span> entity.<span style="color: #006633;">getContent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #666666; font-style: italic;">//Header contentEncoding = resp.getFirstHeader(&quot;Content-encoding&quot;);</span>
			<span style="color: #666666; font-style: italic;">//Log.i(TAG, &quot;endoding&quot; + contentEncoding.getValue());</span>
			response <span style="color: #339933;">=</span> convertStreamToString<span style="color: #009900;">&#40;</span>is<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #666666; font-style: italic;">//response = response.substring(1,response.length()-1);</span>
			<span style="color: #666666; font-style: italic;">//response = &quot;{&quot; + response + &quot;}&quot;;</span>
			Log.<span style="color: #006633;">i</span><span style="color: #009900;">&#40;</span>TAG, <span style="color: #0000ff;">&quot;response: &quot;</span> <span style="color: #339933;">+</span> response<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			is.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> callWebService<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> method<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IllegalStateException</span>, ClientProtocolException, <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>method<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">case</span> GET<span style="color: #339933;">:</span>
			<span style="color: #666666; font-style: italic;">// build correct url</span>
			HttpGet request <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HttpGet<span style="color: #009900;">&#40;</span>url <span style="color: #339933;">+</span> buildParams<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// add headers</span>
			Iterator<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> it <span style="color: #339933;">=</span> headers.<span style="color: #006633;">entrySet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>it.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				Entry header <span style="color: #339933;">=</span> it.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				request.<span style="color: #006633;">addHeader</span><span style="color: #009900;">&#40;</span>header.<span style="color: #006633;">getKey</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, header.<span style="color: #006633;">getValue</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: #009900;">&#125;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// exec request</span>
			HttpClient client <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DefaultHttpClient<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			HttpResponse resp<span style="color: #339933;">;</span>
&nbsp;
			resp <span style="color: #339933;">=</span> client.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span>request<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">respCode</span> <span style="color: #339933;">=</span> resp.<span style="color: #006633;">getStatusLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getStatusCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">responsePhrase</span> <span style="color: #339933;">=</span> resp.<span style="color: #006633;">getStatusLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getReasonPhrase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			HttpEntity entity <span style="color: #339933;">=</span> resp.<span style="color: #006633;">getEntity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>entity <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>
				<span style="color: #003399;">InputStream</span> is <span style="color: #339933;">=</span> entity.<span style="color: #006633;">getContent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				response <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">convertStreamToString</span><span style="color: #009900;">&#40;</span>is<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				response <span style="color: #339933;">=</span> response.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>,response.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				response <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;{&quot;</span> <span style="color: #339933;">+</span> response <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;}&quot;</span><span style="color: #339933;">;</span>
				Log.<span style="color: #006633;">i</span><span style="color: #009900;">&#40;</span>TAG, <span style="color: #0000ff;">&quot;response: &quot;</span> <span style="color: #339933;">+</span> response<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				is.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">case</span> POST<span style="color: #339933;">:</span>
			sendHttpPost<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> createObjectJson<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JSONException<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">recvObj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONArray<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">response</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Log.<span style="color: #006633;">i</span><span style="color: #009900;">&#40;</span>TAG, <span style="color: #0000ff;">&quot;json object&quot;</span> <span style="color: #339933;">+</span> recvObj.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;/json object&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here is a small junit test that makes a JSONArray from HttpPost:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.eosgood.rest.test</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.client.ClientProtocolException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.json.JSONException</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.eosgood.rest.RestClient</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">junit.framework.TestCase</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RestClientTest <span style="color: #000000; font-weight: bold;">extends</span> TestCase <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testClientGet<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		RestClient ri <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> RestClient<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;urlhere&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//ri.addParam(&quot;httpcontext&quot;, &quot;application/json&quot;);</span>
		ri.<span style="color: #006633;">addHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Accept&quot;</span>, <span style="color: #0000ff;">&quot;application/json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		ri.<span style="color: #006633;">addHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-type&quot;</span>, <span style="color: #0000ff;">&quot;application/json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			ri.<span style="color: #006633;">callWebService</span><span style="color: #009900;">&#40;</span>RestClient.<span style="color: #006633;">POST</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IllegalStateException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			assertTrue<span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">getLocalizedMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>ClientProtocolException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			assertTrue<span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">getLocalizedMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			assertTrue<span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">getLocalizedMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			ri.<span style="color: #006633;">createObjectJson</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>JSONException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			assertTrue<span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">getLocalizedMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		assertTrue<span style="color: #009900;">&#40;</span>ri.<span style="color: #006633;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<div class="AWD_facebook_likebutton "><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fericosgood.com%2Fprog%2Frest-webservice-android%2F&amp;send=false&amp;layout=standard&amp;width=300&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=&amp;height=35&ref=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:300px; height:35px;" allowTransparency="true"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://ericosgood.com/prog/rest-webservice-android/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Adding Microsoft Fonts to Open Office</title>
		<link>http://ericosgood.com/prog/linux/openoffic-msfonts/</link>
		<comments>http://ericosgood.com/prog/linux/openoffic-msfonts/#comments</comments>
		<pubDate>Tue, 07 Sep 2010 17:03:02 +0000</pubDate>
		<dc:creator>eosgood</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Arial]]></category>
		<category><![CDATA[Font]]></category>
		<category><![CDATA[install microsoft fonts ubuntu]]></category>
		<category><![CDATA[Microsoft Fonts]]></category>
		<category><![CDATA[OpenOffice]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://ericosgood.com/?p=61</guid>
		<description><![CDATA[Calibri sucks. Open Office default fonts are bad on a good day. Adding support for the core MS Fonts is really easy and definitely worth the two minute effort. Simply open a terminal and type: sudo apt-get install msttcorefonts This<span class="ellipsis">&#8230;</span> <a href="http://ericosgood.com/prog/linux/openoffic-msfonts/"><div class="read-more">Read more &#8250;</div><!-- end of .read-more --></a>]]></description>
			<content:encoded><![CDATA[<div class="AWD_facebook_likebutton "><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fericosgood.com%2Fprog%2Flinux%2Fopenoffic-msfonts%2F&amp;send=false&amp;layout=standard&amp;width=300&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=&amp;height=35&ref=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:300px; height:35px;" allowTransparency="true"></iframe></div><p>Calibri sucks. Open Office default fonts are bad on a good day. Adding support for the core MS Fonts is really easy and definitely worth the two minute effort.  </p>
<p>Simply open a terminal and type:</p>
<p><code>sudo apt-get install msttcorefonts</code></p>
<p>This will install all of the MS fonts that are available in Microsoft Word for you to use in Open Office! </p>
<p>If you are using OpenOffice, restart it and you can finally use Arial instead of Calibri (Thank God!).</p>
<div class="AWD_facebook_likebutton "><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fericosgood.com%2Fprog%2Flinux%2Fopenoffic-msfonts%2F&amp;send=false&amp;layout=standard&amp;width=300&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=&amp;height=35&ref=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:300px; height:35px;" allowTransparency="true"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://ericosgood.com/prog/linux/openoffic-msfonts/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

