« »

New Tutorials – How to build a service-enabled Android app

Posted June 4th, 2008 by Nazmul

We believe in the power of ONE, at ScreamingToaster. We believe in innovation - ONE experience can change a person's life. We believe in partnerships - ONE endeavor can establish lasting trust & mutual success. We believe in education - ONE training course can empower a developer for life. We believe in giving back - ONE open-source project can inspire future entrepreneur. ONE person can become the leader that starts a revolution. ONE company can change the world and make it a better place - join us.

I’ve written 3 tutorials to show you how to create a service enabled Android application that performs all of it’s network I/O in a background thread (not the UI thread). There are three tutorials covering the following:

  1. How to build a simple UI without using XML, by writing Java code to layout the UI.
  2. How to use Apache HTTP Client to connect to services over HTTP or HTTPS and exchange serialized Java objects with services.
  3. How to use background threads to perform long running network IO operations, so that the main UI thread is not locked up.

Here are some screenshots of this app in action:

Here’s a screenshot of the Eclipse Android LogCat facility (for debugging log messages):

Here’s a screenshot of the servlet (running in Tomcat 6) that communicates with the Android app:

Enjoy!

 

We believe in the power of ONE, at ScreamingToaster. We believe in innovation - ONE experience can change a person's life. We believe in partnerships - ONE endeavor can establish lasting trust & mutual success. We believe in education - ONE training course can empower a developer for life. We believe in giving back - ONE open-source project can inspire future entrepreneur. ONE person can become the leader that starts a revolution. ONE company can change the world and make it a better place - join us.

21 Responses to “New Tutorials – How to build a service-enabled Android app”

  1. Dan Wahlin Says:

    Awesome tutorial especially for those of us just starting out with Android.

  2. Nazmul Says:

    Hi Dan

    I’m glad you liked the tutorial. I’ve been adding more in depth tutorials that cover various parts of the Android API here. There’s also a good book to help you get started.

    Thanks for reading, and keep coming back
    Nazmul.

  3. Guru Says:

    Hello Nazmul,

    I am using the android-sdk-windows-1.0_r1 SDK.

    I am very new to Android.When i was trying to run the application you have developed (How to build a service-enabled Android app),I ran into problems.I got this error,Please could you help me

    [2008-09-30 11:38:43 - NetworkActivity] (skipping index file ‘C:\Documents and Settings\Administrator\Desktop\android\NetworkActivity\res\drawable\Thumbs.db’)
    [2008-09-30 11:38:44 - NetworkActivity] (skipping index file ‘C:\Documents and Settings\Administrator\Desktop\android\NetworkActivity\res\drawable\Thumbs.db’)
    [2008-09-30 11:38:45 - NetworkActivity] warning: Ignoring InnerClasses attribute for an anonymous inner class that doesn’t come with an associated EnclosingMethod attribute. (This class was probably produced by a broken compiler.)
    [2008-09-30 11:38:45 - NetworkActivity] warning: Ignoring InnerClasses attribute for an anonymous inner class that doesn’t come with an associated EnclosingMethod attribute. (This class was probably produced by a broken compiler.)

    [2008-09-30 11:38:50 - NetworkActivity] warning: Ignoring InnerClasses attribute for an anonymous inner class that doesn’t come with an associated EnclosingMethod attribute. (This class was probably produced by a broken compiler.)
    [2008-09-30 11:38:50 - NetworkActivity]
    UNEXPECTED TOP-LEVEL EXCEPTION:
    java.lang.IllegalArgumentException: already added: Lorg/apache/commons/beanutils/BeanComparator;
    [2008-09-30 11:38:50 - NetworkActivity] at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:123)
    [2008-09-30 11:38:50 - NetworkActivity] at com.android.dx.dex.file.DexFile.add(DexFile.java:143)
    [2008-09-30 11:38:50 - NetworkActivity] at com.android.dx.command.dexer.Main.processClass(Main.java:299)
    [2008-09-30 11:38:50 - NetworkActivity] at com.android.dx.command.dexer.Main.processFileBytes(Main.java:276)
    [2008-09-30 11:38:50 - NetworkActivity] at com.android.dx.command.dexer.Main.access$100(Main.java:56)
    [2008-09-30 11:38:50 - NetworkActivity] at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:228)
    [2008-09-30 11:38:50 - NetworkActivity] at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:245)
    [2008-09-30 11:38:50 - NetworkActivity] at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:130)
    [2008-09-30 11:38:50 - NetworkActivity] at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:108)
    [2008-09-30 11:38:50 - NetworkActivity] at com.android.dx.command.dexer.Main.processOne(Main.java:245)
    [2008-09-30 11:38:50 - NetworkActivity] at com.android.dx.command.dexer.Main.processAllFiles(Main.java:183)
    [2008-09-30 11:38:50 - NetworkActivity] at com.android.dx.command.dexer.Main.run(Main.java:139)
    [2008-09-30 11:38:50 - NetworkActivity] at

    [2008-09-30 11:39:13 - NetworkActivity] Conversion to Dalvik format failed with error 1

  4. Nazmul Says:

    Hi Guru,

    I have no idea what could be causing these errors. Sorry I couldn’t help you with this one. Maybe try the google android group for compiler errors.

    Thanks for reading and keep coming back,
    Nazmul.

  5. Guru Says:

    Thats ok.However can you provide a me a fair idea of the configuration like as to which version you have ran this code and a brief set up of Tomcat.

    Thanks
    Guru

  6. Nazmul Says:

    The Android code runs on the m5 sdk. I used Tomcat 6.x, with default configuration. Just put the WAR file in the webapps folder and it will work.

  7. Aarthi Says:

    The tutorial about building the UI through java code rather than using xml was jus great.
    It really helped me a lot.
    Thanks a lot………..

  8. Nazmul Says:

    Hi Aarthi

    You’re most welcome. I’m glad that you found the tutorial useful. I couldn’t find any other tutorials/books/examples that use java for building the UI rather than XML. Personally, I find the XML syntax cumbersome to generate and read, and it really lends itself to being generated by a UI builder (like JFormDesigner for Android if you will). Until these UI building tools are available, I find it easier to build UI’s in Java, using my own code-ui-builders (like JGoodies form builders).

    Thanks for reading, and keep coming back! :)

    Take care
    Nazmul.

  9. Jonathan Quimbly Says:

    HttpUtils – searching the docs, there doesn’t seem to be such a class?

    Rereading your tutorial, it seems this is part of a separate library?

    Is there a http service tutorial that’s based only on current Android code?

    Otherwise, it looks nice, good work!

  10. Nazmul Says:

    Thanks. You can download the source code for the tutorial which has HttpUtils in it, from here. This information is in the tutorials as well.

  11. dev Says:

    Hi
    I am new to android and just started to work with it.I have worked in Java/J2me. As you know in j2me there is no XML concept to build GUI, this tutorial helps me a lot.

    If you have any tutorial/document on:
    a) date and time /calendar
    b) Screen navigation
    c) Bluetooth
    d) Connecting to a servlet

    Then please share with me.
    Thanks and regards
    Devadrita harh

  12. Nazmul Says:

    I do have a tutorial to connect to a servlet and serialize data to it and back… Just look at my first 3 android tutorials. As far as the other stuff, you should buy this book.

  13. dev Says:

    Hi Nazmul,
    Can you provide the link(URL) for your tutorial
    Regards
    Devadrita

  14. Nazmul Says:

    http://developerlife.com/tutorials/?cat=41

  15. Shankari Says:

    Hi Nazmul,
    Great tutorial. This background thread concept is interesting. And where is the war file for tomcat deployment?

    I have a question. How can i connect to an Apache webserver and Mysql database from android app? Need to connect to these external servers in order to login into the app and to retrieve the data in a listed view. Finally, need to insert data into the external database from a form which generated by clicking on an item of the listed view.
    Hope you could help me in this regard.

  16. Charles Says:

    I have been trying to follow through your tutorials but I cannot run them because somehow the following are missing. My newness to the Eclipse and Android may be at fault, but I have tried to import Android library and resolved the imports, etc. Any special way to resolve these? (From what I know Android is not accompanied by servlet package.)

    org.apache.commons.httpclient.methods.*;
    import javax.servlet.*;
    import javax.servlet.http.*;

    Thanks.

  17. Charles Says:

    Hello,

    I have been trying to follow through your tutorials but I cannot run them because somehow the following are missing. My newness to the Eclipse and Android may be at fault, but I have tried to import Android library and resolved the imports, etc. Any special way to resolve these? (From what I know Android is not accompanied by servlet package.)

    org.apache.commons.httpclient.methods.*;
    import javax.servlet.*;
    import javax.servlet.http.*;

    Thanks.

  18. Dave Says:

    Hey!

    @Nazmul: Big THX for your great tutorial!

    @Charles: The servlet package comes from tomcat. The commons-httpclient-3.1.jar with the httpclient should be in the project’s lib folder: include it in your path and you should be fine. httpclient is not included in android anymore, cause they moved to version 4 of the apache http client and the different versions are not compatible.

  19. Scott Says:

    Dave —

    What are the ramifications of placing a jar like the commons-httpclient-3.1.jar on the Android project classpath? Since this is not an integrated piece of Android, won’t it blow chunks when Eclipse tries to build the APK for the emulator?

    P.S. Or should there be two different projects? 1-Android, 1-Web

    Peace,
    Scott

  20. Wajdi Says:

    Short, Simple, and comprehensive!
    Thanx a lot

  21. Cristian Says:

    First of all, a big thanks for taking the time to put up this tutorial!

    I’m starting out with Android and was looking for a comprehensive explanation of how to retrieve data from a server. This certainly helped me a lot!

    One thing that dazzled me a bit was that you are not creating two activities, one for the log in screen and one for the display screen. My understanding (from the official Android site) was that these actually belong in two activities. Or maybe I’m getting this all wrong?

    Thanks again!

Leave a Reply