Month: May 2012

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:  


Java web services and Android clients – I


This is the second blog post in my April vacation. My intention of this post is to share my third year Software Engineering project experience with others before forget. When I decided to do this project I was new to Android and web services. So I had to over come various problems and try various examples available on internet.

Through this post I’ll show the path worked for me along with some problems I saw and I faced. Therefore I wish this would help for those people who are new to this type of developments.

Web services

The above image shows the the basic structure of the Service Oriented Architecture (SOA). There are so many good resources about SOA for learning.

The thing I want to mention here is for this purpose we want at least 2 things, a web server and a client (service requester). Program in those 2 sides need not to be in same language, but need to support same method of communication. In here I’ll use SOAP (Simple Object Access Protocol) messages, but there are many other methods too.

Creating a web service

For this purpose you need to have following things.

  • Eclipse EE edition
  • Axis2 .war file
  • Tomcat server
  • Other libraries according to your need (eg: JDBC driver, JUnit… etc.)

Creating the web service can be done in this way: Click here

(You can use place Axis2 .war file inside ..\Apache Software Foundation\Tomcat 7.0\webapps  folder without downloading binaries and configure to that)

Actually, web service is nothing other than one .java file is set to communicate with client. Other files inside need not to have .wsdl files. You can also use libraries just as you do in desktop application.

Problems You may face
  1. Fail to build a Dynamic web project – some times you face this error due to a bug in eclipse. A solution would be to import a previously created Dynamic web project through File->Import, and continue the rest.
  2. Libraries filed to support – When you use an external library you need to set build path of project to that .jar file, add .jar file to project’s Project_Name\WebContent\WEB-INF\lib folder and refresh that folder in Eclipse package explorer.

Example web service

Following code is the interface that client connect and used to create the WSDL file. This example is just to show how client use each method in web service.

import java.sql.DriverManager;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
 * @author BUDDHIMA
public class EmergencyAid {

     * @param args the command line arguments
    public String getLocations(String inputLocation) {

	String output;

         // Code for processing data and assign value to "output" string.
	}catch(Exception e){
        return output;

Basically communication between web service and client is done using Strings or other primary data types in Android. But If you rally want to exchange custom objects or Lists you have to convert them in to String (will discuss an easy way for this later).

Since this post is getting longer I’m moving client part to next post.


1. Eclipse WTP tutorial:

2. Christon’s tutorial:

Alternative ways of creating Axis 2 web services:

3. WSO2 oxygen tank:

4. Youtube demo:

Not only Axis 2, but also there are other web service engines available (eg: CFX )