Java web services and Android clients – II


In my early post I showed you how to create a web service. In this post I’m going to discuss about how to create an Android client for that web service. But the intention of this post is to create the connection between server and Android client, so presenting returned string in GUI will not be discussed.

What You Need

What You should know

First you have to create an Android project in Eclipse and add KSOAP library. It can be done easily by right click on project name at Project Explorer -> Build path -> Configure Build Path, and then go to Library tab and add KSOAP library through “Add External Jars”. Then make sure you can see that in Project Explorer.

You should be able to create and other GUI stuffs in Android (there are lots of stuffs for your help in internet), and be familiar with the terminology use in Android development.

Next I’m going to discuss on how to create the SOAP client part of the web service.

For Android versions before 3.2

Most of the examples for android SOAP clients belongs to this category. So I’m pointing to some resources without discussing further more.

Java Dzone:

For Android versions after 3.2

If you are going to use the sample code given above in these types of versions, you will end up with an error. It would surely be “Network On Main Thread Exception”. This is because the later versions of Android is not allowing to do network connecting activities on the main thread, in order to keep the main flow safe (You can have more information  about this error from here).

So in following example I’ll show how to create a web client using AsyncTasks, so to avoid from that error. This client is for the web service I talk about in previous post.

Example Android Client

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.os.AsyncTask;
import java.lang.reflect.Type;

 * @author BUDDHIMA
public class AndroidClientIF {

// To store returned value until return from methods
 String locationString;
 String userDataString;

 // Following will change according to web service
 final String NAMESPACE = "http://networkIf"; // has to change
 final String URL = ""; // has to change

public String receivedLocation(String input) {
 new wsTask1().execute(input); // wsTask1 is assigned for the functionality

 try {
 Thread.sleep(2000); // small delay until data receive - actually not a good practice, have to find alternative like Observer pattern
 } catch (Exception e) {

 return locationString;

private class wsTask1 extends AsyncTask<String, Void, Void> {

// For the web service method: public String getLocations(String inputLocation))
 protected Void doInBackground(String... entry) {

 String METHOD = "getLocations";
 SoapObject request = new SoapObject(NAMESPACE, METHOD);
 request.addProperty("inputLocation", entry[0]);
 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

envelope.dotNet = true;
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

try {, envelope);

 Object response = (Object) envelope.getResponse();

 locationString = response.toString();

} catch (Exception e) {
 // TODO: handle exception
 return null;

I have to mention specially about the NAMESPACE and URL. They should be find out from the WSDL file of the web service.

For “localhost:8080” I used “” because it’s the IP address which use to call the local machine from Android emulator. Otherwise it should be the machine IP where your web service is hosted. Namespace also can be found as shown above.

Other than that, Method name and Properties are according to web service discussed in previous post.


My idea in these two post is to share experience I got in 3rd year project. So I hope that these post will help to new comers to cope with errors. This is not the only way of consuming a web service. You could try with JSON too.


1. Android developer site:

2. KSOAP with AsyncTasks example:

3. AsyncTasks:

4. My previous post on creating Web services:

5. KSOAP Library:  


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s