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.
<uses-permission android:name="android.permission.INTERNET"/>
Add following code in respective files.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" tools:context="com.androidsoapclient.MainActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textBox" android:layout_marginBottom="5dp"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Find Square" android:id="@+id/button" android:layout_below="@+id/textBox" android:layout_marginBottom="5dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text" android:layout_below="@id/button"/> </RelativeLayout>
MainActivity.java
package com.androidsoapclient; import android.app.ProgressDialog; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; 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 MainActivity extends AppCompatActivity { EditText textBox; Button button; TextView text; String URL = "http://192.168.1.6:14870/SOAPWebService/services/DemoService?WSDL"; String NAMESPACE = "http://com"; String SOAP_ACTION = "http://com/square"; String METHOD_NAME = "square"; String PARAMETER_NAME = "n"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textBox = (EditText)findViewById(R.id.textBox); button = (Button)findViewById(R.id.button); text = (TextView)findViewById(R.id.text); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new CallWebService().execute(textBox.getText().toString()); } }); } class CallWebService extends AsyncTask<String, Void, String> { @Override protected void onPostExecute(String s) { text.setText("Square = " + s); } @Override protected String doInBackground(String... params) { String result = ""; SoapObject soapObject = new SoapObject(NAMESPACE, METHOD_NAME); PropertyInfo propertyInfo = new PropertyInfo(); propertyInfo.setName(PARAMETER_NAME); propertyInfo.setValue(params[0]); propertyInfo.setType(String.class); soapObject.addProperty(propertyInfo); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.setOutputSoapObject(soapObject); HttpTransportSE httpTransportSE = new HttpTransportSE(URL); try { httpTransportSE.call(SOAP_ACTION, envelope); SoapPrimitive soapPrimitive = (SoapPrimitive)envelope.getResponse(); result = soapPrimitive.toString(); } catch (Exception e) { e.printStackTrace(); } return result; } } }
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
Comment below if you have any queries or found anything incorrect in above android soap client example.
its not showing the square value
The soap webservice should be already running on the server.
The Same for me , just Square=
but does not show the result
the IP address 192.168.1.1 is not accesible
Funny : )
My app is not passing any value to the soap webservice i am using .
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?
You have to import ksop2 library in your project. Download it from below link.
http://www.java2s.com/Code/Jar/k/Downloadksoap2android252jar.htm
If i want to retrieve all data without get data from Edittext?
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
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
What is the structure of your xml file?
Can you help, please?
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?