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.

13 thoughts on “Android SOAP Client Example Using ksoap2”

  1. good evening, I’ve copied the code, but I can’t solve the dependencies error, when I’ve synchronized the app.gradle, I have the next error “Failed to resolve:com.google.code.ksoap2-android:3.0.0”, can help me?

  2. hi,
    thanks for this. in this tut you only need one parameter, what if I need to pass more than one params? Can you pls give me a working sample? thks.

    Andy

  3. Hi,
    I have this error “W/System.err: java.lang.ClassCastException: java.util.Vector cannot be cast to org.ksoap2.serialization.SoapPrimitive”

    Please help me

    Thks

  4. i have asmx web-service which returns json.i tried your code.but when i run my apllication.its giving me xmlpullparser exception .i think response is json but its handle xml. can you help me with it?

Leave a Comment

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