How to call Java web service in android

It was bit difficult to find out a good tutorial on how to call Java web service from inside Android application so I just decided to write one.

In this post, we are going to discuss about calling a web service from inside an Android application that has been developed using JEE technology.

Before we start with developing application, create a simple web service and deploy it in Apache Tomcat or Glass Fish server (server installed in your local machine or hosted server).

Do follow below link to create and deploy web service using JEE technology in Apache or Glass Fish server:


Quick Links

Project Structure

Layout creation:

  • Create new android project [File >> New >> Android Project] with project name JavaWSActivity
  • Click next and select target android device version [I chose version 3.2]
  • Click next and enter package name – ‘’
  • Click finish

Points to note:

  1. Connect your mobile and your local machine (where Apache or Glass Fish server installed) to same WiFi internet connection so that application which we are developing can access WebService installed in the local server.
  2. Make sure you turned off Firewall in your local machine since FireWall will not allow the local machine to accept Http calls from other machines or servers


As this application requires Internet connectivity to hit Web Service hosted in remote server, it would be good if Internet connectivity check is done before trying to hit Web Service..

Here are the tutorials which talk about checking Wifi/Mobile Internet:

Android Check Wi-Fi Internet Connection
Android Check Mobile Internet Connection

KSOAP client library is being used in Android to make WebService calls. You can download KSOAP library from here and add it in lib folder of Android project

Code Listings

Layout XML:

Open main.xml present under /res/layout folder and replace the XML with the below one.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" >

        android:text="Call JEE Webservice"
        android:textSize="30dp" />

        android:hint="Enter you name"/>

        android:text="Invoke JEE WS" />

        android:text="" android:textSize="26dp"/>



Application layout will look like:

Change to graphical layout of Main.xml, the layout design should look like below:

Create a new class called ‘WebService’ which is going to have basic webservice configuration and web method invocations:


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

public class WebService {
	//Namespace of the Webservice - can be found in WSDL
	private static String NAMESPACE = "";
	//Webservice URL - WSDL File location	
	private static String URL = "";
	//SOAP Action URI again Namespace + Web method name
	private static String SOAP_ACTION = "";
	public static String invokeHelloWorldWS(String name, String webMethName) {
		String resTxt = null;
		// Create request
		SoapObject request = new SoapObject(NAMESPACE, webMethName);
		// Property which holds input parameters
		PropertyInfo sayHelloPI = new PropertyInfo();
		// Set Name
		// Set Value
		// Set dataType
		// Add the property to request object
		// Create envelope
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
		// Set output SOAP object
		// Create HTTP call object
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		try {
			// Invoke web service, envelope);
			// Get the response
			SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
			// Assign it to resTxt variable static variable
			resTxt = response.toString();

		} catch (Exception e) {
			//Print error
			//Assign error message to resTxt
			resTxt = "Error occured";
		//Return resTxt to calling object
		return resTxt;

Create following objects under DotNetWSActivity class:

	Button b;
	TextView tv;
	EditText et;
	ProgressBar pg;
	String editText;
	String displayText;

Add following code snippet in the place of onCreate method.

 public void onCreate(Bundle savedInstanceState) {
        //Name Text control
        et = (EditText) findViewById(;
        //Display Text control
        tv = (TextView) findViewById(;
        //Button to trigger web service invocation
        b = (Button) findViewById(;
        //Display progress bar until web service invocation completes
        pg = (ProgressBar) findViewById(;
        //Button Click Listener
        b.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                //Check if Name text control is not empty
                if (et.getText().length() != 0 && et.getText().toString() != "") {
                    //Get the text control value
                	editText = et.getText().toString();
                    //Create instance for AsyncCallWS
                    AsyncCallWS task = new AsyncCallWS();
                    //Call execute 
                //If text control is empty
                } else {
                    tv.setText("Please enter name");

Class AsyncCallWS:
Create it as inner class inside JavaWSActivity.class.
This class will perform the webservice call which will be executed as separate thread. When execute method is called in onCreate method, it calls the following methods in order:

  1. onPreExecute() – Make ProgressBar visible
  2. doInBackground() – Invoke invokeHelloWorldWS by passing text and webmethod name
  3. onProgressUpdate() -Does nothing
  4. onPostExecute() – Make ProgressBar invisible and set the webservice response text in TextView
private class AsyncCallWS extends AsyncTask<String, Void, Void> {
		protected Void doInBackground(String... params) {
                        //Invoke webservice
			displayText = WebService.invokeHelloWorldWS(editText,"hello");
			return null;

		protected void onPostExecute(Void result) {
			//Set response
                        //Make ProgressBar invisible

		protected void onPreExecute() {
                        //Make ProgressBar invisible

		protected void onProgressUpdate(Void... values) {


Click here to know more about AsyncTask,

Internet permission
Don’t forget to add internet permission in AndroidManifest.xml:

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



Let us test the application:
Run click on the project >> Run as >> Android application
You could see following screens:

Download Source Code

Entire project is zipped and is available for download. Unzip the downloaded project and to import the project into eclipse, launch eclipse >> File >> Import.. >> Choose downloaded project(How to import android project in eclipse). If you just want to run the application in your mobile and see the output but don’t want to hit your head with source code, download application(apk) file and install it in your mobile device.

Download Source Code Download Application(apk)

*apk in Android is the installation file simliar to exe in windows.


If you feel the tutorial is helpful, please share your feedback below.


Related Articles

Author: Udhay

Share This Post On
468 ad
Join now to get more access to our Android Tutorials

Join now to get more access to our Android Tutorials

Once you Join, you will receive Mail containing latest Android Tutorials once a month !!

I promise I wont spam you !!

You have Successfully Subscribed!

Pin It on Pinterest