Android Development
SOAP Web Service
Mobile Programming
API Integration
Coding Tutorials

How to call a SOAP web service on Android

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Calling a SOAP (Simple Object Access Protocol) web service in an Android application involves a series of steps, from setting up the necessary permissions to parsing the SOAP response. SOAP is a protocol specification for exchanging structured information in the implementation of web services in computer networks. It relies heavily on XML, and due to its verbosity, it is generally considered more secure than REST.

Essential Setup and Permissions

Before you begin making SOAP calls, ensure that your Android project is properly set up to handle network operations and XML parsing. You will need to add the internet permission in your AndroidManifest.xml file:

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

Also, depending on your build version, you might need to handle network operations on a separate thread from the UI thread to avoid NetworkOnMainThreadException. This can be achieved using AsyncTask or other concurrency constructs such as Executors.

Using KSOAP2 Library

While there are various ways to consume SOAP web services, one of the most popular libraries that simplify this process is KSOAP2. It's specifically designed for the Android platform. First, add the KSOAP2 dependency to your build.gradle:

groovy
implementation 'com.google.code.ksoap2-android:ksoap2-android:3.6.4'

Constructing the SOAP Request

To create a SOAP request, you'll need to know the namespace, method name, and URL of the web service. Here’s a basic example of how to construct a SOAP request using KSOAP2:

java
1String namespace = "http://tempuri.org/";
2String methodName = "GetTemperature";
3String soapAction = namespace + methodName;
4String url = "http://www.webservicex.net/temperature.asmx";
5
6SoapObject request = new SoapObject(namespace, methodName);
7request.addProperty("CityName", "London");
8request.addProperty("CountryName", "UK");
9
10SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
11envelope.dotNet = true;
12envelope.setOutputSoapObject(request);

Performing the Network Request

To execute the network request and get the response, you can use the HttpTransportSE class provided by KSOAP2. This can be performed in an AsyncTask to ensure that it runs on a background thread:

java
1private class SoapCall extends AsyncTask<Void, Void, String> {
2    @Override
3    protected String doInBackground(Void... params) {
4        try {
5            HttpTransportSE transport = new HttpTransportSE(url);
6            transport.call(soapAction, envelope);
7            SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
8            return response.toString();
9        } catch (Exception e) {
10            e.printStackTrace();
11            return null;
12        }
13    }
14
15    @Override
16    protected void onPostExecute(String result) {
17        // Update UI or handle result
18    }
19}
20new SoapCall().execute();

Handling the SOAP Response

The result obtained from the SOAP service can be a simple value, complex object, or even a list depending on the SOAP method called. Parsing this result will depend on what the SoapPrimitive or SoapObject contains. Often with complex types, you may need to parse through multiple levels of data.

Error Handling

Always implement robust error handling when dealing with network operations. Handle timeouts, network exceptions, and malformed responses adequately. Ensure that the UI informs the user appropriately in all error scenarios.

Summary Table

AspectDescriptionTools/Classes Used
Network SetupRequired internet permission and background threading.AndroidManifest.xml, AsyncTask
LibrarySimplifies SOAP calls on Android.KSOAP2 (ksoap2-android)
SOAP EnvelopeCreating requests and handling responses.SoapObject, SoapSerializationEnvelope, HttpTransportSE
Parsing ResponseDepending on returned data. Can be simple or complex.SoapPrimitive, SoapObject
Error HandlingManaging timeouts and exceptions.try-catch, network checks

Conclusion

Calling a SOAP web service on Android can seem complex due to the verbosity and strict communication protocol. However, with the help of libraries like KSOAP2 and proper background threading, it becomes a structured and manageable task. Remember, always test the network and XML functionalities on various devices to catch and fix any platform-specific issues.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.