<?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>How-To &#8211; Wiebe Elsinga</title>
	<atom:link href="http://wiebe-elsinga.com/blog/category/howto/feed/" rel="self" type="application/rss+xml" />
	<link>http://wiebe-elsinga.com/blog</link>
	<description>Blog</description>
	<lastBuildDate>Thu, 17 Jan 2013 16:29:49 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.8.1</generator>
	<item>
		<title>Google TV emulator device definition</title>
		<link>http://wiebe-elsinga.com/blog/google-tv-emulator-device-definition/</link>
					<comments>http://wiebe-elsinga.com/blog/google-tv-emulator-device-definition/#respond</comments>
		
		<dc:creator><![CDATA[W.Elsinga]]></dc:creator>
		<pubDate>Thu, 17 Jan 2013 09:29:49 +0000</pubDate>
				<category><![CDATA[Business]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[Emulator]]></category>
		<category><![CDATA[Google TV]]></category>
		<guid isPermaLink="false">http://wiebe-elsinga.com/blog/?p=1405</guid>

					<description><![CDATA[So you want to use a Google TV emulator to develop Android applications. Many people having problems using the Google TV emulator because it hangs during booting (only showing the Google TV logo). The problem is that it only works with specific device configurations and resolutions. So lets have a look at the device definition: This setup is for a 720p resolution, for a 1080p you must change the resolution to 1920×1080 and the density from tvdpito xhdpi. Once the device definition is created, the next step is to create a new Android Virtual Device using it:]]></description>
										<content:encoded><![CDATA[<p>So you want to use a Google TV emulator to develop Android applications. Many people having problems using the Google TV emulator because it hangs during booting (only showing the Google TV logo).<br />
The problem is that it only works with specific device configurations and resolutions.</p>
<p>So lets have a look at the device definition:<br />
<a href="http://wiebe-elsinga.com/blog/?attachment_id=1407" rel="attachment wp-att-1407"><img loading="lazy" class="size-full wp-image-1407 aligncenter" alt="Device definition" src="http://wiebe-elsinga.com/blog/wp-content/uploads/2013/01/Screen-Shot-2013-01-17-at-17.21.58.png" width="667" height="389" srcset="http://wiebe-elsinga.com/blog/wp-content/uploads/2013/01/Screen-Shot-2013-01-17-at-17.21.58.png 667w, http://wiebe-elsinga.com/blog/wp-content/uploads/2013/01/Screen-Shot-2013-01-17-at-17.21.58-300x175.png 300w" sizes="(max-width: 667px) 100vw, 667px" /></a></p>
<p>This setup is for a <strong>720p</strong> resolution, for a <strong>1080p</strong> you must change the resolution to <strong>1920×1080</strong> and the density from <strong>tvdpi</strong>to <strong>xhdpi</strong>. Once the device definition is created, the next step is to create a new <strong>Android Virtual Device</strong> using it:</p>
<p><a href="http://wiebe-elsinga.com/blog/?attachment_id=1408" rel="attachment wp-att-1408"><img loading="lazy" class="aligncenter size-full wp-image-1408" alt="Avd" src="http://wiebe-elsinga.com/blog/wp-content/uploads/2013/01/Screen-Shot-2013-01-17-at-17.25.27.png" width="529" height="525" srcset="http://wiebe-elsinga.com/blog/wp-content/uploads/2013/01/Screen-Shot-2013-01-17-at-17.25.27.png 529w, http://wiebe-elsinga.com/blog/wp-content/uploads/2013/01/Screen-Shot-2013-01-17-at-17.25.27-150x150.png 150w, http://wiebe-elsinga.com/blog/wp-content/uploads/2013/01/Screen-Shot-2013-01-17-at-17.25.27-300x298.png 300w" sizes="(max-width: 529px) 100vw, 529px" /></a></p>
]]></content:encoded>
					
					<wfw:commentRss>http://wiebe-elsinga.com/blog/google-tv-emulator-device-definition/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Obfuscating for Android with ProGuard</title>
		<link>http://wiebe-elsinga.com/blog/obfuscating-for-android-with-proguard/</link>
					<comments>http://wiebe-elsinga.com/blog/obfuscating-for-android-with-proguard/#comments</comments>
		
		<dc:creator><![CDATA[W.Elsinga]]></dc:creator>
		<pubDate>Fri, 14 Dec 2012 05:56:23 +0000</pubDate>
				<category><![CDATA[Business]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Proguard]]></category>
		<guid isPermaLink="false">http://wiebe-elsinga.com/blog/?p=1337</guid>

					<description><![CDATA[Obfu-what? Right, Obfuscation, in general, describes a practice that is used to intentionally make something more difficult to understand. The nature of Java (the programming language for Android apps) is that the code is not compiled down to machine code; it is compiled to an intermediate format that is ready to be run on a variety of hardware platforms. While this allows great portability, it also leaves the code for Android apps, as present in the APK (Application PacKage file), available for extraction. I&#8217;m going to describe a way for you to obfuscate your Android code to make it harder for others to reverse engineer. And also why not shrink the size of your Android applications and optimize them to make them run faster at the same time. Without a doubt, the simplest method to protect an app is to enable the obfuscation in ProGuard. This freely available tool is already built into the Android toolkit. Obfuscation By default, compiled bytecode still contains a lot of debugging information: source file names, line numbers, field names, method names, argument names, variable names, etc. This information makes it straightforward to decompile the bytecode and reverse-engineer entire apps. Sometimes, this is not desirable. Obfuscators, such as ProGuard, can remove the debugging information and replace all names by meaningless character sequences, making it much harder to reverse-engineer the code. It further compacts the code as a bonus. The app remains functionally equivalent, except for the class names, method names, and line numbers given in exception stack traces. Shrinking Java source code (.java files) is typically compiled to bytecode (.class files). Bytecode is more compact than Java source code, but it may still contain a lot of unused code, especially if it includes program libraries. Shrinking programs such as ProGuard can analyze bytecode and remove unused classes, fields, and methods. The app remains functionally equivalent, including the information given in exception stack traces. For a realistic example, take the following code: if&#40;Config.LOGD&#41;&#41; &#123; &#160;Log.d&#40;TAG, &#34;Some text&#34;&#41;; &#125; The code as shown above is a typical scenario during development. You create code like this to help debug and test your code. Before releasing the final product, though, you set Config.LOGD to false, so it doesn&#8217;t execute. The problem is, this code is still in your application. It makes it bigger, and may cause potential security issues by including code which should never be seen by a snooping hacker. Shrinking the code solves this problem beautifully. The code is completely removed from the final product, leaving the final package safer and smaller. Optimizing Apart from removing unused classes, fields, and methods in the shrinking step, ProGuard can also perform optimizations at the bytecode level, inside and across methods. Proguard So let&#8217;s have a look at the proguard.cfg file. I recommend you open up the sample code, as I will be highlighting the key parts on this page. Basic template -dontpreverify -repackageclasses '' -allowaccessmodification -optimizations !code/simplification/arithmetic -keepattributes *Annotation* -keep public class * extends android.app.Activity -keep public class * extends android.app.Application -keep public class * extends android.app.Service -keep public class * extends android.content.BroadcastReceiver -keep public class * extends android.content.ContentProvider -keep public class * extends android.view.View { &#160; &#160; public &#60;init&#62;(android.content.Context); &#160; &#160; public &#60;init&#62;(android.content.Context, android.util.AttributeSet); &#160; &#160; public &#60;init&#62;(android.content.Context, android.util.AttributeSet, int); &#160; &#160; public void set*(...); } -keepclasseswithmembers class * { &#160; &#160; public &#60;init&#62;(android.content.Context, android.util.AttributeSet); } -keepclasseswithmembers class * { &#160; &#160; public &#60;init&#62;(android.content.Context, android.util.AttributeSet, int); } -keepclassmembers class * implements android.os.Parcelable { &#160; &#160; static android.os.Parcelable$Creator CREATOR; } -keepclassmembers class **.R$* { &#160; &#160; public static &#60;fields&#62;; } Fragments -keep public class * extends android.support.v4.app.Fragment -keep public class * extends android.app.Fragment Serializables -keepnames class * implements java.io.Serializable -keepclassmembers class * implements java.io.Serializable { &#160; &#160; static final long serialVersionUID; &#160; &#160; private static final java.io.ObjectStreamField[] serialPersistentFields; &#160; &#160; !static !transient &#60;fields&#62;; &#160; &#160; !private &#60;fields&#62;; &#160; &#160; !private &#60;methods&#62;; &#160; &#160; private void writeObject(java.io.ObjectOutputStream); &#160; &#160; private void readObject(java.io.ObjectInputStream); &#160; &#160; java.lang.Object writeReplace(); &#160; &#160; java.lang.Object readResolve(); } Removing Logging -assumenosideeffects class android.util.Log { &#160; &#160; public static *** e(...); &#160; &#160; public static *** w(...); &#160; &#160; public static *** wtf(...); &#160; &#160; public static *** d(...); &#160; &#160; public static *** v(...); } Methods -keepclasseswithmembernames class * { &#160; &#160; native &#60;methods&#62;; } -keepclassmembers class * { &#160; &#160; public void *ButtonClicked(android.view.View); } Now that we have added all our Proguard settings, we need to run Proguard during the build fase. I will use Maven to illustrate this. Within your pom.xml the android-maven-plugin should be defined. To this definition add the proguard setting like: &#160; &#160; &#160; &#60;plugin&#62; &#160; &#160; &#160; &#160; &#60;groupId&#62;com.jayway.maven.plugins.android.generation2&#60;/groupId&#62; &#160; &#160; &#160; &#160; &#60;artifactId&#62;android-maven-plugin&#60;/artifactId&#62; &#160; &#160; &#160; &#160; &#60;version&#62;3.0.0&#60;/version&#62; &#160; &#160; &#160; &#160; &#60;extensions&#62;true&#60;/extensions&#62; &#160; &#160; &#160; &#160; &#60;configuration&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#60;sdk&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#60;platform&#62;15&#60;/platform&#62; &#160; &#160; &#160; &#160; &#160; &#60;/sdk&#62; &#160; &#160; &#160; &#160; &#160; &#60;dex&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#60;jvmArguments&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#60;argument&#62;-Xms256m&#60;/argument&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#60;argument&#62;-Xmx512m&#60;/argument&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#60;/jvmArguments&#62; &#160; &#160; &#160; &#160; &#160; &#60;/dex&#62; &#160; &#160; &#160; &#160; &#160; &#60;run&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#60;debug&#62;true&#60;/debug&#62; &#160; &#160; &#160; &#160; &#160; &#60;/run&#62; &#160; &#160; &#160; &#160; &#160; &#60;proguard&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#60;skip&#62;false&#60;/skip&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#60;config&#62;proguard.cfg&#60;/config&#62; &#160; &#160; &#160; &#160; &#160; &#60;/proguard&#62; &#160; &#160; &#160; &#160; &#60;/configuration&#62; &#160; &#160; &#160; &#60;/plugin&#62; All that is left is to run Maven with mvn clean install To check if the app has been obfuscated, install the apk on you Android device and when you run it, you shouldn&#8217;t see any logging in logcat.]]></description>
										<content:encoded><![CDATA[<p>Obfu-what? Right, Obfuscation, in general, describes a practice that is used to intentionally make something more difficult to understand.<br />
The nature of Java (the programming language for Android apps) is that the code is not compiled down to machine code; it is compiled to an intermediate format that is ready to be run on a variety of hardware platforms. While this allows great portability, it also leaves the code for Android apps, as present in the APK (Application PacKage file), available for extraction.<br />
I&#8217;m going to describe a way for you to obfuscate your Android code to make it harder for others to reverse engineer. And also why not shrink the size of your Android applications and optimize them to make them run faster at the same time.</p>
<p>Without a doubt, the simplest method to protect an app is to enable the obfuscation in <a href="http://proguard.sourceforge.net/" title="Proguard" target="_blank">ProGuard</a>. This freely available tool is already built into the Android toolkit.</p>
<h3>Obfuscation</h3>
<p>By default, compiled bytecode still contains a lot of debugging information: source file names, line numbers, field names, method names, argument names, variable names, etc. This information makes it straightforward to decompile the bytecode and reverse-engineer entire apps. Sometimes, this is not desirable. Obfuscators, such as ProGuard, can remove the debugging information and replace all names by meaningless character sequences, making it much harder to reverse-engineer the code. It further compacts the code as a bonus. The app remains functionally equivalent, except for the class names, method names, and line numbers given in exception stack traces.</p>
<h3>Shrinking</h3>
<p>Java source code (.java files) is typically compiled to bytecode (.class files). Bytecode is more compact than Java source code, but it may still contain a lot of unused code, especially if it includes program libraries. Shrinking programs such as ProGuard can analyze bytecode and remove unused classes, fields, and methods. The app remains functionally equivalent, including the information given in exception stack traces. </p>
<p>For a realistic example, take the following code:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>Config.<span style="color: #006633;">LOGD</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp;Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG, <span style="color: #0000ff;">&quot;Some text&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>The code as shown above is a typical scenario during development. You create code like this to help debug and test your code. Before releasing the final product, though, you set Config.LOGD to false, so it doesn&#8217;t execute. The problem is, this code is still in your application. It makes it bigger, and may cause potential security issues by including code which should never be seen by a snooping hacker.</p>
<p>Shrinking the code solves this problem beautifully. The code is completely removed from the final product, leaving the final package safer and smaller.</p>
<h3>Optimizing</h3>
<p>Apart from removing unused classes, fields, and methods in the shrinking step, ProGuard can also perform optimizations at the bytecode level, inside and across methods.</p>
<h3>Proguard</h3>
<p>So let&#8217;s have a look at the <i>proguard.cfg</i> file. I recommend you open up the <a href="https://github.com/welsinga/sample_proguard" title="Sample code" target="_blank">sample code</a>, as I will be highlighting the key parts on this page.</p>
<h4>Basic template</h4>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">-dontpreverify<br />
-repackageclasses ''<br />
-allowaccessmodification<br />
-optimizations !code/simplification/arithmetic<br />
-keepattributes *Annotation*<br />
<br />
-keep public class * extends android.app.Activity<br />
-keep public class * extends android.app.Application<br />
-keep public class * extends android.app.Service<br />
-keep public class * extends android.content.BroadcastReceiver<br />
-keep public class * extends android.content.ContentProvider<br />
<br />
-keep public class * extends android.view.View {<br />
&nbsp; &nbsp; public &lt;init&gt;(android.content.Context);<br />
&nbsp; &nbsp; public &lt;init&gt;(android.content.Context, android.util.AttributeSet);<br />
&nbsp; &nbsp; public &lt;init&gt;(android.content.Context, android.util.AttributeSet, int);<br />
&nbsp; &nbsp; public void set*(...);<br />
}<br />
<br />
-keepclasseswithmembers class * {<br />
&nbsp; &nbsp; public &lt;init&gt;(android.content.Context, android.util.AttributeSet);<br />
}<br />
<br />
-keepclasseswithmembers class * {<br />
&nbsp; &nbsp; public &lt;init&gt;(android.content.Context, android.util.AttributeSet, int);<br />
}<br />
<br />
-keepclassmembers class * implements android.os.Parcelable {<br />
&nbsp; &nbsp; static android.os.Parcelable$Creator CREATOR;<br />
}<br />
<br />
-keepclassmembers class **.R$* {<br />
&nbsp; &nbsp; public static &lt;fields&gt;;<br />
}</div></div>
<h4>Fragments</h4>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">-keep public class * extends android.support.v4.app.Fragment<br />
-keep public class * extends android.app.Fragment</div></div>
<h4>Serializables</h4>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">-keepnames class * implements java.io.Serializable<br />
<br />
-keepclassmembers class * implements java.io.Serializable {<br />
&nbsp; &nbsp; static final long serialVersionUID;<br />
&nbsp; &nbsp; private static final java.io.ObjectStreamField[] serialPersistentFields;<br />
&nbsp; &nbsp; !static !transient &lt;fields&gt;;<br />
&nbsp; &nbsp; !private &lt;fields&gt;;<br />
&nbsp; &nbsp; !private &lt;methods&gt;;<br />
&nbsp; &nbsp; private void writeObject(java.io.ObjectOutputStream);<br />
&nbsp; &nbsp; private void readObject(java.io.ObjectInputStream);<br />
&nbsp; &nbsp; java.lang.Object writeReplace();<br />
&nbsp; &nbsp; java.lang.Object readResolve();<br />
}</div></div>
<h4>Removing Logging</h4>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">-assumenosideeffects class android.util.Log {<br />
&nbsp; &nbsp; public static *** e(...);<br />
&nbsp; &nbsp; public static *** w(...);<br />
&nbsp; &nbsp; public static *** wtf(...);<br />
&nbsp; &nbsp; public static *** d(...);<br />
&nbsp; &nbsp; public static *** v(...);<br />
}</div></div>
<h4>Methods</h4>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">-keepclasseswithmembernames class * {<br />
&nbsp; &nbsp; native &lt;methods&gt;;<br />
}<br />
-keepclassmembers class * {<br />
&nbsp; &nbsp; public void *ButtonClicked(android.view.View);<br />
}</div></div>
<p>Now that we have added all our Proguard settings, we need to run Proguard during the build fase. I will use <i>Maven</i> to illustrate this.</p>
<p>Within your <i>pom.xml</i> the <i>android-maven-plugin</i> should be defined. To this definition add the <i>proguard</i> setting like:</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.jayway.maven.plugins.android.generation2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>android-maven-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>3.0.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;extensions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>true<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/extensions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sdk<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;platform<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>15<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/platform<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/sdk<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dex<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jvmArguments<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;argument<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>-Xms256m<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/argument<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;argument<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>-Xmx512m<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/argument<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jvmArguments<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dex<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;run<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;debug<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>true<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/debug<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/run<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;proguard<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;skip<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>false<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/skip<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>proguard.cfg<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/proguard<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>All that is left is to run Maven with</p>
<div class="codecolorer-container dos default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="dos codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">mvn clean install</div></div>
<p>To check if the app has been obfuscated, install the apk on you Android device and when you run it, you shouldn&#8217;t see any logging in logcat.</p>
]]></content:encoded>
					
					<wfw:commentRss>http://wiebe-elsinga.com/blog/obfuscating-for-android-with-proguard/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>Automatically starting an Android application on boot</title>
		<link>http://wiebe-elsinga.com/blog/automatically-starting-an-android-application-on-boot/</link>
					<comments>http://wiebe-elsinga.com/blog/automatically-starting-an-android-application-on-boot/#respond</comments>
		
		<dc:creator><![CDATA[W.Elsinga]]></dc:creator>
		<pubDate>Tue, 04 Dec 2012 06:04:03 +0000</pubDate>
				<category><![CDATA[Business]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Broadcast]]></category>
		<guid isPermaLink="false">http://wiebe-elsinga.com/blog/?p=1311</guid>

					<description><![CDATA[There can be multiple reasons for you to want to automatically start (autostart) an Android application or service after booting your device. Lets have a look at what a colleague of mine wrote. To autostart an Android application there are a couple of steps you need to take. However before you do this you should consider this: Background service awareness When manually or even automatically starting an application, users will see something being started. If you (auto)start a background service it won’t be visible to the user. Users might not be aware of the fact that a service was started. Keep this in mind when using a service. Battery life As we know by now people might not be aware that your automatically started background service is running. Keep the battery life of the phone in mind when performing tasks in the background. To create a battery aware service use these things in moderation: Getting a GPS or network location Connecting to your network and sending/receiving data Read/write files (IO) Intensive tasks Let’s autostart our service First of all we need to make sure your application is allowed to receive and handle the broadcast that tells the application that your phone was booted. To be able to receive the broadcast you should add this permission as a child of the &#60;manifest&#62; element to your Android Manifest: &#60;uses-permission android:name=&#34;android.permission.RECEIVE_BOOT_COMPLETED&#34; /&#62; So, right now our application has the permission to receive a broadcasted message to tell us the phone was booted. In order to do anything with this broadcast we need to have a BroadcastReceiver. An example would look like this: public class ExampleBroadcastReceiver extends BroadcastReceiver &#123; &#160; @Override &#160; public void onReceive&#40;Context context, Intent intent&#41; &#160; &#123; &#160; &#160; if &#40;Intent.ACTION_BOOT_COMPLETED.equals&#40;intent.getAction&#40;&#41;&#41;&#41; &#160; &#160; &#123; &#160; &#160; &#160; /* Only perform this code if the BroadcastReceiver received the ACTION_BOOT_COMPLETED action.*/ &#160; &#160; &#125; &#160; &#125; &#125; What we see here is a class that extends the BroadcastReceiver and overrides the onReceive method. We could add our code to start our background service inside the if statement. This way we make sure that only the ACTION_BOOT_COMPLETED action is being processed. This gives us the opportunity to implement code for different Intent actions if necessary. Read the onReceive documentation for some do&#8217;s and dont&#8217;s. An important thing to know is that the onReceive method will be called whenever the class is declared in your manifest and wishes to receive the specified Intent. Let&#8217;s let our application know that our BroadcastReceiver exists and wishes to receive the android.intent.action.BOOT_COMPLETED intent. Add this to your &#60;application&#62; element of your Android Manifest: &#60;receiver android:enabled=”true” android:name=”com.itude.mobile.broadcast.ItudeMobileBroadcastReceiver” android:permission=”android.permission.RECEIVE_BOOT_COMPLETED”&#62; &#160; &#60;intent-filter&#62; &#160; &#160; &#60;action android:name=”android.intent.action.BOOT_COMPLETED” /&#62; &#160; &#60;/intent-filter&#62; &#60;/receiver&#62; Check and double check So to make sure you got everything covered check if you did this: Add the android.permission.RECEIVE_BOOT_COMPLETED permission as a child of your &#60;manifest&#62; element in your Android Manifest Create a class that extends BroadcastReceiver and add your code to the onReceive method Add this BroadcastReceiver class to your Android Manifest as a child of the &#60;application&#62; element so Android knows of it&#8217;s existence and it can trigger the onReceive method if the class matches the intent-filter Enjoy autostarting your application If all went well you can turn off your phone and turn it back on. After it has been booted your application should receive a broadcast from android that triggers your code. Automatically starting an Android application on boot, done!]]></description>
										<content:encoded><![CDATA[<p>There can be multiple reasons for you to want to automatically start (autostart) an Android application or service after booting your device. Lets have a look at what a <a href="https://plus.google.com/105121117776590013398/posts" title="Ali Derbane" target="_blank">colleague of mine</a> wrote. </p>
<p>To autostart an Android application there are a couple of steps you need to take. However before you do this you should consider this:</p>
<h3>Background service awareness</h3>
<p>When manually or even automatically starting an application, users will see something being started.<br />
If you (auto)start a background service it won’t be visible to the user. Users might not be aware of the fact that a service was started. Keep this in mind when using a service.</p>
<h3>Battery life</h3>
<p>As we know by now people might not be aware that your automatically started background service is running. Keep the battery life of the phone in mind when performing tasks in the background.</p>
<p>To create a battery aware service use these things in moderation:</p>
<ul>
<li>Getting a GPS or network location</li>
<li>Connecting to your network and sending/receiving data</li>
<li>Read/write files (IO)</li>
<li>Intensive tasks</li>
</ul>
<p></br></p>
<h3>Let’s autostart our service</h3>
<p>First of all we need to make sure your application is allowed to receive and handle the broadcast that tells the application that your phone was booted.<br />
To be able to receive the broadcast you should add this permission as a child of the <i>&lt;manifest&gt;</> element to your Android Manifest:</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">&lt;uses-permission</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.permission.RECEIVE_BOOT_COMPLETED&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></div></div>
<p>So, right now our application has the permission to receive a broadcasted message to tell us the phone was booted. In order to do anything with this broadcast we need to have a <a title="BroadcastReceiver" href="http://developer.android.com/reference/android/content/BroadcastReceiver.html" target="_blank"><b>BroadcastReceiver</b></a>. An example would look like this:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ExampleBroadcastReceiver <span style="color: #000000; font-weight: bold;">extends</span> BroadcastReceiver<br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; @Override<br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onReceive<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+context"><span style="color: #003399;">Context</span></a> context, Intent intent<span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>Intent.<span style="color: #006633;">ACTION_BOOT_COMPLETED</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>intent.<span style="color: #006633;">getAction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">/* Only perform this code if the BroadcastReceiver received the ACTION_BOOT_COMPLETED action.*/</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>What we see here is a class that extends the <strong>BroadcastReceiver</strong> and overrides the <strong>onReceive</strong> method. We could add our code to start our background service inside the if statement. This way we make sure that only the ACTION_BOOT_COMPLETED action is being processed. This gives us the opportunity to implement code for different Intent actions if necessary.<br />
Read the <a title="onReceive" href="http://developer.android.com/reference/android/content/BroadcastReceiver.html#onReceive%28android.content.Context,%20android.content.Intent%29" target="_blank">onReceive</a> documentation for some do&#8217;s and dont&#8217;s.</p>
<p>An important thing to know is that the onReceive method will be called whenever the class is declared in your manifest and wishes to receive the specified Intent. Let&#8217;s let our application know that our BroadcastReceiver exists and wishes to receive the <i>android.intent.action.BOOT_COMPLETED</i> intent. Add this to your <i>&lt;application&gt;</i> element of your Android Manifest:</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;receiver</span> <span style="color: #000066;">android:enabled</span>=”true” <span style="color: #000066;">android:name</span>=”com.itude.mobile.broadcast.ItudeMobileBroadcastReceiver” <span style="color: #000066;">android:permission</span>=”android.permission.RECEIVE_BOOT_COMPLETED”<span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;intent-filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span> <span style="color: #000066;">android:name</span>=”android.intent.action.BOOT_COMPLETED” <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/intent-filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/receiver<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p></p>
<h3>Check and double check</h3>
<p>So to make sure you got everything covered check if you did this:</p>
<ol>
<li>Add the <i><strong>android.permission.RECEIVE_BOOT_COMPLETED</strong></i> permission as a child of your <i><strong>&lt;manifest&gt;</strong></i> element in your Android Manifest</li>
<li>Create a class that extends <strong>BroadcastReceiver</strong> and add your code to the <strong>onReceive</strong> method</li>
<li>Add this BroadcastReceiver class to your Android Manifest as a child of the <i><strong>&lt;application&gt;</strong></i> element so Android knows of it&#8217;s existence and it can trigger the onReceive method if the class matches the intent-filter</li>
</ol>
<p></p>
<h3>Enjoy autostarting your application</h3>
<p>If all went well you can turn off your phone and turn it back on. After it has been booted your application should receive a broadcast from android that triggers your code. Automatically starting an Android application on boot, done!</p>
]]></content:encoded>
					
					<wfw:commentRss>http://wiebe-elsinga.com/blog/automatically-starting-an-android-application-on-boot/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Sending and Receiving Data SMS messages with Android</title>
		<link>http://wiebe-elsinga.com/blog/sending-and-receiving-data-sms-messages-with-android/</link>
					<comments>http://wiebe-elsinga.com/blog/sending-and-receiving-data-sms-messages-with-android/#comments</comments>
		
		<dc:creator><![CDATA[W.Elsinga]]></dc:creator>
		<pubDate>Mon, 03 Dec 2012 03:01:40 +0000</pubDate>
				<category><![CDATA[Business]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[AndroidDev]]></category>
		<category><![CDATA[Data SMS]]></category>
		<guid isPermaLink="false">http://wiebe-elsinga.com/blog/?p=1295</guid>

					<description><![CDATA[The humble short message service (SMS) celebrates its 20th birthday today. Engineer Neil Papworth sent the first SMS to an Orbitel 901 handset via Vodafone UK&#8217;s network in 1992: &#8220;Merry Christmas.&#8221; It would be safe to say that nearly every mobile phone sold in the past decade has SMS messaging capabilities. So making use of SMS with your Android app can be a great way to handle information when no Data network (3G or WIFI) is available. In this article, we will take a look at how you can programmatically send and receive SMS messages in your Android applications. Sending SMS Data messages So lets start with the sender AndroidManifest.xml. I recommend you open up the sample code, as I will be highlighting the key parts on this page. &#160;&#60;uses-permission android:name=&#34;android.permission.SEND_SMS&#34; /&#62; The uses-permission tag will allow the application to send SMS messages. Sending SMS message with the application will potentially incur additional cost on the user&#8217;s end. Indicating the SMS permissions in the AndroidManifest.xml file will let the user decide whether to allow the application to install or not. Next, we will create an activity called SendActivity that will handle sending SMS messages. private void sendSMS&#40;&#41; &#160; &#123; &#160; &#160; PendingIntent sent = this.createPendingResult&#40;SENT, new Intent&#40;&#41;, PendingIntent.FLAG_UPDATE_CURRENT&#41;; &#160; &#160; String messageText = _message.getText&#40;&#41;.toString&#40;&#41;; &#160; &#160; SmsManager smsManager = SmsManager.getDefault&#40;&#41;; &#160; &#160; smsManager.sendDataMessage&#40;_phoneNumber.getText&#40;&#41;.toString&#40;&#41;, null, SMS_PORT, messageText.getBytes&#40;&#41;, sent, null&#41;; &#160; &#125; To send an SMS message, you use the SmsManager class. Unlike other classes, you do not directly instantiate this class; instead you will call the getDefault() static method to obtain an SmsManager object. The sendDataMessage() method sends the SMS message with a PendingIntent. The PendingIntent object is used to identify a target to invoke at a later time. For example, after sending the message, you can use a PendingIntent object to update the current activity, and onActivityResult will be triggered. &#160; @Override &#160; protected void onActivityResult&#40;int requestCode, int resultCode, Intent data&#41; &#160; &#123; &#160; &#160; String toast = null; &#160; &#160; switch &#40;requestCode&#41; &#160; &#160; &#123; &#160; &#160; &#160; case SENT : &#160; &#160; &#160; &#160; switch &#40;resultCode&#41; &#160; &#160; &#160; &#160; &#123; &#160; &#160; &#160; &#160; &#160; case RESULT_OK : &#160; &#160; &#160; &#160; &#160; &#160; toast = &#34;OK&#34;; &#160; &#160; &#160; &#160; &#160; &#160; break; &#160; &#160; &#160; &#160; &#160; case SmsManager.RESULT_ERROR_GENERIC_FAILURE : &#160; &#160; &#160; &#160; &#160; &#160; toast = &#34;Generic Failure&#34;; &#160; &#160; &#160; &#160; &#160; &#160; break; &#160; &#160; &#160; &#160; &#160; case SmsManager.RESULT_ERROR_RADIO_OFF : &#160; &#160; &#160; &#160; &#160; &#160; toast = &#34;Radio Off&#34;; &#160; &#160; &#160; &#160; &#160; &#160; break; &#160; &#160; &#160; &#160; &#160; case SmsManager.RESULT_ERROR_NULL_PDU : &#160; &#160; &#160; &#160; &#160; &#160; toast = &#34;Null Pdu&#34;; &#160; &#160; &#160; &#160; &#160; &#160; break; &#160; &#160; &#160; &#160; &#125; &#160; &#160; &#160; &#160; break; &#160; &#160; &#125; &#160; &#160; if &#40;toast != null&#41; &#160; &#160; &#123; &#160; &#160; &#160; Toast.makeText&#40;this, toast, Toast.LENGTH_LONG&#41;.show&#40;&#41;; &#160; &#160; &#125; &#125; Receiving SMS Data messages Besides sending SMS messages, you can also intercept incoming SMS messages using a BroadcastReceiver object. To see how to receive SMS messages from within your Android application, lets look at the AndroidManifest.xml file within our receiver sample code. &#160;&#60;uses-permission android:name=&#34;android.permission.RECEIVE_SMS&#34; /&#62; &#160;&#60;uses-permission android:name=&#34;android.permission.BROADCAST_SMS&#34; /&#62; &#160;&#60;uses-permission android:name=&#34;android.permission.READ_SMS&#34; /&#62; The uses-permission tags will allow the application to receive and read SMS messages. Lets create a class that extends the BroadcastReceiver class and override the onReceive() method: &#160;@Override &#160; public void onReceive&#40;Context context, Intent intent&#41; &#160; &#123; &#160; &#160; Bundle bundle = intent.getExtras&#40;&#41;; &#160; &#160; String recMsgString = &#34;&#34;; &#160; &#160; String fromAddress = &#34;&#34;; &#160; &#160; SmsMessage recMsg = null; &#160; &#160; byte&#91;&#93; data = null; &#160; &#160; if &#40;bundle != null&#41; &#160; &#160; &#123; &#160; &#160; &#160; //---retrieve the SMS message received--- &#160; &#160; &#160; Object&#91;&#93; pdus = &#40;Object&#91;&#93;&#41; bundle.get&#40;&#34;pdus&#34;&#41;; &#160; &#160; &#160; for &#40;int i = 0; i &#60; pdus.length; i++&#41; &#160; &#160; &#160; &#123; &#160; &#160; &#160; &#160; recMsg = SmsMessage.createFromPdu&#40;&#40;byte&#91;&#93;&#41; pdus&#91;i&#93;&#41;; &#160; &#160; &#160; &#160; try &#160; &#160; &#160; &#160; &#123; &#160; &#160; &#160; &#160; &#160; data = recMsg.getUserData&#40;&#41;; &#160; &#160; &#160; &#160; &#125; &#160; &#160; &#160; &#160; catch &#40;Exception e&#41; &#160; &#160; &#160; &#160; &#123; &#160; &#160; &#160; &#160; &#125; &#160; &#160; &#160; &#160; if &#40;data != null&#41; &#160; &#160; &#160; &#160; &#123; &#160; &#160; &#160; &#160; &#160; for &#40;int index = 0; index &#60; data.length; ++index&#41; &#160; &#160; &#160; &#160; &#160; &#123; &#160; &#160; &#160; &#160; &#160; &#160; recMsgString += Character.toString&#40;&#40;char&#41; data&#91;index&#93;&#41;; &#160; &#160; &#160; &#160; &#160; &#125; &#160; &#160; &#160; &#160; &#125; &#160; &#160; &#160; &#160; fromAddress = recMsg.getOriginatingAddress&#40;&#41;; &#160; &#160; &#160; &#125; &#160; &#160; &#125; &#160; &#160; Intent result = new Intent&#40;context, ReceiveActivity.class&#41;; &#160; &#160; result.putExtra&#40;ReceiveActivity.MESSAGE, &#34;Received SMS from &#34; + fromAddress + &#34;: &#34; + recMsgString&#41;; &#160; &#160; result.addFlags&#40;Intent.FLAG_ACTIVITY_NEW_TASK&#41;; &#160; &#160; context.startActivity&#40;result&#41;; &#160; &#125; When SMS messages are received, the onReceive() method will be invoked. The SMS message is contained and attached to the Intent object via a Bundle object. The messages are stored in an Object array in the PDU format. To extract each message, you use the static createFromPdu() method from the SmsMessage class. The SMS message is then passed to an Activity that shows the message. That&#8217;s it. We&#8217;ve managed to send and received a Data SMS within an Android application. Till next time.]]></description>
										<content:encoded><![CDATA[<p>The humble short message service (SMS) celebrates its 20th birthday today. Engineer Neil Papworth sent the first SMS to an Orbitel 901 handset via Vodafone UK&#8217;s network in 1992: &#8220;Merry Christmas.&#8221;</p>
<p>It would be safe to say that nearly every mobile phone sold in the past decade has SMS messaging capabilities. So making use of SMS with your Android app can be a great way to handle information when no Data network (3G or WIFI) is available.<br />
In this article, we will take a look at how you can programmatically send and receive SMS messages in your Android applications.</p>
<h2>Sending SMS Data messages</h2>
<p>So lets start with the sender <i>AndroidManifest.xml</i>. I recommend you open up the <a href="https://github.com/welsinga/sample_sms" title="Sample project" target="_blank">sample code</a>, as I will be highlighting the key parts on this page.</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;uses-permission</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.permission.SEND_SMS&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></div></div>
<p>The <b>uses-permission</b> tag will allow the application to send SMS messages. Sending SMS message with the application will potentially incur additional cost on the user&#8217;s end.<br />
Indicating the SMS permissions in the <i>AndroidManifest.xml</i> file will let the user decide whether to allow the application to install or not. </p>
<p>Next, we will create an activity called <i>SendActivity</i> that will handle sending SMS messages.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> sendSMS<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; PendingIntent sent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">createPendingResult</span><span style="color: #009900;">&#40;</span>SENT, <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, PendingIntent.<span style="color: #006633;">FLAG_UPDATE_CURRENT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> messageText <span style="color: #339933;">=</span> _message.<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; SmsManager smsManager <span style="color: #339933;">=</span> SmsManager.<span style="color: #006633;">getDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; smsManager.<span style="color: #006633;">sendDataMessage</span><span style="color: #009900;">&#40;</span>_phoneNumber.<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000066; font-weight: bold;">null</span>, SMS_PORT, messageText.<span style="color: #006633;">getBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, sent, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>To send an SMS message, you use the <b>SmsManager</b> class. Unlike other classes, you do not directly instantiate this class; instead you will call the <b>getDefault()</b> static method to obtain an <b>SmsManager</b> object. The <b>sendDataMessage()</b> method sends the SMS message with a PendingIntent. The <b>PendingIntent</b> object is used to identify a target to invoke at a later time. For example, after sending the message, you can use a PendingIntent object to update the current activity, and <b>onActivityResult</b> will be triggered.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&nbsp; @Override<br />
&nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onActivityResult<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> requestCode, <span style="color: #000066; font-weight: bold;">int</span> resultCode, Intent data<span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> toast <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>requestCode<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">case</span> SENT <span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>resultCode<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">case</span> RESULT_OK <span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toast <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;OK&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">case</span> SmsManager.<span style="color: #006633;">RESULT_ERROR_GENERIC_FAILURE</span> <span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toast <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Generic Failure&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">case</span> SmsManager.<span style="color: #006633;">RESULT_ERROR_RADIO_OFF</span> <span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toast <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Radio Off&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">case</span> SmsManager.<span style="color: #006633;">RESULT_ERROR_NULL_PDU</span> <span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toast <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Null Pdu&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>toast <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; Toast.<span style="color: #006633;">makeText</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, toast, Toast.<span style="color: #006633;">LENGTH_LONG</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p></p>
<h2>Receiving SMS Data messages</h2>
<p>Besides sending SMS messages, you can also intercept incoming SMS messages using a <b>BroadcastReceiver</b> object.</p>
<p>To see how to receive SMS messages from within your Android application, lets look at the <i>AndroidManifest.xml</i> file within our receiver <a href="https://github.com/welsinga/sample_sms" title="Sample project" target="_blank">sample code</a>.</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;uses-permission</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.permission.RECEIVE_SMS&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;uses-permission</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.permission.BROADCAST_SMS&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;uses-permission</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.permission.READ_SMS&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></div></div>
<p>The <b>uses-permission</b> tags will allow the application to receive and read SMS messages. Lets create a class that extends the <b>BroadcastReceiver</b> class and override the onReceive() method:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&nbsp;@Override<br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onReceive<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+context"><span style="color: #003399;">Context</span></a> context, Intent intent<span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; Bundle bundle <span style="color: #339933;">=</span> intent.<span style="color: #006633;">getExtras</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> recMsgString <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> fromAddress <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; SmsMessage recMsg <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> data <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>bundle <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//---retrieve the SMS message received---</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+object"><span style="color: #003399;">Object</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> pdus <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+object"><span style="color: #003399;">Object</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> bundle.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pdus&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &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;">&lt;</span> pdus.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; recMsg <span style="color: #339933;">=</span> SmsMessage.<span style="color: #006633;">createFromPdu</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> pdus<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data <span style="color: #339933;">=</span> recMsg.<span style="color: #006633;">getUserData</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+exception"><span style="color: #003399;">Exception</span></a> e<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>data <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &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> index <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> index <span style="color: #339933;">&lt;</span> data.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>index<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; recMsgString <span style="color: #339933;">+=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+character"><span style="color: #003399;">Character</span></a>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#41;</span> data<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; fromAddress <span style="color: #339933;">=</span> recMsg.<span style="color: #006633;">getOriginatingAddress</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; Intent result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span>context, ReceiveActivity.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; result.<span style="color: #006633;">putExtra</span><span style="color: #009900;">&#40;</span>ReceiveActivity.<span style="color: #006633;">MESSAGE</span>, <span style="color: #0000ff;">&quot;Received SMS from &quot;</span> <span style="color: #339933;">+</span> fromAddress <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;: &quot;</span> <span style="color: #339933;">+</span> recMsgString<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; result.<span style="color: #006633;">addFlags</span><span style="color: #009900;">&#40;</span>Intent.<span style="color: #006633;">FLAG_ACTIVITY_NEW_TASK</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; context.<span style="color: #006633;">startActivity</span><span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>When SMS messages are received, the <b>onReceive()</b> method will be invoked. The SMS message is contained and attached to the Intent object via a <b>Bundle</b> object.<br />
The messages are stored in an Object array in the <b>PDU</b> format. To extract each message, you use the static <b>createFromPdu()</b> method from the <b>SmsMessage</b> class.<br />
The SMS message is then passed to an Activity that shows the message.</p>
<p>That&#8217;s it. We&#8217;ve managed to send and received a Data SMS within an Android application. Till next time.</p>
]]></content:encoded>
					
					<wfw:commentRss>http://wiebe-elsinga.com/blog/sending-and-receiving-data-sms-messages-with-android/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
		<item>
		<title>Fun with Android and NFC</title>
		<link>http://wiebe-elsinga.com/blog/fun-with-android-and-nfc/</link>
					<comments>http://wiebe-elsinga.com/blog/fun-with-android-and-nfc/#comments</comments>
		
		<dc:creator><![CDATA[W.Elsinga]]></dc:creator>
		<pubDate>Thu, 29 Nov 2012 03:25:48 +0000</pubDate>
				<category><![CDATA[Business]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[NFC]]></category>
		<guid isPermaLink="false">http://wiebe-elsinga.com/blog/?p=1269</guid>

					<description><![CDATA[So, you probably heard of something called NFC (Near Field Communication), and maybe you have seen the video (as shown below) from the 2012 Google I/O showing you a developers perspective on NFC. But what you want is a short &#8220;How to&#8221; on creating an Android NFC application. In this tutorial we’re going to build an app that reads and writes to an NFC tag. In order to follow along you will need an NFC enabled Android device, such as the Samsung Galaxy Nexus or Samsung Nexus 7. Also, but not mandatory, what you will need is an NFC tag or sticker. Be sure the NFC tag is pre-formatted because a bug in Android 4.0.2 may prevent you from encoding completely blank tags. Getting started Before we can make use of NFC we need to add a few new tags to our manifest XML. I recommend you open up the sample code, as I will be highlighting the key parts on this page. When you have the project imported, open up AndroidManifest.xml in the XML view. &#60;uses-feature android:name=&#34;android.hardware.nfc&#34; android:required=&#34;true&#34; /&#62; &#160; &#60;uses-permission android:name=&#34;android.permission.NFC&#34; /&#62; &#60;activity android:name=&#34;.ReadTagActivity&#34; &#160; &#160; &#160; &#160; &#160;android:screenOrientation=&#34;portrait&#34; &#160; &#160; &#160; &#160; &#160;android:label=&#34;@string/title_activity_readtag&#34; &#160; &#160; &#160; &#160; &#160;android:parentActivityName=&#34;.MainActivity&#34;&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#60;intent-filter&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#60;action android:name=&#34;android.nfc.action.NDEF_DISCOVERED&#34; /&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#60;category android:name=&#34;android.intent.category.DEFAULT&#34; /&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#60;data android:mimeType=&#34;application/com.elsinga.sample.nfc&#34; /&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#60;/intent-filter&#62; &#160; &#160; &#160; &#160; &#160; &#60;/activity&#62; The uses-permission tag will allow us to access the NFC chip in the device, and the uses-hardware tag will make it required, which will mean our app only appears to NFC enabled devices in Google Play. For our ReadTagActivity tag we add a new intent-filter, this will launch the activity when a tag is scanned that contains data in our format. We do this by specifying a custom mime-type, in this case I’ve chosen “application/com.elsinga.sample.nfc”. Android handles NFC tag scans by trying to find the best match for the data on the tag, providing several fallback mechanisms to make sure the tag is handled by the right app and allowing the user to pick from a chooser dialog when there is no clear target. The first thing we need to do is write some data to a blank NFC tag, so open up WriteTagActivity.java. This activity is launched when the user clicks the &#8220;Write button&#8221; in the app’s main activity. It displays a button that, when pressed, waits for a tag to write to. In our onCreate() we grab a reference to the NFC adapter for later use: _nfcAdapter = NfcAdapter.getDefaultAdapter&#40;this&#41;; The NfcAdapter allows us to begin listening to a tag being scanned, before any other app gets a chance. This is called “foreground dispatch”. When the user presses the button we call enableTagWriteMode() to begin this process. In this method we set up a PendingIntent to run when a tag gets scanned, in this case we simply want the Intent to launch the current Activity when any tag gets scanned, so set up no special filters. When the system dispatches ACTION_TAG_DISCOVERED, this Activity will launch and onNewIntent() will get called with the tag details. &#160; @Override &#160; protected void onNewIntent&#40;Intent intent&#41; &#160; &#123; &#160; &#160; if &#40;_writeMode&#41; &#160; &#160; &#123; &#160; &#160; &#160; if &#40;intent.getAction&#40;&#41;.equals&#40;NfcAdapter.ACTION_TAG_DISCOVERED&#41;&#41; &#160; &#160; &#160; &#123; &#160; &#160; &#160; &#160; Tag detectedTag = intent.getParcelableExtra&#40;NfcAdapter.EXTRA_TAG&#41;; &#160; &#160; &#160; &#160; writeTag&#40;buildNdefMessage&#40;&#41;, detectedTag&#41;; &#160; &#160; &#160; &#160; _imageViewImage.setImageDrawable&#40;getResources&#40;&#41;.getDrawable&#40;R.drawable.android_blue_logo&#41;&#41;; &#160; &#160; &#160; &#160; _editTextData.setEnabled&#40;true&#41;; &#160; &#160; &#160; &#125; &#160; &#160; &#125; &#160; &#125; In onNewIntent() we pull out the NFC Tag and begin writing to it in writeTag(). NDEF The data we store is organised into NDEF Records inside a single NDEF Message. The first thing we do is to determine whether the tag is already NDEF formatted by calling Ndef.get(tag). If it’s already NDEF formatted we check if it’s writeable and that the data is not too large. With all being well we write the data with ndef.writeNdefMessage(). That’s it, we’ve written to an NFC tag! If you go to the home screen, or exit the app, and re-scan the tag, it should now launch the ReadTagActivity and show the text. Open up ReadTagActivity.java to see how we read the information from the Intent: &#160; NdefMessage&#91;&#93; getNdefMessagesFromIntent&#40;Intent intent&#41; &#160; &#123; &#160; &#160; // Parse the intent &#160; &#160; NdefMessage&#91;&#93; msgs = null; &#160; &#160; String action = intent.getAction&#40;&#41;; &#160; &#160; if &#40;action.equals&#40;NfcAdapter.ACTION_TAG_DISCOVERED&#41; &#124;&#124; action.equals&#40;NfcAdapter.ACTION_NDEF_DISCOVERED&#41;&#41; &#160; &#160; &#123; &#160; &#160; &#160; Parcelable&#91;&#93; rawMsgs = intent.getParcelableArrayExtra&#40;NfcAdapter.EXTRA_NDEF_MESSAGES&#41;; &#160; &#160; &#160; if &#40;rawMsgs != null&#41; &#160; &#160; &#160; &#123; &#160; &#160; &#160; &#160; msgs = new NdefMessage&#91;rawMsgs.length&#93;; &#160; &#160; &#160; &#160; for &#40;int i = 0; i &#60; rawMsgs.length; i++&#41; &#160; &#160; &#160; &#160; &#123; &#160; &#160; &#160; &#160; &#160; msgs&#91;i&#93; = &#40;NdefMessage&#41; rawMsgs&#91;i&#93;; &#160; &#160; &#160; &#160; &#125; &#160; &#160; &#160; &#125; &#160; &#160; &#160; else &#160; &#160; &#160; &#123; &#160; &#160; &#160; &#160; // Unknown tag type &#160; &#160; &#160; &#160; byte&#91;&#93; empty = new byte&#91;&#93;&#123;&#125;; &#160; &#160; &#160; &#160; NdefRecord record = new NdefRecord&#40;NdefRecord.TNF_UNKNOWN, empty, empty, empty&#41;; &#160; &#160; &#160; &#160; NdefMessage msg = new NdefMessage&#40;new NdefRecord&#91;&#93;&#123;record&#125;&#41;; &#160; &#160; &#160; &#160; msgs = new NdefMessage&#91;&#93;&#123;msg&#125;; &#160; &#160; &#160; &#125; &#160; &#160; &#125; &#160; &#160; else &#160; &#160; &#123; &#160; &#160; &#160; Log.e&#40;TAG, &#34;Unknown intent.&#34;&#41;; &#160; &#160; &#160; finish&#40;&#41;; &#160; &#160; &#125; &#160; &#160; return msgs; &#160; &#125; With that determined we can go ahead and grab the NdefMessage from the Intent’s extras. In our case we know the first NdefRecord is the record we want, containing our written text in the payload: &#160; &#160; if &#40;getIntent&#40;&#41;.getAction&#40;&#41; != null&#41; &#160; &#160; &#123; &#160; &#160; &#160; if &#40;getIntent&#40;&#41;.getAction&#40;&#41;.equals&#40;NfcAdapter.ACTION_NDEF_DISCOVERED&#41;&#41; &#160; &#160; &#160; &#123; &#160; &#160; &#160; &#160; NdefMessage&#91;&#93; msgs = getNdefMessagesFromIntent&#40;getIntent&#40;&#41;&#41;; &#160; &#160; &#160; &#160; NdefRecord record = msgs&#91;0&#93;.getRecords&#40;&#41;&#91;0&#93;; &#160; &#160; &#160; &#160; byte&#91;&#93; payload = record.getPayload&#40;&#41;; &#160; &#160; &#160; &#160; String payloadString = new String&#40;payload&#41;; &#160; &#160; &#160; &#160; _textViewData.setText&#40;payloadString&#41;; &#160; &#160; &#160; &#125; &#160; &#160; &#125; &#160; &#160; _nfcAdapter.enableForegroundDispatch&#40;this, [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>So, you probably heard of something called NFC (Near Field Communication), and maybe you have seen the video (as shown below) from the 2012 Google I/O showing you a developers perspective on NFC.</p>
<p><center></p>
<div style="max-width:320px;" ><iframe src="http://www.youtube.com/embed/HkzPc8ZvCco?wmode=transparent" width="320" height="180" ></iframe></div>
<p></center>But what you want is a short &#8220;How to&#8221; on creating an Android NFC application. In this tutorial we’re going to build an app that reads and writes to an NFC tag.<br />
In order to follow along you will need an NFC enabled Android device, such as the Samsung Galaxy Nexus or Samsung Nexus 7. Also, but not mandatory, what you will need is an NFC tag or sticker. Be sure the NFC tag is pre-formatted because a bug in Android 4.0.2 may prevent you from encoding completely blank tags.</p>
<h2>Getting started</h2>
<p>Before we can make use of NFC we need to add a few new tags to our manifest XML. I recommend you open up the <a title="Github Sample code" href="https://github.com/welsinga/sample_nfc" target="_blank">sample code</a>, as I will be highlighting the key parts on this page. When you have the project imported, open up <i>AndroidManifest.xml</i> in the XML view.</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;uses-feature</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.hardware.nfc&quot;</span> <span style="color: #000066;">android:required</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; <br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;uses-permission</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.permission.NFC&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
<br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;activity</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;.ReadTagActivity&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">android:screenOrientation</span>=<span style="color: #ff0000;">&quot;portrait&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">android:label</span>=<span style="color: #ff0000;">&quot;@string/title_activity_readtag&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">android:parentActivityName</span>=<span style="color: #ff0000;">&quot;.MainActivity&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;intent-filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.nfc.action.NDEF_DISCOVERED&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;category</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.intent.category.DEFAULT&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;data</span> <span style="color: #000066;">android:mimeType</span>=<span style="color: #ff0000;">&quot;application/com.elsinga.sample.nfc&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/intent-filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/activity<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>The <b>uses-permission</b> tag will allow us to access the NFC chip in the device, and the <b>uses-hardware</b> tag will make it required, which will mean our app only appears to NFC enabled devices in Google Play.<br />
For our <strong>ReadTagActivity</strong> tag we add a new <strong>intent-filter</strong>, this will launch the activity when a tag is scanned that contains data in our format. We do this by specifying a custom <strong>mime-type</strong>, in this case I’ve chosen “<strong>application/com.elsinga.sample.nfc</strong>”. Android handles NFC tag scans by trying to find the best match for the data on the tag, providing several fallback mechanisms to make sure the tag is handled by the right app and allowing the user to pick from a chooser dialog when there is no clear target.</p>
<p>The first thing we need to do is write some data to a blank NFC tag, so open up <i>WriteTagActivity.java</i>. This activity is launched when the user clicks the &#8220;Write button&#8221; in the app’s main activity. It displays a button that, when pressed, waits for a tag to write to.</p>
<p>In our <strong>onCreate()</strong> we grab a reference to the NFC adapter for later use:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">_nfcAdapter <span style="color: #339933;">=</span> NfcAdapter.<span style="color: #006633;">getDefaultAdapter</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>The <strong>NfcAdapter</strong> allows us to begin listening to a tag being scanned, before any other app gets a chance. This is called “foreground dispatch”. When the user presses the button we call <strong>enableTagWriteMode()</strong> to begin this process. In this method we set up a <strong>PendingIntent</strong> to run when a tag gets scanned, in this case we simply want the <strong>Intent</strong> to launch the current Activity when any tag gets scanned, so set up no special filters. When the system dispatches <strong>ACTION_TAG_DISCOVERED</strong>, this Activity will launch and <strong>onNewIntent()</strong> will get called with the tag details.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&nbsp; @Override<br />
&nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onNewIntent<span style="color: #009900;">&#40;</span>Intent intent<span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>_writeMode<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>intent.<span style="color: #006633;">getAction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>NfcAdapter.<span style="color: #006633;">ACTION_TAG_DISCOVERED</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Tag detectedTag <span style="color: #339933;">=</span> intent.<span style="color: #006633;">getParcelableExtra</span><span style="color: #009900;">&#40;</span>NfcAdapter.<span style="color: #006633;">EXTRA_TAG</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; writeTag<span style="color: #009900;">&#40;</span>buildNdefMessage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, detectedTag<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; _imageViewImage.<span style="color: #006633;">setImageDrawable</span><span style="color: #009900;">&#40;</span>getResources<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getDrawable</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">drawable</span>.<span style="color: #006633;">android_blue_logo</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _editTextData.<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><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>In <strong>onNewIntent()</strong> we pull out the NFC Tag and begin writing to it in <strong>writeTag()</strong>.</p>
<h2>NDEF</h2>
<p>The data we store is organised into NDEF Records inside a single NDEF Message. The first thing we do is to determine whether the tag is already NDEF formatted by calling <strong>Ndef.get(tag)</strong>. If it’s already NDEF formatted we check if it’s writeable and that the data is not too large. With all being well we write the data with <strong>ndef.writeNdefMessage()</strong>. That’s it, we’ve written to an NFC tag!</p>
<p>If you go to the home screen, or exit the app, and re-scan the tag, it should now launch the <strong>ReadTagActivity</strong> and show the text.<br />
Open up <strong>ReadTagActivity.java</strong> to see how we read the information from the Intent:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&nbsp; NdefMessage<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> getNdefMessagesFromIntent<span style="color: #009900;">&#40;</span>Intent intent<span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Parse the intent</span><br />
&nbsp; &nbsp; NdefMessage<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> msgs <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> action <span style="color: #339933;">=</span> intent.<span style="color: #006633;">getAction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>action.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>NfcAdapter.<span style="color: #006633;">ACTION_TAG_DISCOVERED</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> action.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>NfcAdapter.<span style="color: #006633;">ACTION_NDEF_DISCOVERED</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; Parcelable<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> rawMsgs <span style="color: #339933;">=</span> intent.<span style="color: #006633;">getParcelableArrayExtra</span><span style="color: #009900;">&#40;</span>NfcAdapter.<span style="color: #006633;">EXTRA_NDEF_MESSAGES</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>rawMsgs <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; msgs <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> NdefMessage<span style="color: #009900;">&#91;</span>rawMsgs.<span style="color: #006633;">length</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &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;">&lt;</span> rawMsgs.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msgs<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>NdefMessage<span style="color: #009900;">&#41;</span> rawMsgs<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Unknown tag type</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> empty <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; NdefRecord record <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> NdefRecord<span style="color: #009900;">&#40;</span>NdefRecord.<span style="color: #006633;">TNF_UNKNOWN</span>, empty, empty, empty<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; NdefMessage msg <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> NdefMessage<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> NdefRecord<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>record<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; msgs <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> NdefMessage<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>msg<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">else</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">e</span><span style="color: #009900;">&#40;</span>TAG, <span style="color: #0000ff;">&quot;Unknown intent.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; finish<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> msgs<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>With that determined we can go ahead and grab the NdefMessage from the Intent’s extras. In our case we know the first <strong>NdefRecord</strong> is the record we want, containing our written text in the payload:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>getIntent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getAction</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;">null</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>getIntent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getAction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>NfcAdapter.<span style="color: #006633;">ACTION_NDEF_DISCOVERED</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; NdefMessage<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> msgs <span style="color: #339933;">=</span> getNdefMessagesFromIntent<span style="color: #009900;">&#40;</span>getIntent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; NdefRecord record <span style="color: #339933;">=</span> msgs<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #006633;">getRecords</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> payload <span style="color: #339933;">=</span> record.<span style="color: #006633;">getPayload</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> payloadString <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#40;</span>payload<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; _textViewData.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>payloadString<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; _nfcAdapter.<span style="color: #006633;">enableForegroundDispatch</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, _nfcPendingIntent, _readTagFilters, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>That&#8217;s it. We have managed to write custom data to an NFC tag, and read back that custom data.<br />
Have a look at the <a title="Github Sample code" href="https://github.com/welsinga/sample_nfc" target="_blank">sample code</a> and also discover how to beam data from one NFC-enabled device to another.</p>
]]></content:encoded>
					
					<wfw:commentRss>http://wiebe-elsinga.com/blog/fun-with-android-and-nfc/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
		<item>
		<title>How-To: Android emulator performance tuning tips</title>
		<link>http://wiebe-elsinga.com/blog/android-emulator-performance-tuning-tips/</link>
					<comments>http://wiebe-elsinga.com/blog/android-emulator-performance-tuning-tips/#comments</comments>
		
		<dc:creator><![CDATA[W.Elsinga]]></dc:creator>
		<pubDate>Sun, 10 Jul 2011 23:30:10 +0000</pubDate>
				<category><![CDATA[Business]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Emulator]]></category>
		<category><![CDATA[Honeycomb]]></category>
		<category><![CDATA[Tips]]></category>
		<guid isPermaLink="false">http://wiebe-elsinga.com/blog/?p=674</guid>

					<description><![CDATA[As some of you may already noticed, running Android Honeycomb application on the Android emulator, isn&#8217;t the most fun thing to do, because it&#8217;s painfully slow. Besides the start-up time (can be avoided, which I explain later), just using it is a pain. Google is planning to improve the performance, but if you can&#8217;t wait that long I’ve put together a few tips which may help improve your experience. &#8211; Emulated memory size. By default the Honeycomb AVD is created to emulate 256MB of RAM, my advice is to increase this significant, I’m using 1GB (1024MB). If you’re creating the AVD for Honeycomb in Eclipse you’ll find the RAM setting in the Hardware section under “Device ram size”. If you already have an AVD you can increase the setting by finding the directory you’re AVD is in (e.g. .android/avd/honeycomb.avd), editing the emulator-user.ini file, and changing hw.ramSize to whatever you want the ram size to be in MB. &#8211; Enable Snapshots. Enabling snapshots on an AVD allows the emulator to start-up an AVD in the state it was in when you closed it. This means that it doesn’t have to go through the boot-up procedure each time, which will save you a reasonable chunk of time. First on the initial launch make sure you select “Save to snapshot”. Get the emulator up and running and put it into the state you want. I like to have it start at the desktop (home screen) view. Now close the emulator. I found that it wasn’t very responsive when closing, so wait a bit, watch the mouse icon go to “busy” and let it write everything to disk. Now, launch the emulator again and this time uncheck “Save to snapshot” but make sure Launch from snapshot is chosen. Voila! The emulator launches quickly. And when you save it it shuts down quickly because it’s not saving anymore.]]></description>
										<content:encoded><![CDATA[<p>As some of you may already noticed, running Android Honeycomb application on the Android emulator, isn&#8217;t the most fun thing to do, because it&#8217;s painfully slow. Besides the start-up time (can be avoided, which I explain later), just using it is a pain. Google is planning to improve the performance, but if you can&#8217;t wait that long I’ve put together a few tips which may help improve your experience.<br />
<span id="more-674"></span><br />
&#8211; <em><strong>Emulated memory size</strong></em>.<br />
By default the Honeycomb <em>AVD</em> is created to emulate 256MB of RAM, my advice is to increase this significant, I’m using 1GB (1024MB).<br />
If you’re creating the <em>AVD</em> for Honeycomb in Eclipse you’ll find the RAM setting in the Hardware section under “<em>Device ram size</em>”. If you already have an <em>AVD</em> you can increase the setting by finding the directory you’re <em>AVD</em> is in (e.g. .android/avd/honeycomb.avd), editing the <em>emulator-user.ini</em> file, and changing <em>hw.ramSize</em> to whatever you want the ram size to be in MB.</p>
<p>&#8211; <em><strong>Enable Snapshots</strong></em>.<br />
Enabling snapshots on an <em>AVD</em> allows the emulator to start-up an <em>AVD</em> in the state it was in when you closed it. This means that it doesn’t have to go through the boot-up procedure each time, which will save you a reasonable chunk of time.<br />
First on the initial launch make sure you select “<em>Save to snapshot</em>”. Get the emulator up and running and put it into the state you want. I like to have it start at the desktop (home screen) view. Now close the emulator. I found that it wasn’t very responsive when closing, so wait a bit, watch the mouse icon go to “busy” and let it write everything to disk.<br />
Now, launch the emulator again and this time uncheck “<em>Save to snapshot</em>” but make sure Launch from snapshot is chosen. Voila! The emulator launches quickly. And when you save it it shuts down quickly because it’s not saving anymore.</p>
<p><a href="http://wiebe-elsinga.com/blog/wp-content/uploads/2011/07/snapshot_1.png" rel="lightbox[674]" title="snapshot_1"><img loading="lazy" class="size-medium wp-image-676 alignnone" title="snapshot_1" src="http://wiebe-elsinga.com/blog/wp-content/uploads/2011/07/snapshot_1-212x300.png" alt="" width="212" height="300" srcset="http://wiebe-elsinga.com/blog/wp-content/uploads/2011/07/snapshot_1-212x300.png 212w, http://wiebe-elsinga.com/blog/wp-content/uploads/2011/07/snapshot_1.png 259w" sizes="(max-width: 212px) 100vw, 212px" /></a> <a href="http://wiebe-elsinga.com/blog/wp-content/uploads/2011/07/snapshot_2.png" rel="lightbox[674]" title="snapshot_2"><img loading="lazy" class="size-medium wp-image-677 alignright" title="snapshot_2" src="http://wiebe-elsinga.com/blog/wp-content/uploads/2011/07/snapshot_2-212x300.png" alt="" width="212" height="300" srcset="http://wiebe-elsinga.com/blog/wp-content/uploads/2011/07/snapshot_2-212x300.png 212w, http://wiebe-elsinga.com/blog/wp-content/uploads/2011/07/snapshot_2-213x300.png 213w, http://wiebe-elsinga.com/blog/wp-content/uploads/2011/07/snapshot_2.png 259w" sizes="(max-width: 212px) 100vw, 212px" /></a></p>
]]></content:encoded>
					
					<wfw:commentRss>http://wiebe-elsinga.com/blog/android-emulator-performance-tuning-tips/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>How-To: Android Fragments</title>
		<link>http://wiebe-elsinga.com/blog/android-fragment/</link>
					<comments>http://wiebe-elsinga.com/blog/android-fragment/#comments</comments>
		
		<dc:creator><![CDATA[W.Elsinga]]></dc:creator>
		<pubDate>Tue, 22 Mar 2011 02:12:10 +0000</pubDate>
				<category><![CDATA[Business]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Fragment]]></category>
		<category><![CDATA[Honeycomb]]></category>
		<guid isPermaLink="false">http://wiebe-elsinga.com/blog/?p=578</guid>

					<description><![CDATA[Typically, an Android user interface is composed of views within layouts, such as a ListView within a LinearLayout. A hierarchy of View objects gets loaded or created when an Activity is started. On small screens, this is fine. But when you spread a UI out over the surface of a tablet&#8217;s screen, it calls for a different style of interaction. Some parts of the screen should remain constant over longer durations than others While developers can implement interactions where parts of the screen change by showing and hiding views, Android&#8217;s developers decided they needed more than just convention to encourage better large screen UIs and consistent implementation of the feel of the UI. Now, in order to facilitate this new kind of interaction, as part of the Android 3.0 SDK, we get a class called Fragment. A Fragment object is something between a View and and Activity: It can be part of a layout, but it isn&#8217;t a subclass of View. It implements the ComponentCallbacks interface, and it has a lifecycle, but that lifecycle is dependent on the Activity the Fragment object belongs to. Let&#8217;s see what it can do in a tablet-sized user interface. Here are the contents of the main.xml file that describes a screen layout for our application. In a list on the left of the screen some numbers will appear. Selecting a number from that list will create that many Android icons, displayed in a Fragment instance on the right. &#60;!--?xml version=&#34;1.0&#34; encoding=&#34;utf-8&#34;?--&#62; xmlns:android=&#34;http://schemas.android.com/apk/res/android&#34; android:orientation=&#34;horizontal&#34; android:layout_width=&#34;match_parent&#34; android:layout_height=&#34;match_parent&#34; android:id=&#34;@+id/frags&#34;&#62; android:id=&#34;@+id/number_list&#34; android:layout_width=&#34;250dip&#34; android:layout_height=&#34;match_parent&#34; /&#62; android:id=&#34;@+id/the_frag&#34; android:layout_width=&#34;match_parent&#34; android:layout_height=&#34;match_parent&#34; /&#62; The code for the Fragment subclass referred to in the layout in main.xml is below. package com.my; import android.app.Fragment; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; public class FragmentExample extends Fragment &#123; private int nAndroids; public FragmentExample&#40;&#41; &#123; &#125; /** * Constructor for being created explicitly */ public FragmentExample&#40;int nAndroids&#41; &#123; this.nAndroids = nAndroids; &#125; /** * If we are being created with saved state, restore our state */ @Override public void onCreate&#40;Bundle saved&#41; &#123; super.onCreate&#40;saved&#41;; if &#40;null != saved&#41; &#123; nAndroids = saved.getInt&#40;&#34;nAndroids&#34;&#41;; &#125; &#125; /** * Save the number of Androids to be displayed */ @Override public void onSaveInstanceState&#40;Bundle toSave&#41; &#123; toSave.putInt&#40;&#34;nAndroids&#34;, nAndroids&#41;; &#125; /** * Make a grid and fill it with n Androids */ @Override public View onCreateView&#40;LayoutInflater inflater, ViewGroup container, Bundle saved&#41; &#123; int n; Context c = getActivity&#40;&#41;.getApplicationContext&#40;&#41;; LinearLayout l = new LinearLayout&#40;c&#41;; for &#40;n = 0; n &#60; nAndroids; n++&#41; &#123; ImageView i = new ImageView&#40;c&#41;; i.setImageResource&#40;R.drawable.android&#41;; l.addView&#40;i&#41;; &#125; return l; &#125; &#125; package com.my; import android.app.Activity; import android.app.Fragment; import android.app.FragmentTransaction; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.AdapterView.OnItemClickListener; public class FragmentExampleActivity extends Activity implements OnItemClickListener &#123; /** Called when the activity is first created. */ @Override public void onCreate&#40;Bundle savedInstanceState&#41; &#123; super.onCreate&#40;savedInstanceState&#41;; setContentView&#40;R.layout.main&#41;; ListView l = &#40;ListView&#41; findViewById&#40;R.id.number_list&#41;; ArrayAdapter numbers = new ArrayAdapter&#40; getApplicationContext&#40;&#41;, android.R.layout.simple_list_item_1, new String&#91;&#93; &#123; &#34;one&#34;, &#34;two&#34;, &#34;three&#34;, &#34;four&#34;, &#34;five&#34;, &#34;six&#34; &#125;&#41;; l.setAdapter&#40;numbers&#41;; l.setOnItemClickListener&#40;this&#41;; &#125; /** * Add a Fragment to our stack with n Androids in it */ private void stackAFragment&#40;int nAndroids&#41; &#123; Fragment f = new FragmentExample&#40;nAndroids&#41;; FragmentTransaction ft = getFragmentManager&#40;&#41;.beginTransaction&#40;&#41;; ft.replace&#40;R.id.the_frag, f&#41;; ft.setTransition&#40;FragmentTransaction.TRANSIT_FRAGMENT_OPEN&#41;; ft.addToBackStack&#40;null&#41;; ft.commit&#40;&#41;; &#125; /** * Called when a number gets clicked */ public void onItemClick&#40;AdapterView&#60;!--?--&#62; parent, View view, int position, long id&#41; &#123; stackAFragment&#40;position + 1&#41;; &#125; &#125; This is what you see when you when you run the program: Initially, you see a list of numbers, and a blank area to the right. Touch a number, and a new Fragment is created, with the specified number of Android icons. If you keep touching numbers in the list on the left, the corresponding number of Android icons appears in the Fragment instance on the right. Now try the back button. Instead of returning to an instance of the Activity that was on the screen before this application launched, you see the previous Fragment instance on the right. That is, the Fragment objects have been integrated into the task&#8217;s back stack. Fragments are a new feature of Android, and this brief example only scratches the surface of how Fragment will change the way Android UIs are implemented. You can see from this example that using Fragments is efficient and expressive. You can read more about Fragment, and how it can be used alongside starting Activity objects to drill into detail views in an Android UI in an article by Dianne Hackborn, here. Download source code FragmentExample [19kB]]]></description>
										<content:encoded><![CDATA[<p>Typically, an Android user interface is composed of views within layouts, such as a ListView within a LinearLayout. A hierarchy of View objects gets loaded or created when an Activity is started. On small screens, this is fine.<br />
But when you spread a UI out over the surface of a tablet&#8217;s screen, it calls for a different style of interaction. Some parts of the screen should remain constant over longer durations than others</p>
<p>While developers can implement interactions where parts of the screen change by showing and hiding views, Android&#8217;s developers decided they needed more than just convention to encourage better large screen UIs and consistent implementation of the feel of the UI. Now, in order to facilitate this new kind of interaction, as part of the Android 3.0 SDK, we get a class called Fragment.<br />
<span id="more-578"></span><br />
A Fragment object is something between a View and and Activity: It can be part of a layout, but it isn&#8217;t a subclass of View.<br />
It implements the ComponentCallbacks interface, and it has a lifecycle, but that lifecycle is dependent on the Activity the Fragment object belongs to.<br />
Let&#8217;s see what it can do in a tablet-sized user interface.</p>
<p>Here are the contents of the <em>main.xml</em> file that describes a screen layout for our application. In a list on the left of the screen some numbers will appear. Selecting a number from that list will create that many Android icons, displayed in a Fragment instance on the right.</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #808080; font-style: italic;">&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?--&gt;</span><br />
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br />
android:orientation=&quot;horizontal&quot;<br />
android:layout_width=&quot;match_parent&quot;<br />
android:layout_height=&quot;match_parent&quot;<br />
android:id=&quot;@+id/frags&quot;&gt;<br />
<br />
android:id=&quot;@+id/number_list&quot;<br />
android:layout_width=&quot;250dip&quot;<br />
android:layout_height=&quot;match_parent&quot; /&gt;<br />
<br />
android:id=&quot;@+id/the_frag&quot;<br />
android:layout_width=&quot;match_parent&quot;<br />
android:layout_height=&quot;match_parent&quot; /&gt;</div></div>
<p>The code for the Fragment subclass referred to in the layout in <em>main.xml</em> is below.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.my</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.Fragment</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.content.Context</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.LayoutInflater</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.View</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.ViewGroup</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.ImageView</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.LinearLayout</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FragmentExample <span style="color: #000000; font-weight: bold;">extends</span> Fragment <span style="color: #009900;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> nAndroids<span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> FragmentExample<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
* Constructor for being created explicitly<br />
*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> FragmentExample<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> nAndroids<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">nAndroids</span> <span style="color: #339933;">=</span> nAndroids<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
* If we are being created with saved state, restore our state<br />
*/</span><br />
@Override<br />
<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 saved<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>saved<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">!=</span> saved<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
nAndroids <span style="color: #339933;">=</span> saved.<span style="color: #006633;">getInt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;nAndroids&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
* Save the number of Androids to be displayed<br />
*/</span><br />
@Override<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onSaveInstanceState<span style="color: #009900;">&#40;</span>Bundle toSave<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
toSave.<span style="color: #006633;">putInt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;nAndroids&quot;</span>, nAndroids<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
* Make a grid and fill it with n Androids<br />
*/</span><br />
@Override<br />
<span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+view"><span style="color: #003399;">View</span></a> onCreateView<span style="color: #009900;">&#40;</span>LayoutInflater inflater, ViewGroup container, Bundle saved<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #000066; font-weight: bold;">int</span> n<span style="color: #339933;">;</span><br />
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+context"><span style="color: #003399;">Context</span></a> c <span style="color: #339933;">=</span> getActivity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getApplicationContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
LinearLayout l <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LinearLayout<span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>n <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> n <span style="color: #339933;">&lt;</span> nAndroids<span style="color: #339933;">;</span> n<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
ImageView i <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ImageView<span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
i.<span style="color: #006633;">setImageResource</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">drawable</span>.<span style="color: #006633;">android</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
l.<span style="color: #006633;">addView</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">return</span> l<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.my</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.Activity</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.Fragment</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.FragmentTransaction</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.View</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.AdapterView</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.ArrayAdapter</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.ListView</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.AdapterView.OnItemClickListener</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FragmentExampleActivity <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #000000; font-weight: bold;">implements</span><br />
OnItemClickListener <span style="color: #009900;">&#123;</span><br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/** Called when the activity is first created. */</span><br />
@Override<br />
<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><br />
<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><br />
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><br />
<br />
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+listview"><span style="color: #003399;">ListView</span></a> l <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+listview"><span style="color: #003399;">ListView</span></a><span style="color: #009900;">&#41;</span> findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">number_list</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
ArrayAdapter numbers <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayAdapter<span style="color: #009900;">&#40;</span><br />
getApplicationContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, android.<span style="color: #006633;">R</span>.<span style="color: #006633;">layout</span>.<span style="color: #006633;">simple_list_item_1</span>,<br />
<span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;one&quot;</span>, <span style="color: #0000ff;">&quot;two&quot;</span>, <span style="color: #0000ff;">&quot;three&quot;</span>, <span style="color: #0000ff;">&quot;four&quot;</span>, <span style="color: #0000ff;">&quot;five&quot;</span>, <span style="color: #0000ff;">&quot;six&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
l.<span style="color: #006633;">setAdapter</span><span style="color: #009900;">&#40;</span>numbers<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
l.<span style="color: #006633;">setOnItemClickListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
* Add a Fragment to our stack with n Androids in it<br />
*/</span><br />
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> stackAFragment<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> nAndroids<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
Fragment f <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FragmentExample<span style="color: #009900;">&#40;</span>nAndroids<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
FragmentTransaction ft <span style="color: #339933;">=</span> getFragmentManager<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">beginTransaction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
ft.<span style="color: #006633;">replace</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">the_frag</span>, f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
ft.<span style="color: #006633;">setTransition</span><span style="color: #009900;">&#40;</span>FragmentTransaction.<span style="color: #006633;">TRANSIT_FRAGMENT_OPEN</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
ft.<span style="color: #006633;">addToBackStack</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
ft.<span style="color: #006633;">commit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
* Called when a number gets clicked<br />
*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onItemClick<span style="color: #009900;">&#40;</span>AdapterView<span style="color: #339933;">&lt;!--?--&gt;</span> parent, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+view"><span style="color: #003399;">View</span></a> view, <span style="color: #000066; font-weight: bold;">int</span> position, <span style="color: #000066; font-weight: bold;">long</span> id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
stackAFragment<span style="color: #009900;">&#40;</span>position <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>This is what you see when you when you run the program: Initially, you see a list of numbers, and a blank area to the right.</p>
<p><a href="http://wiebe-elsinga.com/blog/wp-content/uploads/2011/03/frag1.gif" rel="lightbox[578]" title="frag1"><img loading="lazy" class="aligncenter size-medium wp-image-581" title="frag1" src="http://wiebe-elsinga.com/blog/wp-content/uploads/2011/03/frag1-300x191.gif" alt="" width="300" height="191" srcset="http://wiebe-elsinga.com/blog/wp-content/uploads/2011/03/frag1-300x191.gif 300w, http://wiebe-elsinga.com/blog/wp-content/uploads/2011/03/frag1.gif 669w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>Touch a number, and a new Fragment is created, with the specified number of Android icons. If you keep touching numbers in the list on the left, the corresponding number of Android icons appears in the Fragment instance on the right. Now try the back button. Instead of returning to an instance of the Activity that was on the screen before this application launched, you see the previous Fragment instance on the right. That is, the Fragment objects have been integrated into the task&#8217;s back stack.</p>
<p><a href="http://wiebe-elsinga.com/blog/wp-content/uploads/2011/03/frag2.gif" rel="lightbox[578]" title="frag2"><img loading="lazy" class="aligncenter size-medium wp-image-582" title="frag2" src="http://wiebe-elsinga.com/blog/wp-content/uploads/2011/03/frag2-300x192.gif" alt="" width="300" height="192" srcset="http://wiebe-elsinga.com/blog/wp-content/uploads/2011/03/frag2-300x193.gif 300w, http://wiebe-elsinga.com/blog/wp-content/uploads/2011/03/frag2.gif 666w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>Fragments are a new feature of Android, and this brief example only scratches the surface of how Fragment will change the way Android UIs are implemented. You can see from this example that using Fragments is efficient and expressive.<br />
You can read more about Fragment, and how it can be used alongside starting Activity objects to drill into detail views in an Android UI in an article by Dianne Hackborn, <a href="http://android-developers.blogspot.com/2011/02/android-30-fragments-api.html">here</a>.</p>
<p>Download source code <a href="http://wiebe-elsinga.com/blog/wp-content/uploads/2011/03/FragmentExample.zip">FragmentExample</a> [19kB]</p>
]]></content:encoded>
					
					<wfw:commentRss>http://wiebe-elsinga.com/blog/android-fragment/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
		<item>
		<title>How-To: Debug Android UI</title>
		<link>http://wiebe-elsinga.com/blog/debug-android-ui/</link>
					<comments>http://wiebe-elsinga.com/blog/debug-android-ui/#respond</comments>
		
		<dc:creator><![CDATA[W.Elsinga]]></dc:creator>
		<pubDate>Tue, 15 Mar 2011 00:47:53 +0000</pubDate>
				<category><![CDATA[Business]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[HierarchyViewer]]></category>
		<category><![CDATA[Layoutopt]]></category>
		<guid isPermaLink="false">http://wiebe-elsinga.com/blog/?p=542</guid>

					<description><![CDATA[When debugging an Android application it’s important to look at the structure of the UI. There are tools that help you visualize the UI. Inside this article I will be explaining two tools: Hierarchy Viewer Layoutopt Hierarchy Viewer The Hierarchy Viewer application provides a visual representation of the layout&#8217;s View hierarchy (the Layout View) and a magnified inspector of the display (the Pixel Perfect View). So lets run the tool. Connect your device or launch an emulator From a terminal, launch hierarchyviewer from your SDK /tools directory. In the window that opens, you&#8217;ll see a list of Devices. When a device is selected, a list of currently active Windows is displayed on the right. The focused window is the window currently in the foreground, and also the default window loaded if you do not select another. Select the window that you&#8217;d like to inspect and click Load View Hierarchy. The Layout View will be loaded. You can then load the Pixel Perfect View by clicking the second icon at the bottom-left of the window. Layoutopt Layoutopt is a command line tool which can analyse a layout resource file and tell you what is not required. So lets run the tool. From a terminal, launch layoutopt from your SDK /tools directory. The argument is a space- delimited list of resources you want to analyze, either non-compiled resource xml files or directories of such files.]]></description>
										<content:encoded><![CDATA[<p>When debugging an Android application it’s important to look at the structure of the UI. There are tools that help you visualize the UI. Inside this article I will be explaining two tools:</p>
<ul>
<li>Hierarchy Viewer</li>
<li>Layoutopt</li>
</ul>
<p><span id="more-542"></span></p>
<h2>Hierarchy Viewer</h2>
<p>The <a href="http://developer.android.com/guide/developing/tools/hierarchy-viewer.html">Hierarchy Viewer</a> application provides a visual representation of the layout&#8217;s View hierarchy (the Layout View) and a magnified inspector of the display (the Pixel Perfect View). So lets run the tool.</p>
<ol>
<li>Connect your device or launch an emulator</li>
<li>From a terminal, launch <strong>hierarchyviewer</strong> from your SDK /tools directory.</li>
<li> In the window that opens, you&#8217;ll see a list of Devices. When a device is selected, a list of currently active Windows is displayed on the right. The <em>focused window</em> is the window currently in the foreground, and also the default window loaded if you do not select another.</li>
<li> Select the window that you&#8217;d like to inspect and click Load View Hierarchy. The Layout View will be loaded. You can then load the Pixel Perfect View by clicking the second icon at the bottom-left of the window.</li>
</ol>
<p><a href="http://wiebe-elsinga.com/blog/wp-content/uploads/2011/03/HierarchyViewer.gif" rel="lightbox[542]" title="HierarchyViewer"><img loading="lazy" class="aligncenter size-thumbnail wp-image-543" title="HierarchyViewer" src="http://wiebe-elsinga.com/blog/wp-content/uploads/2011/03/HierarchyViewer-150x150.gif" alt="" width="150" height="150" /></a></p>
<h2>Layoutopt</h2>
<p><a href="http://developer.android.com/guide/developing/tools/layoutopt.html">Layoutopt</a> is a command line tool which can analyse a layout resource file and tell you what is not required. So lets run the tool.</p>
<ol>
<li>From a terminal, launch <strong>layoutopt</strong> from your SDK /tools directory.</li>
<li>The argument is a space- delimited list of resources you want to analyze, either non-compiled resource xml files or directories of such files.</li>
</ol>
<p><a href="http://wiebe-elsinga.com/blog/wp-content/uploads/2011/03/Layoutopt.gif" rel="lightbox[542]" title="Layoutopt"><img loading="lazy" class="aligncenter size-thumbnail wp-image-544" title="Layoutopt" src="http://wiebe-elsinga.com/blog/wp-content/uploads/2011/03/Layoutopt-150x150.gif" alt="" width="150" height="150" /></a></p>
]]></content:encoded>
					
					<wfw:commentRss>http://wiebe-elsinga.com/blog/debug-android-ui/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How-To: Android and Databases</title>
		<link>http://wiebe-elsinga.com/blog/android-and-databases/</link>
					<comments>http://wiebe-elsinga.com/blog/android-and-databases/#comments</comments>
		
		<dc:creator><![CDATA[W.Elsinga]]></dc:creator>
		<pubDate>Tue, 19 Oct 2010 04:52:03 +0000</pubDate>
				<category><![CDATA[Business]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[DB4o]]></category>
		<category><![CDATA[SQLite]]></category>
		<category><![CDATA[Tutorial]]></category>
		<guid isPermaLink="false">http://wiebe-elsinga.com/blog/?p=415</guid>

					<description><![CDATA[Applications usually uses databases to store application data. Inside this article I will be explaining two implementation types: SQLite DB4o I will use the Android application which was developed inside my previous post. SQLite SQLite is a lightweight relational database engine. SQLite is fast and has a small footprint, making it perfect for Android devices. Instead of the heavyweight server-based databases like Oracle and Microsoft SQL Server, each SQLite database is stored within a single file on disk. So lets set-up an implementation. Create a DB Helper Class public DbHelper&#40;Context context&#41; &#123; &#160; &#160; &#160;super&#40;context, DATABASE_NAME, null, DATABASE_VERSION&#41;; &#125; @Override public void onCreate&#40;SQLiteDatabase sqliteDb&#41; &#123; &#160; &#160; &#160;String sql = &#34;CREATE TABLE &#34; + TABLE_NAME + &#34; (&#34; + COLOMN_NAME+ &#34; LONGTEXT&#34; + &#34;);&#34;; &#160; &#160; &#160;sqliteDb.execSQL&#40;sql&#41;; &#125; @Override public void onUpgrade&#40;SQLiteDatabase sqliteDb, int oldVersion, &#160; &#160; &#160; &#160; &#160; int newVersion&#41; &#123; &#160; &#160; &#160;Log.i&#40;DbHelper.class.getName&#40;&#41;, &#34;Upgrading database from version &#34; + oldVersion + &#34; to &#34; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;+ newVersion + &#34;, which will destroy all old data&#34;&#41;; &#160; &#160; &#160;sqliteDb.execSQL&#40;&#34;DROP TABLE IF EXISTS &#34; + TABLE_NAME&#41;; &#160; &#160; &#160;onCreate&#40;sqliteDb&#41;; &#125; Create a Data layer class. This class will be responsible for using our DbHelper to perform our desired CRUD functions. public void addSearchResult&#40;String text&#41; &#123; &#160; &#160; &#160;SQLiteDatabase db = _dbHelper.getWritableDatabase&#40;&#41;; &#160; &#160; &#160;try &#123; &#160; &#160; &#160; &#160; &#160; ContentValues values = new ContentValues&#40;&#41;; &#160; &#160; &#160; &#160; &#160; values.put&#40;_dbHelper.COLOMN_NAME, text&#41;; &#160; &#160; &#160; &#160; &#160; db.insert&#40;_dbHelper.TABLE_NAME, &#34;&#34;, values&#41;; &#160; &#160; &#160;&#125; finally &#123; &#160; &#160; &#160; &#160; &#160; if &#40;db != null&#41; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;db.close&#40;&#41;; &#160; &#160; &#160;&#125; &#125; public void clearHistory&#40;&#41; &#123; &#160; &#160; &#160;SQLiteDatabase db = _dbHelper.getWritableDatabase&#40;&#41;; &#160; &#160; &#160;try &#123; &#160; &#160; &#160; &#160; &#160; db.delete&#40;_dbHelper.TABLE_NAME, &#34;&#34;, null&#41;; &#160; &#160; &#160;&#125; finally &#123; &#160; &#160; &#160; &#160; &#160; if &#40;db != null&#41; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;db.close&#40;&#41;; &#160; &#160; &#160;&#125; &#125; public List&#60;String&#62; getHistory&#40;&#41; &#123; &#160; &#160; &#160;SQLiteDatabase db = _dbHelper.getWritableDatabase&#40;&#41;; &#160; &#160; &#160;try &#123; &#160; &#160; &#160; &#160; &#160; List&#60;String&#62; results = new ArrayList&#60;String&#62;&#40;&#41;; &#160; &#160; &#160; &#160; &#160; Cursor c = db.rawQuery&#40;&#34;select * from &#34; + _dbHelper.TABLE_NAME, null&#41;; &#160; &#160; &#160; &#160; &#160; if &#40;c.getCount&#40;&#41; &#62; 0&#41; &#123; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;c.moveToFirst&#40;&#41;; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;do &#123; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; results.add&#40;c.getString&#40;c.getColumnIndex&#40;_dbHelper.COLOMN_NAME&#41;&#41;&#41;; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;&#125; while &#40;c.moveToNext&#40;&#41;&#41;; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;&#125; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;return results; &#160; &#160; &#160;&#125; finally &#123; &#160; &#160; &#160; &#160; &#160; if &#40;db != null&#41; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;db.close&#40;&#41;; &#160; &#160; &#160;&#125; &#125; Add CRUD functions to your Android application Db dB = new Db&#40;getBaseContext&#40;&#41;&#41;; dB.addSearchResult&#40;result&#41;; private void clearHistory&#40;&#41; &#123; &#160; &#160; &#160;Db dB = new Db&#40;getBaseContext&#40;&#41;&#41;; &#160; &#160; &#160;dB.clearHistory&#40;&#41;; &#125; private void displayHistory&#40;&#41; &#123; &#160; &#160; &#160;Db dB = new Db&#40;getBaseContext&#40;&#41;&#41;; &#160; &#160; &#160;List&#60;String&#62; history = dB.getHistory&#40;&#41;; &#160; &#160; &#160; &#160; &#160; &#160;ArrayAdapter&#60;String&#62; mHistory = new ArrayAdapter&#60;String&#62;&#40;this, R.layout.row, R.id.ENTRY_CELL, history&#41;; &#160; &#160; &#160; &#160; &#160; &#160;historyList.setAdapter&#40;mHistory&#41;; &#125; Download source code AndroidSQLLite.zip [20kB] DB4o db4o is an object database, so no mapping of tables in a relational model. You can persist complex objects with nested collections or other complex objects, any level of complexity in your hierarchy. You&#8217;re not limited to flat objects with primitive types to get a reasonable performance. So lets set-up an implementation. Create a persistent Object public class HistoryItem &#123; &#160; &#160; &#160; &#160; &#160; &#160;private String item; &#160; &#160; &#160; &#160; &#160; &#160;public HistoryItem&#40;&#41; &#123; &#160; &#160; &#160;&#125; &#160; &#160; &#160; &#160; &#160; &#160;public HistoryItem&#40;String item&#41; &#123; &#160; &#160; &#160; &#160; &#160; this.item= item; &#160; &#160; &#160;&#125; &#160; &#160; &#160;public void setItem&#40;String item&#41; &#123; &#160; &#160; &#160; &#160; &#160; this.item = item; &#160; &#160; &#160;&#125; &#160; &#160; &#160;public String getItem&#40;&#41; &#123; &#160; &#160; &#160; &#160; &#160; return item; &#160; &#160; &#160;&#125; &#125; Create a DB Helper Class public DbHelper&#40;Context context&#41; &#123; &#160; &#160; &#160;try &#123; &#160; &#160; &#160; &#160; &#160; if &#40;database == null &#124;&#124; database.ext&#40;&#41;.isClosed&#40;&#41;&#41; &#123; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;database = Db4oEmbedded.openFile&#40;configure&#40;&#41;, db4oDBFullPath&#40;context&#41;&#41;; &#160; &#160; &#160; &#160; &#160; &#125; &#160; &#160; &#160;&#125; catch &#40;Exception ie&#41; &#123; &#160; &#160; &#160; &#160; &#160; Log.e&#40;DbHelper.class.getName&#40;&#41;, ie.toString&#40;&#41;&#41;; &#160; &#160; &#160;&#125; &#125; private static EmbeddedConfiguration configure&#40;&#41; &#123; &#160; &#160; &#160;EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration&#40;&#41;; &#160; &#160; &#160;configuration.common&#40;&#41;.objectClass&#40;HistoryItem.class&#41;.objectField&#40; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;&#34;item&#34;&#41;.indexed&#40;true&#41;; &#160; &#160; &#160;configuration.common&#40;&#41;.objectClass&#40;HistoryItem.class&#41;.cascadeOnUpdate&#40;true&#41;; &#160; &#160; &#160;configuration.common&#40;&#41;.objectClass&#40;HistoryItem.class&#41;.cascadeOnDelete&#40;true&#41;; &#160; &#160; &#160;return configuration; &#125; private String db4oDBFullPath&#40;Context ctx&#41; &#123; &#160; &#160; &#160;return ctx.getDir&#40;&#34;data&#34;, DATABASE_MODE&#41; + &#34;/&#34; + DATABASE_NAME; &#125; public void close&#40;&#41; &#123; &#160; &#160; &#160;if &#40;this.database != null&#41; &#123; &#160; &#160; &#160; &#160; &#160; this.database.close&#40;&#41;; &#160; &#160; &#160;&#125; &#125; Create a Data layer class. public Db&#40;Context c&#41; &#123; &#160; &#160; &#160;if&#40;_dbHelper==null&#41; &#123; &#160; &#160; &#160; &#160; &#160; _dbHelper = new DbHelper&#40;c&#41;; &#160; &#160; &#160;&#125; &#125; public void addSearchResult&#40;String text&#41; &#123; &#160; &#160; &#160;ObjectContainer db = _dbHelper.getDatabase&#40;&#41;; &#160; &#160; &#160;try &#123; &#160; &#160; &#160; &#160; &#160; db.store&#40;new HistoryItem&#40;text&#41;&#41;; &#160; &#160; &#160; &#160; &#160; db.commit&#40;&#41;; &#160; &#160; &#160;&#125; finally &#123; &#160; &#160; &#160; &#160; &#160; if &#40;db != null&#41; &#123; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;db.close&#40;&#41;; &#160; &#160; &#160; &#160; &#160; &#125; &#160; &#160; &#160;&#125; &#125; public void clearHistory&#40;&#41; &#123; &#160; &#160; &#160;ObjectContainer db = _dbHelper.getDatabase&#40;&#41;; &#160; &#160; &#160;try &#123; &#160; &#160; &#160; &#160; &#160; ObjectSet&#60;HistoryItem&#62; items = db.queryByExample&#40;new HistoryItem&#40;&#41;&#41;; &#160; &#160; &#160; &#160; &#160; for &#40;HistoryItem historyItem : items&#41; &#123; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;db.delete&#40;historyItem&#41;; &#160; &#160; &#160; &#160; &#160; &#125; &#160; &#160; &#160; &#160; &#160; db.commit&#40;&#41;; &#160; &#160; &#160;&#125; catch &#40;Exception e&#41; &#123; &#160; &#160; &#160; &#160; &#160; Log.e&#40;Db.class.getName&#40;&#41;, e.toString&#40;&#41;&#41;; &#160; &#160; &#160;&#125; finally &#123; &#160; &#160; &#160; &#160; &#160; if &#40;db != null&#41; &#123; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;db.close&#40;&#41;; &#160; &#160; &#160; &#160; &#160; &#125; &#160; &#160; &#160;&#125; &#125; public List&#60;String&#62; getHistory&#40;&#41; &#123; &#160; &#160; &#160;ObjectContainer db = _dbHelper.getDatabase&#40;&#41;; &#160; &#160; &#160;try &#123; &#160; &#160; &#160; &#160; &#160; List&#60;String&#62; results = new ArrayList&#60;String&#62;&#40;&#41;; &#160; &#160; &#160; [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a href="http://wiebe-elsinga.com/blog/wp-content/uploads/2010/05/android.jpg" rel="lightbox[415]" title="android"><img loading="lazy" class="alignleft size-thumbnail wp-image-170" style="padding-right: 10px;" title="android" src="http://wiebe-elsinga.com/blog/wp-content/uploads/2010/05/android-150x150.jpg" alt="" width="150" height="150" srcset="http://wiebe-elsinga.com/blog/wp-content/uploads/2010/05/android-150x150.jpg 150w, http://wiebe-elsinga.com/blog/wp-content/uploads/2010/05/android-300x300.jpg 300w, http://wiebe-elsinga.com/blog/wp-content/uploads/2010/05/android-200x200-cropped.jpg 200w, http://wiebe-elsinga.com/blog/wp-content/uploads/2010/05/android.jpg 500w" sizes="(max-width: 150px) 100vw, 150px" /></a></p>
<p>Applications usually uses databases to store application data. Inside this article I will be explaining two implementation types:</p>
<ul>
<li>SQLite</li>
<li>DB4o</li>
</ul>
<p>I will use the Android application which was developed inside my <a href="http://wiebe-elsinga.com/blog/?p=275">previous post</a>.<br />
<span id="more-415"></span></p>
<h2>SQLite</h2>
<p>SQLite is a lightweight relational database engine. SQLite is fast and has a small footprint, making it perfect for Android devices. Instead of the heavyweight server-based databases like Oracle and Microsoft SQL Server, each SQLite database is stored within a single file on disk. So lets set-up an implementation.</p>
<ol>
<li>Create a DB Helper Class</li>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">public</span> DbHelper<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+context"><span style="color: #003399;">Context</span></a> context<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span>context, DATABASE_NAME, <span style="color: #000066; font-weight: bold;">null</span>, DATABASE_VERSION<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
@Override<br />
<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>SQLiteDatabase sqliteDb<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> sql <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;CREATE TABLE &quot;</span> <span style="color: #339933;">+</span> TABLE_NAME <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; (&quot;</span> <span style="color: #339933;">+</span> COLOMN_NAME<span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; LONGTEXT&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;);&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;sqliteDb.<span style="color: #006633;">execSQL</span><span style="color: #009900;">&#40;</span>sql<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
@Override<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onUpgrade<span style="color: #009900;">&#40;</span>SQLiteDatabase sqliteDb, <span style="color: #000066; font-weight: bold;">int</span> oldVersion,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> newVersion<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;Log.<span style="color: #006633;">i</span><span style="color: #009900;">&#40;</span>DbHelper.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;Upgrading database from version &quot;</span> <span style="color: #339933;">+</span> oldVersion <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; to &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #339933;">+</span> newVersion <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;, which will destroy all old data&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;sqliteDb.<span style="color: #006633;">execSQL</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;DROP TABLE IF EXISTS &quot;</span> <span style="color: #339933;">+</span> TABLE_NAME<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;onCreate<span style="color: #009900;">&#40;</span>sqliteDb<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<li> Create a Data layer class. This class will be responsible for using our <i>DbHelper</i> to perform our desired CRUD functions.</li>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> addSearchResult<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> text<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;SQLiteDatabase db <span style="color: #339933;">=</span> _dbHelper.<span style="color: #006633;">getWritableDatabase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ContentValues values <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ContentValues<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; values.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>_dbHelper.<span style="color: #006633;">COLOMN_NAME</span>, text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db.<span style="color: #006633;">insert</span><span style="color: #009900;">&#40;</span>_dbHelper.<span style="color: #006633;">TABLE_NAME</span>, <span style="color: #0000ff;">&quot;&quot;</span>, values<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>db <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;db.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> clearHistory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;SQLiteDatabase db <span style="color: #339933;">=</span> _dbHelper.<span style="color: #006633;">getWritableDatabase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db.<span style="color: #006633;">delete</span><span style="color: #009900;">&#40;</span>_dbHelper.<span style="color: #006633;">TABLE_NAME</span>, <span style="color: #0000ff;">&quot;&quot;</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>db <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;db.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> getHistory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;SQLiteDatabase db <span style="color: #339933;">=</span> _dbHelper.<span style="color: #006633;">getWritableDatabase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> results <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+cursor"><span style="color: #003399;">Cursor</span></a> c <span style="color: #339933;">=</span> db.<span style="color: #006633;">rawQuery</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;select * from &quot;</span> <span style="color: #339933;">+</span> _dbHelper.<span style="color: #006633;">TABLE_NAME</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>c.<span style="color: #006633;">getCount</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;c.<span style="color: #006633;">moveToFirst</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">do</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; results.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>c.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span>c.<span style="color: #006633;">getColumnIndex</span><span style="color: #009900;">&#40;</span>_dbHelper.<span style="color: #006633;">COLOMN_NAME</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>c.<span style="color: #006633;">moveToNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">return</span> results<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>db <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;db.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<li>Add CRUD functions to your Android application</li>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">Db dB <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Db<span style="color: #009900;">&#40;</span>getBaseContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
dB.<span style="color: #006633;">addSearchResult</span><span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> clearHistory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;Db dB <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Db<span style="color: #009900;">&#40;</span>getBaseContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;dB.<span style="color: #006633;">clearHistory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> displayHistory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;Db dB <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Db<span style="color: #009900;">&#40;</span>getBaseContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> history <span style="color: #339933;">=</span> dB.<span style="color: #006633;">getHistory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;ArrayAdapter<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> mHistory <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayAdapter<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">row</span>, R.<span style="color: #006633;">id</span>.<span style="color: #006633;">ENTRY_CELL</span>, history<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;historyList.<span style="color: #006633;">setAdapter</span><span style="color: #009900;">&#40;</span>mHistory<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
</ol>
<p>Download source code <a href='http://wiebe-elsinga.com/blog/wp-content/uploads/2010/10/AndroidSQLLite.zip'>AndroidSQLLite.zip</a> [20kB]</p>
<h2>DB4o</h2>
<p>db4o is an object database, so no mapping of tables in a relational model. You can persist complex objects with nested collections or other complex objects, any level of complexity in your hierarchy. You&#8217;re not limited to flat objects with primitive types to get a reasonable performance. So lets set-up an implementation.</p>
<ol>
<li>Create a persistent Object</li>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HistoryItem <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> item<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">public</span> HistoryItem<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">public</span> HistoryItem<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> item<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">item</span><span style="color: #339933;">=</span> item<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setItem<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> item<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">item</span> <span style="color: #339933;">=</span> item<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> getItem<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> item<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<li>Create a DB Helper Class</li>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">public</span> DbHelper<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+context"><span style="color: #003399;">Context</span></a> context<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>database <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> database.<span style="color: #006633;">ext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">isClosed</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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;database <span style="color: #339933;">=</span> Db4oEmbedded.<span style="color: #006633;">openFile</span><span style="color: #009900;">&#40;</span>configure<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, db4oDBFullPath<span style="color: #009900;">&#40;</span>context<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+exception"><span style="color: #003399;">Exception</span></a> ie<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">e</span><span style="color: #009900;">&#40;</span>DbHelper.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, ie.<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><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> EmbeddedConfiguration configure<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;EmbeddedConfiguration configuration <span style="color: #339933;">=</span> Db4oEmbedded.<span style="color: #006633;">newConfiguration</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;configuration.<span style="color: #006633;">common</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">objectClass</span><span style="color: #009900;">&#40;</span>HistoryItem.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">objectField</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">&quot;item&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">indexed</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><br />
&nbsp; &nbsp; &nbsp;configuration.<span style="color: #006633;">common</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">objectClass</span><span style="color: #009900;">&#40;</span>HistoryItem.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">cascadeOnUpdate</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><br />
&nbsp; &nbsp; &nbsp;configuration.<span style="color: #006633;">common</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">objectClass</span><span style="color: #009900;">&#40;</span>HistoryItem.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">cascadeOnDelete</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><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">return</span> configuration<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> db4oDBFullPath<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+context"><span style="color: #003399;">Context</span></a> ctx<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">return</span> ctx.<span style="color: #006633;">getDir</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;data&quot;</span>, DATABASE_MODE<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;/&quot;</span> <span style="color: #339933;">+</span> DATABASE_NAME<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> close<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">database</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">database</span>.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<li> Create a Data layer class.</li>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">public</span> Db<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+context"><span style="color: #003399;">Context</span></a> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>_dbHelper<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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _dbHelper <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DbHelper<span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> addSearchResult<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> text<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;ObjectContainer db <span style="color: #339933;">=</span> _dbHelper.<span style="color: #006633;">getDatabase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db.<span style="color: #006633;">store</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> HistoryItem<span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db.<span style="color: #006633;">commit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>db <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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;db.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> clearHistory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;ObjectContainer db <span style="color: #339933;">=</span> _dbHelper.<span style="color: #006633;">getDatabase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ObjectSet<span style="color: #339933;">&lt;</span>HistoryItem<span style="color: #339933;">&gt;</span> items <span style="color: #339933;">=</span> db.<span style="color: #006633;">queryByExample</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> HistoryItem<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>HistoryItem historyItem <span style="color: #339933;">:</span> items<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;db.<span style="color: #006633;">delete</span><span style="color: #009900;">&#40;</span>historyItem<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db.<span style="color: #006633;">commit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+exception"><span style="color: #003399;">Exception</span></a> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">e</span><span style="color: #009900;">&#40;</span>Db.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, e.<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><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>db <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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;db.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> getHistory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;ObjectContainer db <span style="color: #339933;">=</span> _dbHelper.<span style="color: #006633;">getDatabase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> results <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ObjectSet<span style="color: #339933;">&lt;</span>HistoryItem<span style="color: #339933;">&gt;</span> items <span style="color: #339933;">=</span> db.<span style="color: #006633;">queryByExample</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> HistoryItem<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>HistoryItem historyItem <span style="color: #339933;">:</span> items<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;results.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>historyItem.<span style="color: #006633;">getItem</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> results<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>db <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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;db.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<li>Add CRUD functions to your Android application</li>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">Db dB <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Db<span style="color: #009900;">&#40;</span>getBaseContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
dB.<span style="color: #006633;">addSearchResult</span><span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> clearHistory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;Db dB <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Db<span style="color: #009900;">&#40;</span>getBaseContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;dB.<span style="color: #006633;">clearHistory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> displayHistory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;Db dB <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Db<span style="color: #009900;">&#40;</span>getBaseContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> history <span style="color: #339933;">=</span> dB.<span style="color: #006633;">getHistory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;ArrayAdapter<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> mHistory <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayAdapter<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">row</span>, R.<span style="color: #006633;">id</span>.<span style="color: #006633;">ENTRY_CELL</span>, history<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;historyList.<span style="color: #006633;">setAdapter</span><span style="color: #009900;">&#40;</span>mHistory<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
</ol>
<p>Download source code <a href='http://wiebe-elsinga.com/blog/wp-content/uploads/2010/10/AndroidDb4o.zip'>AndroidDb4o</a> [2131kB]</p>
]]></content:encoded>
					
					<wfw:commentRss>http://wiebe-elsinga.com/blog/android-and-databases/feed/</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
		<item>
		<title>How-To: Android Client – Server designs</title>
		<link>http://wiebe-elsinga.com/blog/android-client-%e2%80%93-server-designs/</link>
					<comments>http://wiebe-elsinga.com/blog/android-client-%e2%80%93-server-designs/#comments</comments>
		
		<dc:creator><![CDATA[W.Elsinga]]></dc:creator>
		<pubDate>Fri, 08 Oct 2010 06:15:18 +0000</pubDate>
				<category><![CDATA[Business]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[Servlet]]></category>
		<category><![CDATA[SOAP]]></category>
		<guid isPermaLink="false">http://wiebe-elsinga.com/blog/?p=405</guid>

					<description><![CDATA[Applications are usually designed so that a Server provides a service to a Client. Choosing an implementation depends on the requirements. Inside this article I will be explaining three implementation types: SOAP Servlet JSON I will use the Android application which was developed inside my previous post. SOAP Simple Object Access Protocol is basically designed to provide a lightweight mechanism to exchange data in a XML format. Setting up a SOAP web service isn’t al that difficult. There is a disadvantage of using SOAP, and that’s overhead. Because there is no default SOAP support inside the Android library, I will be using kSOAP. So lets set-up an implementation. Create a server implementation &#160; &#160; &#160;public class Hello &#123; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; public String sayHello&#40;String name&#41; &#123; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;return &#34;Hello &#34; + name; &#160; &#160; &#160; &#160; &#160; &#125; &#160; &#160; &#160;&#125; Create an Android implementation Create a call to the server &#160; &#160; &#160;private void callService&#40;SoapSerializationEnvelope envelope&#41; &#123; &#160; &#160; &#160; &#160; &#160; HttpTransportSE androidHttpTransport = new HttpTransportSE&#40;URL&#41;; &#160; &#160; &#160; &#160; &#160; // Call the URL &#160; &#160; &#160; &#160; &#160; try &#123; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;androidHttpTransport.call&#40;SOAP_ACTION, envelope&#41;; &#160; &#160; &#160; &#160; &#160; &#125; catch &#40;IOException e&#41; &#123; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;display&#40;&#34;Error:&#34; + e.getClass&#40;&#41;.getName&#40;&#41; + &#34;: &#34; + e.getMessage&#40;&#41;&#41;; &#160; &#160; &#160; &#160; &#160; &#125; catch &#40;XmlPullParserException e&#41; &#123; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;display&#40;&#34;Error:&#34; + e.getClass&#40;&#41;.getName&#40;&#41; + &#34;: &#34; + e.getMessage&#40;&#41;&#41;; &#160; &#160; &#160; &#160; &#160; &#125; &#160; &#160; &#160;&#125; Handle the response &#160; &#160; &#160;private SoapPrimitive getResultFromService&#40;String text&#41; &#123; &#160; &#160; &#160; &#160; &#160; SoapObject request = new SoapObject&#40;NAMESPACE, METHOD_NAME&#41;; &#160; &#160; &#160; &#160; &#160; SoapSerializationEnvelope envelope = new SoapSerializationEnvelope&#40;SoapEnvelope.VER11&#41;; &#160; &#160; &#160; &#160; &#160; request.addProperty&#40;&#34;name&#34;, text&#41;; &#160; &#160; &#160; &#160; &#160; envelope.setOutputSoapObject&#40;request&#41;; &#160; &#160; &#160; &#160; &#160; callService&#40;envelope&#41;; &#160; &#160; &#160; &#160; &#160; SoapPrimitive result = null; &#160; &#160; &#160; &#160; &#160; try &#123; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;result = &#40;SoapPrimitive&#41; envelope.getResponse&#40;&#41;; &#160; &#160; &#160; &#160; &#160; &#125; catch &#40;SoapFault e&#41; &#123; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;display&#40;&#34;Error:&#34; + e.getClass&#40;&#41;.getName&#40;&#41; + &#34;: &#34; + e.getMessage&#40;&#41;&#41;; &#160; &#160; &#160; &#160; &#160; &#125; &#160; &#160; &#160; &#160; &#160; return result; &#160; &#160; &#160;&#125; Download source code BackEndWSDL [5kB] and FrondEndWSDL [114kB] Servlet The most simple form of exchanging data is by way of HTTP(s) with the use of Java Servlets. There’s no need to serialize to/from XML, so it can be a real time saver. So lets set-up an implementation. Create a server implementation &#160; &#160; &#160;protected void doPost&#40;HttpServletRequest request, HttpServletResponse response&#41; throws ServletException, IOException &#123; &#160; &#160; &#160; &#160; &#160; String text = &#34;&#34;; &#160; &#160; &#160; &#160; &#160; StringBuffer sb = new StringBuffer&#40;&#41;; &#160; &#160; &#160; &#160; &#160; try &#123; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;BufferedReader reader = new BufferedReader&#40;new InputStreamReader&#40;request.getInputStream&#40;&#41;&#41;&#41;; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;String line = reader.readLine&#40;&#41;; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;while &#40;line != null&#41; &#123; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; sb.append&#40;line&#41;; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; line = reader.readLine&#40;&#41;; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;&#125; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;if &#40;sb.length&#40;&#41; != 0&#41; &#123; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; text = sb.toString&#40;&#41;; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;&#125; &#160; &#160; &#160; &#160; &#160; &#125; catch &#40;Exception e&#41; &#123; &#160; &#160; &#160; &#160; &#160; &#125; &#160; &#160; &#160; &#160; &#160; response.setContentType&#40;&#34;text/plain&#34;&#41;; &#160; &#160; &#160; &#160; &#160; response.setHeader&#40;&#34;Cache-Control&#34;, &#34;no-cache&#34;&#41;; &#160; &#160; &#160; &#160; &#160; if &#40;StringUtils.isNotEmpty&#40;text&#41;&#41; &#123; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;response.getWriter&#40;&#41;.write&#40;HELLO_STRING + text&#41;; &#160; &#160; &#160; &#160; &#160; &#125; else &#123; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;response.getWriter&#40;&#41;.write&#40;HELLO_STRING&#41;; &#160; &#160; &#160; &#160; &#160; &#125; &#160; &#160; &#160;&#125; Create an Android implementation Create a call to the server &#160; &#160; &#160;private InputStream callService&#40;String text&#41; &#123; &#160; &#160; &#160; &#160; &#160; InputStream in = null; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; try &#123; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;URL url = new URL&#40;SERVLET_URL&#41;; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;URLConnection conn = url.openConnection&#40;&#41;; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;HttpURLConnection httpConn = &#40;HttpURLConnection&#41; conn; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;httpConn.setRequestMethod&#40;&#34;POST&#34;&#41;; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;httpConn.setDoInput&#40;true&#41;; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;httpConn.setDoOutput&#40;true&#41;; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;httpConn.connect&#40;&#41;; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;DataOutputStream dataStream = new DataOutputStream&#40;conn.getOutputStream&#40;&#41;&#41;; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;dataStream.writeBytes&#40;text&#41;; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;dataStream.flush&#40;&#41;; &#160; &#160; &#160; &#160; &#160; &#160; dataStream.close&#40;&#41;; &#160; &#160; &#160; &#160; &#160; &#160; int responseCode = httpConn.getResponseCode&#40;&#41;; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;if &#40;responseCode == HttpURLConnection.HTTP_OK&#41; &#123; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; in = httpConn.getInputStream&#40;&#41;; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;&#125; &#160; &#160; &#160; &#160; &#160; &#125; catch &#40;Exception ex&#41; &#123; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;display&#40;&#34;Error: Not not connect&#34;&#41;; &#160; &#160; &#160; &#160; &#160; &#125; &#160; &#160; &#160; &#160; &#160; return in; &#160; &#160; &#160;&#125; Handle the response &#160; &#160; &#160;private String getResultFromServlet&#40;String text&#41; &#123; &#160; &#160; &#160; &#160; &#160; String result = &#34;&#34;; &#160; &#160; &#160; &#160; &#160; InputStream in = callService&#40;text&#41;; &#160; &#160; &#160; &#160; &#160; if&#40;in!=null&#41; &#123; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;result= convertStreamToString&#40;in&#41;; &#160; &#160; &#160; &#160; &#160; &#125; else &#123; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;result = &#34;Error: Service not returning result&#34;; &#160; &#160; &#160; &#160; &#160; &#125; &#160; &#160; &#160; &#160; &#160; return result; &#160; &#160; &#160;&#125; &#160; &#160; &#160;private static String convertStreamToString&#40;InputStream is&#41; &#123; &#160; &#160; &#160; &#160; &#160; BufferedReader reader = new BufferedReader&#40;new InputStreamReader&#40;is&#41;&#41;; &#160; &#160; &#160; &#160; &#160; StringBuilder sb = new StringBuilder&#40;&#41;; &#160; &#160; [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a href="http://wiebe-elsinga.com/blog/wp-content/uploads/2010/05/android.jpg" rel="lightbox[405]" title="android"><img loading="lazy" class="alignleft size-thumbnail wp-image-170" style="padding-right: 10px;" title="android" src="http://wiebe-elsinga.com/blog/wp-content/uploads/2010/05/android-150x150.jpg" alt="" width="150" height="150" srcset="http://wiebe-elsinga.com/blog/wp-content/uploads/2010/05/android-150x150.jpg 150w, http://wiebe-elsinga.com/blog/wp-content/uploads/2010/05/android-300x300.jpg 300w, http://wiebe-elsinga.com/blog/wp-content/uploads/2010/05/android-200x200-cropped.jpg 200w, http://wiebe-elsinga.com/blog/wp-content/uploads/2010/05/android.jpg 500w" sizes="(max-width: 150px) 100vw, 150px" /></a></p>
<p>Applications are usually designed so that a Server provides a service to a Client. Choosing an implementation depends on the requirements.  Inside this article I will be explaining three implementation types:</p>
<ul>
<li>SOAP</li>
<li>Servlet</li>
<li>JSON</li>
</ul>
<p>I will use the Android application which was developed inside my <a href="http://wiebe-elsinga.com/blog/?p=275">previous post</a>.<br />
<span id="more-405"></span></p>
<h2>SOAP</h2>
<p><b>S</b>imple <b>O</b>bject <b>A</b>ccess <b>P</b>rotocol is basically designed to provide a lightweight mechanism to exchange data in a <i>XML</i> format. Setting up a SOAP web service isn’t al that difficult. There is a disadvantage of using SOAP, and that’s overhead. Because there is no default SOAP support inside the Android library, I will be using <a href="http://ksoap2.sourceforge.net/">kSOAP</a>.<br />
<br />
So lets set-up an implementation.</p>
<ol>
<li>Create a server implementation</li>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Hello <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> sayHello<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;Hello &quot;</span> <span style="color: #339933;">+</span> name<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span></div></div>
<li>Create an Android implementation</li>
<li>Create a call to the server</li>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> callService<span style="color: #009900;">&#40;</span>SoapSerializationEnvelope envelope<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpTransportSE androidHttpTransport <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HttpTransportSE<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+url"><span style="color: #003399;">URL</span></a><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Call the URL</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;androidHttpTransport.<span style="color: #006633;">call</span><span style="color: #009900;">&#40;</span>SOAP_ACTION, envelope<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+ioexception"><span style="color: #003399;">IOException</span></a> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;display<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error:&quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getName</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> 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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>XmlPullParserException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;display<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error:&quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getName</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> 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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span></div></div>
<li>Handle the response</li>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">private</span> SoapPrimitive getResultFromService<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> text<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SoapObject request <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SoapObject<span style="color: #009900;">&#40;</span>NAMESPACE, METHOD_NAME<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SoapSerializationEnvelope envelope <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SoapSerializationEnvelope<span style="color: #009900;">&#40;</span>SoapEnvelope.<span style="color: #006633;">VER11</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.<span style="color: #006633;">addProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span>, text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; envelope.<span style="color: #006633;">setOutputSoapObject</span><span style="color: #009900;">&#40;</span>request<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; callService<span style="color: #009900;">&#40;</span>envelope<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SoapPrimitive result <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>SoapPrimitive<span style="color: #009900;">&#41;</span> envelope.<span style="color: #006633;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>SoapFault e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;display<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error:&quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getName</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> 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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> result<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span></div></div>
</ol>
<p>Download source code <a href='http://wiebe-elsinga.com/blog/wp-content/uploads/2010/10/BackEndWSDL.zip'>BackEndWSDL</a> [5kB] and <a href='http://wiebe-elsinga.com/blog/wp-content/uploads/2010/10/FrondEndWSDL.zip'>FrondEndWSDL</a> [114kB] </p>
<h2>Servlet</h2>
<p>The most simple form of exchanging data is by way of HTTP(s) with the use of Java Servlets. There’s no need to serialize to/from <i>XML</i>, so it can be a real time saver.</p>
<p>So lets set-up an implementation.</p>
<ol>
<li>Create a server implementation</li>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doPost<span style="color: #009900;">&#40;</span>HttpServletRequest request, HttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+ioexception"><span style="color: #003399;">IOException</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> text <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+stringbuffer"><span style="color: #003399;">StringBuffer</span></a> sb <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+stringbuffer"><span style="color: #003399;">StringBuffer</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+bufferedreader"><span style="color: #003399;">BufferedReader</span></a> reader <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+bufferedreader"><span style="color: #003399;">BufferedReader</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+inputstreamreader"><span style="color: #003399;">InputStreamReader</span></a><span style="color: #009900;">&#40;</span>request.<span style="color: #006633;">getInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> line <span style="color: #339933;">=</span> reader.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>line <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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>line<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; line <span style="color: #339933;">=</span> reader.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>sb.<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;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text <span style="color: #339933;">=</span> sb.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+exception"><span style="color: #003399;">Exception</span></a> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;text/plain&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cache-Control&quot;</span>, <span style="color: #0000ff;">&quot;no-cache&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>StringUtils.<span style="color: #006633;">isNotEmpty</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>HELLO_STRING <span style="color: #339933;">+</span> text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>HELLO_STRING<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span></div></div>
<li>Create an Android implementation</li>
<li>Create a call to the server</li>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+inputstream"><span style="color: #003399;">InputStream</span></a> callService<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> text<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+inputstream"><span style="color: #003399;">InputStream</span></a> in <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+url"><span style="color: #003399;">URL</span></a> url <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+url"><span style="color: #003399;">URL</span></a><span style="color: #009900;">&#40;</span>SERVLET_URL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+urlconnection"><span style="color: #003399;">URLConnection</span></a> conn <span style="color: #339933;">=</span> url.<span style="color: #006633;">openConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+httpurlconnection"><span style="color: #003399;">HttpURLConnection</span></a> httpConn <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+httpurlconnection"><span style="color: #003399;">HttpURLConnection</span></a><span style="color: #009900;">&#41;</span> conn<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;httpConn.<span style="color: #006633;">setRequestMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;POST&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;httpConn.<span style="color: #006633;">setDoInput</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> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;httpConn.<span style="color: #006633;">setDoOutput</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> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;httpConn.<span style="color: #006633;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+dataoutputstream"><span style="color: #003399;">DataOutputStream</span></a> dataStream <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+dataoutputstream"><span style="color: #003399;">DataOutputStream</span></a><span style="color: #009900;">&#40;</span>conn.<span style="color: #006633;">getOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dataStream.<span style="color: #006633;">writeBytes</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dataStream.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataStream.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> responseCode <span style="color: #339933;">=</span> httpConn.<span style="color: #006633;">getResponseCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>responseCode <span style="color: #339933;">==</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+httpurlconnection"><span style="color: #003399;">HttpURLConnection</span></a>.<span style="color: #006633;">HTTP_OK</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; in <span style="color: #339933;">=</span> httpConn.<span style="color: #006633;">getInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+exception"><span style="color: #003399;">Exception</span></a> ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;display<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error: Not not connect&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> in<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span></div></div>
<li>Handle the response</li>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> getResultFromServlet<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> text<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> result <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+inputstream"><span style="color: #003399;">InputStream</span></a> in <span style="color: #339933;">=</span> callService<span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>in<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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result<span style="color: #339933;">=</span> convertStreamToString<span style="color: #009900;">&#40;</span>in<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Error: Service not returning result&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> result<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span></div></div>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> convertStreamToString<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+inputstream"><span style="color: #003399;">InputStream</span></a> is<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+bufferedreader"><span style="color: #003399;">BufferedReader</span></a> reader <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+bufferedreader"><span style="color: #003399;">BufferedReader</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+inputstreamreader"><span style="color: #003399;">InputStreamReader</span></a><span style="color: #009900;">&#40;</span>is<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StringBuilder sb <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> line <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>line <span style="color: #339933;">=</span> reader.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>line <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+ioexception"><span style="color: #003399;">IOException</span></a> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; is.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+ioexception"><span style="color: #003399;">IOException</span></a> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> sb.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span></div></div>
</ol>
<p>Download source code <a href='http://wiebe-elsinga.com/blog/wp-content/uploads/2010/10/BackEndServlet.zip'>BackEndServlet</a> [242kB] and <a href='http://wiebe-elsinga.com/blog/wp-content/uploads/2010/10/FrondEndServlet.zip'>FrondEndServlet</a> [16kB] </p>
<h2>JSON</h2>
<p>So SOAP has the advantage of using a model, and HTTP(s) has the advantage of being simple. There is an implementation that uses both. <b>J</b>ava<b>S</b>cript <b>O</b>bject <b>N</b>otation is a light-weight data-interchange format, think of it as XML on a diet</p>
<p>So lets set-up an implementation.</p>
<ol>
<li>Create a server implementation</li>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doPost<span style="color: #009900;">&#40;</span>HttpServletRequest request, HttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+ioexception"><span style="color: #003399;">IOException</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> text <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+stringbuffer"><span style="color: #003399;">StringBuffer</span></a> sb <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+stringbuffer"><span style="color: #003399;">StringBuffer</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+bufferedreader"><span style="color: #003399;">BufferedReader</span></a> reader <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+bufferedreader"><span style="color: #003399;">BufferedReader</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+inputstreamreader"><span style="color: #003399;">InputStreamReader</span></a><span style="color: #009900;">&#40;</span>request.<span style="color: #006633;">getInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> line <span style="color: #339933;">=</span> reader.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>line <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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>line<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; line <span style="color: #339933;">=</span> reader.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>sb.<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;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text <span style="color: #339933;">=</span> sb.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+exception"><span style="color: #003399;">Exception</span></a> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+system"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;text/plain&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cache-Control&quot;</span>, <span style="color: #0000ff;">&quot;no-cache&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JsonObject jsonObj<span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> JsonObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>StringUtils.<span style="color: #006633;">isNotEmpty</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;jsonObj.<span style="color: #006633;">addProperty</span><span style="color: #009900;">&#40;</span>JSON_KEY, HELLO_STRING <span style="color: #339933;">+</span> text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;jsonObj.<span style="color: #006633;">addProperty</span><span style="color: #009900;">&#40;</span>JSON_KEY, HELLO_STRING<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+printwriter"><span style="color: #003399;">PrintWriter</span></a> out <span style="color: #339933;">=</span> response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>jsonObj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span></div></div>
<li>Create an Android implementation</li>
<li>Create a call to the server</li>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+inputstream"><span style="color: #003399;">InputStream</span></a> callService<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> text<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+inputstream"><span style="color: #003399;">InputStream</span></a> in <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+url"><span style="color: #003399;">URL</span></a> url <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+url"><span style="color: #003399;">URL</span></a><span style="color: #009900;">&#40;</span>SERVLET_URL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+urlconnection"><span style="color: #003399;">URLConnection</span></a> conn <span style="color: #339933;">=</span> url.<span style="color: #006633;">openConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+httpurlconnection"><span style="color: #003399;">HttpURLConnection</span></a> httpConn <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+httpurlconnection"><span style="color: #003399;">HttpURLConnection</span></a><span style="color: #009900;">&#41;</span> conn<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;httpConn.<span style="color: #006633;">setRequestMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;POST&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;httpConn.<span style="color: #006633;">setDoInput</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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;httpConn.<span style="color: #006633;">setDoOutput</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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;httpConn.<span style="color: #006633;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+dataoutputstream"><span style="color: #003399;">DataOutputStream</span></a> dataStream <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+dataoutputstream"><span style="color: #003399;">DataOutputStream</span></a><span style="color: #009900;">&#40;</span>conn<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<span style="color: #006633;">getOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dataStream.<span style="color: #006633;">writeBytes</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dataStream.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dataStream.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">int</span> responseCode <span style="color: #339933;">=</span> httpConn.<span style="color: #006633;">getResponseCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>responseCode <span style="color: #339933;">==</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+httpurlconnection"><span style="color: #003399;">HttpURLConnection</span></a>.<span style="color: #006633;">HTTP_OK</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; in <span style="color: #339933;">=</span> httpConn.<span style="color: #006633;">getInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+exception"><span style="color: #003399;">Exception</span></a> ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;display<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error: Not not connect&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> in<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span></div></div>
<li>Handle the response</li>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> getResultFromServlet<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> text<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> result <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+inputstream"><span style="color: #003399;">InputStream</span></a> in <span style="color: #339933;">=</span> callService<span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>in <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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;JSONObject jsonResponse<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonResponse <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONObject<span style="color: #009900;">&#40;</span>convertStreamToString<span style="color: #009900;">&#40;</span>in<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result <span style="color: #339933;">=</span> jsonResponse.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;output&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Error: JSON Object couldn't be made&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Error: Service not returning result&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> result<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span></div></div>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> convertStreamToString<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+inputstream"><span style="color: #003399;">InputStream</span></a> is<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+bufferedreader"><span style="color: #003399;">BufferedReader</span></a> reader <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+bufferedreader"><span style="color: #003399;">BufferedReader</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+inputstreamreader"><span style="color: #003399;">InputStreamReader</span></a><span style="color: #009900;">&#40;</span>is<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StringBuilder sb <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> line <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>line <span style="color: #339933;">=</span> reader.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>line <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+ioexception"><span style="color: #003399;">IOException</span></a> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; is.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+ioexception"><span style="color: #003399;">IOException</span></a> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> sb.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span></div></div>
</ol>
<p>Download source code <a href='http://wiebe-elsinga.com/blog/wp-content/uploads/2010/10/BackEndJSON.zip'>BackEndJSON</a> [399kB] and <a href='http://wiebe-elsinga.com/blog/wp-content/uploads/2010/10/FrondEndJSON.zip'>FrondEndJSON</a> [15kB] </p>
]]></content:encoded>
					
					<wfw:commentRss>http://wiebe-elsinga.com/blog/android-client-%e2%80%93-server-designs/feed/</wfw:commentRss>
			<slash:comments>13</slash:comments>
		
		
			</item>
	</channel>
</rss>
