Contributor: Abidur Rahman

Though SOAP based web-services are rare in these days, but there are lots of old companies whose web-services are still SOAP-based. If anyone wants to consume those services from Android, it is very important to know the procedure of consuming SOAP-based webservice. There are lots of tutorials on consuming RESTful web-services with better explanation, but there are a few tutorials on consuming SOAP web-services with a clear explanation.

In this article, we will learn how to consume SOAP web services from Android. Before starting code, we have to collect some information:

  • SOAP_ACTION
  • METHOD_NAME
  • NAMESPACE
  • URL
  • Parameter List
  • Request XML/ SOAP_BODY/ SOAP_MESSAGE (for Nested Parameters and Multiple Namespaces)

Now, the question is how can we get these information? if your client or service provider give you all the information, you won’t need anything. But if they don’t, then follow the process given below:

Suppose, you have to consume the data from this service: http://www.w3schools.com/webservices/tempconvert.asmx

This is the URL!!

Click on it, you can see a link named “Service Description” and a list of Method Name(s). So, we can get the METHOD_NAMEs from here.
Now, click on the “Service Description”.

abid's blog pic 1

You can see the NAME_SPACE there. For nested parameters, there can be multiple namespaces. So, a question may arise. “How can I find other namespaces?”. Okay!! I will discuss it later.

abid's blog pic 2

You can see the SOAP_ACTIONs here depending on the METHOD_NAME. So, be careful to choose the SOAP_ACTION!! You can also get the Parameter List here depending on the METHOD_NAME.

Okay we have got all (not all for the complex web services!!!) the information to consume soap web services from Android. Let’s code !!!

Sample Project

Start a new Android project and configure it as you please. Feel free to use your favorite IDE, but for this tutorial I’ll be using Android Studio.

In the project’s manifest file, you need to specify that the application is allowed to connect to the internet. So, add:


We will use a library named “kSOAP” which is an elegant, lightweight, and efficient SOAP client library. The library is open source, easy to use, and it will save you from the hassle of coding a custom parser.

To sync kSOAP library with your project, add these lines (bolded text) to your app’s build.gradle file:

apply plugin: 'com.android.application'

android {
 compileSdkVersion 23
 buildToolsVersion "23.0.2"

defaultConfig {
 applicationId "com.nascenia.soap"
 minSdkVersion 15
 targetSdkVersion 23
 versionCode 1
 versionName "1.0"
 }
 buildTypes {
 release {
 minifyEnabled false
 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
 }
 repositories {
 maven { url 'http://ksoap2-android.googlecode.com/svn/m2-repo' }
 }
 }
}

dependencies {
 compile fileTree(dir: 'libs', include: ['*.jar'])
 compile 'com.android.support:appcompat-v7:23.1.1'
 compile 'com.google.code.ksoap2-android:ksoap2-android:3.3.0'
}

Now, set SOAP_ACTION, METHOD_NAME and NAMESPACE variables in your activity class:

 .
    .
    .

public class MainActivity extends AppCompatActivity {

   private static final String SOAP_ACTION = "http://www.w3schools.com/webservices/CelsiusToFahrenheit";
   private static final String METHOD_NAME = "CelsiusToFahrenheit";
   private static final String NAMESPACE = "http://www.w3schools.com/webservices/";

   @Override
   protected void onCreate(Bundle savedInstanceState) {
    .
    .
    .

Now, create an AsyncTask, and add these doInBackground()’s codes to your doInBackground() method.

private class myAsyncTask extends AsyncTask {


   @Override
   protected Void doInBackground(Void... params) {

     String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";

     //for linear parameter
     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
     request.addProperty("Celsius", "48"); // adding method property here serially

     SoapSerializationEnvelope envelope = new      SoapSerializationEnvelope(SoapEnvelope.VER11);
     envelope.implicitTypes = true;
     envelope.setOutputSoapObject(request);
     envelope.dotNet = true;
 
     HttpTransportSE httpTransport = new HttpTransportSE(URL);
     httpTransport.debug = true;

     try {
        httpTransport.call(SOAP_ACTION, envelope);
     } catch (HttpResponseException e) {
        // TODO Auto-generated catch block
        Log.e("HTTPLOG", e.getMessage());
        e.printStackTrace();
     } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.e("IOLOG", e.getMessage());
        e.printStackTrace();
     } catch (XmlPullParserException e) {
        // TODO Auto-generated catch block
        Log.e("XMLLOG", e.getMessage());
        e.printStackTrace();
     } //send request

     Object  result = null;
     try {
        result = (Object )envelope.getResponse();
        Log.i("RESPONSE",String.valueOf(result)); // see output in the console
     } catch (SoapFault e) {
        // TODO Auto-generated catch block
        Log.e("SOAPLOG", e.getMessage());
        e.printStackTrace();
     }
     return null;
  }
}

Now execute your AsyncTask from onCreate method to see the output in the console.

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   myAsyncTask myRequest = new myAsyncTask();
   myRequest.execute();
}

For Nested Parameters and Multiple Namespaces

For the complex web services, we need SOAP_BODY/ SOAP_MESSAGE/ REQUEST_XML.

There is an elegant and efficient SOAP browser online: http://wsdlbrowser.com/

Copy the URL of your web service, and add “?wsdl” at the end of the URL and then browse.
You should see the METHOD_NAME on the left side. Click on one of the METHOD_NAMEs which you want. You will see REQUEST_XML there. It is the SOAP_BODY or SOAP_MESSGAE.

Consuming SOAP web services from Android

  • NAMESPACE name – Value of “xmlns:ns1”. If there are multiple namespaces, it will be indicated by ns1, ns2 and so on.
  • METHOD_NAME – indicated by the 1st arrow.
  • Parameter Name – indicated by the 2nd arrow.

REQUEST_XML:

  
    
      0
      
        ?
        ?
      
    
  

CODE:

// for nested parameters and multiple namespaces.
SoapObject request = new SoapObject(NAMESPACE1, METHOD_NAME);
PropertyInfo p = new PropertyInfo();
p.setNamespace(NAMESPACE1);
p.setType(PropertyInfo.STRING_CLASS);
p.setName("sessionID");
p.setValue(“value”);
request.addProperty(p);

SoapObject selection = new SoapObject(NAMESPACE1,"selection");
PropertyInfo EndDate = new PropertyInfo();
EndDate.setNamespace(NAMESPACE2);
EndDate.setType(PropertyInfo.STRING_CLASS);
EndDate.setName("EndID");
EndDate.setValue("value");
selection.addProperty(EndDate);

PropertyInfo StartDate = new PropertyInfo();
StartDate.setNamespace(NAMESPACE2);
StartDate .setType(PropertyInfo.STRING_CLASS);
StartDate.setName("StartID");
StartDate .setValue("value");
selection.addProperty(StartDate);

request.addSoapObject(selection);

DONE!!!

I hope that helps. Please check some other blogs on web and mobile app development.

Published On: August 3rd, 2016 / Categories: Android Application, Blog / Tags: /