Oct 25, 2010

Network Programming Tips for Mobile Developers (iPhone, Android, Java ME etc)

Among the most important things to master for a mobile application developer is network programming. Every mobile application I have developed has involved some kind of network communication. During the year I have learned a thing or two. Read on to get some tips & trick from me.

Quite often the server is developed alongside the client. This means that you as a client developer have to wait for those awfully slow server programmers. Not every server programmer is slow, but most likely you have to wait for some server functionality. In situations like these, it is handy to roll your own server. But as a client developer, you do not want to spend an massive amount of time to setup a server. In fact, if it takes longer than a couple of minutes most client developers give up. What you need is a server that is very quick and easy to setup. Also it needs to be simple, yet powerful. Many server programmers would recommend you setting up a Tomcat server. The advantage of using a Tomcat server is that it is very versatile. But I do not really like Tomcat. It is to advanced for me. Another solution is to use a Jetty server. This is simpler to setup than Tomcat, but yet rather powerful. It could be executed from Maven. As such is is convenient to use for automated tests. Maven takes cares most of the work, including starting and stopping your Jetty server. But there is a new and rising star, the Sinatra server. The Sinatra server is actually a Ruby library. You use Ruby to program the behavior of your server.   

A simple “Hello World” implementation for Sinatra looks like the one below (from the Sinatra Book).

require 'rubygems'
require 'sinatra'

get '/' do
  "Hello world, it's #{Time.now} at the server!"

The file is saved as a file with .rb extension, in our example we save it as “hello.rb”. Then you start your server as simple as this:

ruby hello.rb

You get a nice line saying that Sinatra has taken the stage and that your server uses port 4567. This could of course be changed, if you want to mimic another server without changing your code. It is very easy to extend your server functionality. Ruby is easy to learn and powerful for making your own server without any big hazzle. Take a look at the “Sinatra Book” if you want to master Sinatra.

Another common scenario is that you need to figure out what happens when you make a certain URL request, for example if you do a REST request. Before even writing a single of code, you could use the cURL command line tool. Its available on most platforms, like Unix, Linux, Mac OS, Windows etc. For a matter of discussion, let us assume that you want to check that you programmed your Sinatra web server correctly. Then you the following command:


The response should look like this:

Hello world, it's Mon Oct 25 20:44:19 +0200 2010 at the server!

So now you know how to implement your own simple server, as well as how to debug your server request using curl. But wait, there is even more tricks I want to share with you. I hope that you feel like reading a little bit more.

I think that XML is a rather misused technologies around. It is used for many things, ranging from describing your builds (Ant, Maven, etc) to describing serialized objects traveling through cyberspace (SOAP, REST etc). When SOAP was introduced, one main argument for XML is that it human readable. What? Have you ever seen a SOAP request that is human readable? If you are about to send and/or receive objects there are much more suitable technologies than XML. Especially when making a mobile client, where XML parsing could take to much time and memory, it is important to understand that there are good alternatives. One good old technology is ASN.1, that is hugely underestimated. It was designed for communicating data between different architectures and CPUs. It is fast even on a 8-bit CPU. The biggest drawback is that it is not widely supported and it requires an ASN.1 compiler. However you could implement your own ASN.1 encoder/decoder quite easy. Another solution that is easier to use, but building on the same principles as ASN.1 is the Hessian protocol. It is a binary web service protocol. The specification is originally designed by Caucho, who did the Resin web server. The specification is open and implemented in many languages, including .NET, Flash, Ruby, Python, Objective-C etc. I have primarily used it for Java ME, where only a subset is implemented. If you use it in Java ME, I would recommend considering using it to store object data in the record store. But now it is more relevant for me to use it on Android or iPhone. The Objective-C variant for iPhone is called HessianKit. It is open source and released under Apache 2.0 license. Thus it is not a viral open source license, which I think is great. I will not describe how to use it, since there already is a good article on the subject “HessianKit Released”. I hope that you will consider using Hessian if you are in the position to decide what web service protocol to use. If you feel the urge to use XML for your web services, you could use Burlap which is the XML version of Hessian. The communication is as simple as it could get using XML.

Another useful tool is a network analyzer. This is good for finding out what happens between the client and server. For example, if you want to take a look at the headers are many times auto generated. I have used Wireshark with great success. It would not say it is easy to use, but when you need to use it is priceless.

These are the tools that I think I use the most for network programming. What are your best tools when doing network programming?


softwarecollectors said...

Thanks for giving me a idea about Tips of Networking and i am sure that you are right this tips are i mainly used to my blog for free software

Business and IT said...

He boy i found a nice site about Business and IT programming which is http://www.Business-IT.Net please see this site


Anonymous said...

I think the "user readable" argument is often used to mean also, that it's very easy to understand the idea behind the XML and everybody uses it anyway to do almost anything so the developer is already familiar with it. It takes time to find alternatives and e.g. building your own ASN.1 encoder/decoder doesn't sound too tempting when you could just use XML. Processing power is rarely a problem on todays mobiles.

Have you any idea of Java ME:s future? Replaced by other technologies?

My Open Source Software Development Blog said...

I guess that by human readable is that it is easy to print out "human readable" XML code in a log or similar. If the amount is small, this is rather nice. But it tends to get big and hard to read.

No, you should not make your own ASN.1 encoder/decoder. I know that there some ASN.1 compilers that takes an ASN.1 interface description and generates Java code. In the project where we used it, we used C on the server side and Java in the client. Then we generated both C and Java code from the same definition. The auto generated code was easy to use. So you could generate C code to be used in an iPhone, since Objective-C is backward compatible with C.

But I do not say that ASN.1 is the best solution in all cases, but it is an underestimated technology.

Regarding processing power, one must not forget that even though you have the processing power to do XML parsing, this is not always suitable to do so. To much processing => to much battery power. For example an iPhone application that uses to much power might be rejected by Apple. In the case of Android, people might uninstall an application that drain your battery. The Android platform has an excellent utility that figures out which applications that uses the most power. Another problem might also be that to much parsing takes some time.

I am not sure what will happen to Java ME. This also depends on which Java ME configuration you are talking about. Java ME used for small embedded devices will definitely be there for some more time. But Java ME CLDC/MIDP must be upgraded to compete against Android. That is there must be a Java SE like variant for phones, pads and such. But only time will tell.

Anonymous said...

It has been a while since I last time programmed for mobiles so I didn't even realize the battery power is an issue on them. Good point, I agree!

I meant CLDC/MIDP, didn't remember that Java ME is more than just those.


TheRohan said...

Hey guys… Google Maps API in Java ME

Since your Google Maps API key is bound to a specific URL, in order to access map services you need to setup a proxy server that will receive HTTP requests from the mobile application and forward them to Google Maps REST URLs, returning Google responses to mobile clients.

If u need answer to all ur questions regarding usage of google maps then check this link: http://wiki.forum.nokia.com/index.php/How_to_use_Google_Maps_data_in_mobile_applications

vigor said...

Excellent Stuff !
I will apply the Tips for My Use
Hey their Is a Site Full of Networking and Computers Tips Check This

Web Application Development said...

I read this post and found really very useful for me. As I am also in the same industry and I like to reading such posts.

Thanks for posting...

Outsourced Product Development said...

It is used for many things, ranging from describing your builds to describing serialized objects traveling through cyberspace.

Inventory POS System said...

I appreciate your post, thanks for sharing the post, i would like to hear more about this in future

open source development said...

Really nice blog, very informative. Thanks dude for wonderful posting. Keep it up in the future as well.

Outsourced Product Development said...

Really very useful tips for Mobile Applications Development.

Anonymous said...


Anonymous said...

Talented designers realize the natural beauty and understated elegance on this color and then do well in expressing rich fashion taste by bride dresses various light and dark green bridesmaid gowns, without overdoing style. Take only a glimpse on today’ s market; apple green, sage green and prom dresses lime green gowns for bridesmaids do create eye-catching scenes.When this color is used on dresses for bridesmaids, artists have wedding dresses never walked far away from the initially low-pitched theme. On tea and knee-length green bridesmaid gowns, sash and hem in white are wedding gowns always added,If you are invited to go to a formal church wedding, an empire waist bridesmaid gown in green and floor length should be evening gowns

software development said...

Nice and informative post. I appreciate it. Thanks for sharing this informative post. Keep posting updates.

Web Development Company said...

Thanks for sharing nice information about Mobile application development. Mobile Application Development useful for iPhone development and Android Mobile Application Development. Awesome post.

Anonymous said...

bridesmaid dress
bridesmaid dresses