blog_soap_service

Consuming SOAP Web Service in Android


In this tutorial, you’ll learn how to consume web services using the popular kSOAP library in an Android application.

kSOAP is a reputable library loved by developers who have the need to parse WSDL (Web Service Definition Language) and SOAP (Simple Object Access Protocol) messages. The kSOAP library is an elegant, lightweight, and efficient SOAP client library. The library is open source, easy to use, and it can save you from the hassle of coding a custom parser.

The objective of this tutorial is to make you familiar with the kSOAP library. For demonstration purposes, we’ll be using a simple web service from W3Schools. The web service is a Fahrenheit to Celsius converter and vice versa. The web service accepts a value in degrees Fahrenheit and responds with the equivalent value in degrees Celsius and vice versa. We’ll go through the example step by step. At the end of this tutorial, we’ll display the result on the user’s device.

 

In this tutorial, we’ll be using version 3.6.1 of the kSOAP library. After downloading the kSOAP library, add it to your project’s libs folder and include it in build.gradle (app module) file.

Visit the website of the W3Schools web service that we’ll use in this tutorial. You’ll see that there are two operations, CelsiusToFahrenheit and FahrenheitToCelsius. The name of each operation is self-explanatory. If you select an operation on the W3Schools website, you’re shown an example of the request that the web service expects as well as the response of the web service.

soap_webservice_request_response

Take a look at the code snippet below, which is an example request that the web service expects.

For FahrenheitToCelsius:

For CelsiusToFahrenheit:

Pay close attention to the SOAPAction, we will use it a bit later in this tutorial.

soap_action_in_details

Creating an Envelope

The first thing we need to do is creating a SOAP envelope using theSoapSerializationEnvelope class (org.ksoap2.serialization.SoapSerializationEnvelope), which you need to import from the kSOAP library.

When creating an instance of the SoapSerializationEnvelope class, theSoapEnvelope version is set by passing in SoapEnvelope.VER12, which tells the kSOAP library that we’ll be using SOAP 1.2. We set the envelope’s dotNetproperty to true as the web service we’ll be consuming runs on Microsoft’s .NET framework.

We start by creating an instance of the SoapObject class, which requires two parameters, a namespace and a method name. You can add additional properties to the request using the addProperty method as shown below.

For call a soap method using ksoap we need these 4 things (URL ADRESS, SOAP ACTION ADRRESS, NAMESPACE, METHOD NAME). You can find those information using the WSDL.

(Soap Action = Namespace + Method name).

To send the request to the web service, we need to create an HTTP transport request. We’ll use the HttpTransportSE class (org.ksoap2.transport.HttpTransportSE) for this.

It is time to send the SOAP request to the web service. We do this over HTTP using the transport and envelope objects we created earlier. The HTTP transport object has a call method, which is used to add the SOAP action and envelope that we created earlier.

When the web service sends back a response, we’ll need to process it and handle any errors that may have been thrown. We can then display the data to the user.

Permissions

We need request for internet permission.

soap_fahrenheit_to_celsiussoap_celsius_to_fahrenheit

Create an AsyncTask to consuming Web service in background thread. In doInBackground(), we create and properties for our SoapObject, create HttpRequest, Envelop, sending request and processing the response… In onPostExcute(), call back to our activity:

Complete Source code available here.

Conclusion

I hope you will like the article and it will definitely help you to make your apps more productive. In case of any confusion or query, feel free to ask. And if you found this tutorial to be useful, Share with others.


About Durga Chiranjeevi

I’m a normal guy, engineer by education who is passionate about Programming and Internet. An android enthusiast, now moving for cross platforms and game development.