Android SOAP Client Example Using ksoap2

In this tutorial you will learn to make an android soap client using ksoap2 library.

Prerequisites

  • We need ksop2 library. You can download its jar easily by searching on google.
  • A soap web service running on server.

Also Read: Android Restful Web Service Client Example

In this example we will create android client that will consume soap web service developed in Java. If you don’t have soap web service and want to know how to develop then follow below link to read the tutorial.

Also Read: Java SOAP Web Services Tutorial

How it works?

URL: It is the url of WSDL file.

NAMESPACE: The targetNamespace in WSDL.

METHOD_NAME: It is the method name in web service. We can have several methods.

SOAP_ACTION: NAMESPACE + METHOD_NAME.

You can obtain above details by reading WSDL file of web service.

The soap web service that I have used in this example have only one method that takes a number as parameter and find its square.

How to call soap web service?

  • First create request object of SoapObject class.
  • Now add information to request object that you want to send to web service. This is done using PropertyInfo class.
  • Create soap envelope using SoapSerializationEnvelope class.
  • Assign request object to envelope using setOutputSoapObject().
  • Create object of HttpTransportSE() class. Using this object call the soap web service.
  • We can get response from web service by using getResponse() method of SoapSerializationEnvelope class. The response is stored into SoapPrimitive reference variable.

This is how we use ksop2 library to call soap web service in android.

Android SOAP Client Example

Create an android studio project with package name com.androidsoapclient.

Import the ksoap2 library in your project. If you don’t know how to import a library then follow below link.

Read: How to Add Jar as Library in Android Studio

Add internet access permission in AndroidManifest.xml file by adding following line.

Add following code in respective files.

activity_main.xml

MainActivity.java

AsyncTask class is used to perform some operation in background. As we are doing network related operation so it is good to perform it in background.

Screenshot

Android SOAP Client Example Using ksoap2

Comment below if you have any queries or found anything incorrect in above android soap client example.

Leave a Reply

Your email address will not be published. Required fields are marked *