Mobile Zone is brought to you in partnership with:

I am a Software Architect at IBM India Software Labs and part of the BPM product development team.In my spare time, I like to research on upcoming technologies and like to share my knowledge and experiences through blogs,articles, books and various forums.I am also a Master Author with IBM developerWorks,have co-authored books on Spring framework, the first book on Apache CXF and multiple IBM Redbooks.You can reach me on my website Naveen is a DZone MVB and is not an employee of DZone and has posted 7 posts at DZone. You can read more from them at their website. View Full User Profile

Invoke Web Services from Android

  • submit to reddit

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.

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 , click on downloads link on the menu, and download the latest release artifact – . 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

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 –

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.

This project would invoke the web service and display – “ “  on the device when the application is executed. To build the project, select Project -> Clean

package android.websevice.client.samples;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.os.Bundle;
import android.widget.TextView;

public class AndroidClientService extends Activity {

private static final String SOAP_ACTION = "";

private static final String OPERATION_NAME = "todaysMessage";

private static final String WSDL_TARGET_NAMESPACE = "";

private static final String SOAP_ADDRESS = "";

public void onCreate(Bundle savedInstanceState) {

TextView textView = new TextView(this);


SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
envelope.dotNet = true;


HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);


{, envelope);

Object response = envelope.getResponse();



catch (Exception exception)






To run the AndroidClientService  Android application, click on it and select Run As > Android Application.

On the eclipse console, you would see the following similar message –

[AndroidClientService] Performing android.websevice.client.samples.AndroidClientService activity launch

[AndroidClientService] Automatic Target Mode: launching new emulator with compatible AVD ‘AVD’

[AndroidClientService] Launching a new emulator with Virtual Device ‘AVD’

[AndroidClientService] Waiting for HOME (‘android.process.acore’) to be launched…

You should see the Android AVD being launched. After the above message, it takes a while (2-3 minutes) for the first time to get the Android home page on the emulator.

After the device is started, you should see the following message on console..

[AndroidClientService] Uploading AndroidClientService.apk onto device ‘emulator-5554′

[AndroidClientService] Installing AndroidClientService.apk…

[AndroidClientService] Success!

[AndroidClientService] Starting activity android.websevice.client.samples.AndroidClientService on device emulator-5554

If the application doesn’t show up on the emulator, Click on Menu option on the emulator and you would see the WasuppTodayMessage android application and message being displayed.

Issues encountered during invoking the web services application from Android Emulator

  • Unknown host exception  –

If you get the following exception – “’, than you need to add required domain name server which emulator would use to resolve domain.

A list of network limitations on emulator is available at –

As per the documentation – “

“At startup, the emulator reads the list of DNS servers that your system is currently using. It then stores the IP addresses of up to four servers on this
list and sets up aliases to them on the emulated addresses,, and as needed.

On Linux and OS X, the emulator obtains the DNS server addresses by parsing the file /etc/resolv.conf. On Windows, the emulator obtains the addresses by calling the GetNetworkParams() API. Note that this usually means that the emulator ignores the content of your “hosts file”

Now, to add the domain name server, click on Run configurations and select AndroidClientService and add the following


in the additional emulator command line options as shown below. Click Run to run the configuration

  • Security

If you get a permission issue while accessing internet, you need to add the following line in <uses-permission android:name=”android.permission.INTERNET”></uses-permission> to allow application to access  internet

Here is the  complete listing of AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidClientService"
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />

<uses-sdk android:minSdkVersion="9" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>




Published at DZone with permission of Naveen Balani, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)



Prasad Seemakurthi replied on Thu, 2011/07/28 - 5:59am

hi , such a great tutorial , i was sitting behind the corporate firewall and want to access the internet via the emulator i did try using the method specified at and its not working .please let me know the solution for this .pls mail at

Pratik Pawar replied on Sat, 2012/04/28 - 7:56am

Nice Info. But can you make a post regarding the use of the java web services in android in both the cases soap based and restful one... I was recommended to use restful webservices due to their nature...

Kuku Caca replied on Sun, 2012/08/19 - 9:17pm



Could you please tell me why  I get the error:


 08-20 14:03:37.910: E/AndroidRuntime(467): FATAL EXCEPTION: main
08-20 14:03:37.910: E/AndroidRuntime(467): java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject
08-20 14:03:37.910: E/AndroidRuntime(467):     at
08-20 14:03:37.910: E/AndroidRuntime(467):     at
08-20 14:03:37.910: E/AndroidRuntime(467):     at
08-20 14:03:37.910: E/AndroidRuntime(467):     at
08-20 14:03:37.910: E/AndroidRuntime(467):     at$1500(
08-20 14:03:37.910: E/AndroidRuntime(467):     at$H.handleMessage(
08-20 14:03:37.910: E/AndroidRuntime(467):     at android.os.Handler.dispatchMessage(
08-20 14:03:37.910: E/AndroidRuntime(467):     at android.os.Looper.loop(
08-20 14:03:37.910: E/AndroidRuntime(467):     at
08-20 14:03:37.910: E/AndroidRuntime(467):     at java.lang.reflect.Method.invokeNative(Native Method)
08-20 14:03:37.910: E/AndroidRuntime(467):     at java.lang.reflect.Method.invoke(
08-20 14:03:37.910: E/AndroidRuntime(467):     at$
08-20 14:03:37.910: E/AndroidRuntime(467):     at
08-20 14:03:37.910: E/AndroidRuntime(467):     at dalvik.system.NativeStart.main(Native Method)
08-20 14:03:55.420: I/Process(467): Sending signal. PID: 467 SIG: 9

 Please email on

Thanks alot

Roshni Patel replied on Thu, 2012/10/11 - 6:41am

How to handle different Exceptions like Request timeout , no url found ,if  web adress is ok but wrong method name.

Can you please help me to handle above exceptions in androi?

My applications working fne but i want to handle above exception to prevent the forced stop of my application

I am  using ksoap2-android-assembly-2.6.5-jar-with-dependencies.jar lib

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.