This is a mirror of official site: http://jasper-net.blogspot.com/

Invoke Web Services from Android

| Sunday, February 20, 2011
This is ongoing blog on Getting Started with Android. In earlier blog, I provided an architecture overview of android application, followed by setting up the development environment for Android and creating and running a simple application.

In this blog, I will describe how to invoke web services (soap based services) via Android. In my next blog, I will follow it up with how to invoke REST based services. For trying out the tutorial, you need to have the android development environment setup as mentioned in my previous blog.

There are two ways in which invoke web services

Raw APIs  : Use the HttpClient and XML parser to manually create a soap request and parse the soap response.
Using a soap client library : like KSOAP library which does the low level work for parsing and dealing with soap messages – For Android, there is library available at http://code.google.com/p/ksoap2-android/ . Its good to see some active development for KSOAP 2, I remembered I wrote the first article on KSOAP 2 way back in 2003 ( http://naveenbalani.com/index.php/2010/05/deliver-web-services-to-mobiles/)and good to see it back in development for android.
I would start development with the later approach, but I plan to use RAW APIs in the follow up post -

Download the KSOAP2 library , go to http://code.google.com/p/ksoap2-android/ , click on downloads link on the menu, and download the latest release artifact – http://code.google.com/p/ksoap2-android/source/browse/m2-repo/com/google/code/ksoap2-android/ksoap2-android-assembly/2.5.2/ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar . In the release artifact page, click on “View raw file” and select “Save Link as” and download the jar file which has all the required dependencies.

Next we would create a sample android project which would invoke a .NET web service. I decided to host a simple .NET web service in my website , so it would easier for you all to try out the sample . The web service is available at http://naveenbalani.com/WassupAndroid.asmx

This is a simple .NET service, with one operation called todayMessage(), which display “Wassup Android from a .NET application “ as output.

To create an andrioid project.  Start eclipse.

Select File > New > Project.
Select Android > Android Project, Click Next.
Enter the following information for the project -
Project name –  AndroidClientService

Build Target – Android 2.3

Application name –  WasuppTodaysMessage

Package name – org.android.websevice.client.samples

Create Activity –  AndroidClientService

Min SDK Version –  9

Click Finish
This would create a Project called AndroidClientService in your workspace.

Next , add the ksoap2-andriod dependency to the project. Select the AndroidClientService, click properties , click on Java build path , click on Libraries , select Add External Jars and add the ksoap2 library (ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar) and click Ok.

Next, open up the WasuppServiceClientAndroid class and replace the onCreate method with the following onCreate() method as shown in listing below. Following shows the complete code listing.


Read more: Javalobby

Posted via email from Jasper-net

0 comments: