Though SOAP-based web services from Android are rare these days, many old companies still have SOAP-based web services. If anyone wants to consume those services from Android, it is essential to know the procedure for consuming SOAP-based web services. There are lots of tutorials on consuming RESTful web services with better explanations, but there are 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 the 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 this information? if your client or service provider gives 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!!
When you 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".
You can see the NAME_SPACE there. For nested parameters, multiple namespaces can exist. So, a question may arise: "How can I find other namespaces?" Okay!! I will discuss it later.
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 SOAP Android web 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 and easy to use, and it will save you from the hassle of coding a custom parser.
To sync the kSOAP library with your project, add these lines (bolded text) to your app's build.gradle file:
apply plugin: 'com.android.application' android dependencies
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
Now execute your AsyncTask from the onCreate method to see the output in the console.
@Override protected void onCreate(Bundle savedInstanceState)
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: https://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 that you want. You will see REQUEST_XML there. It is the SOAP_BODY or SOAP_MESSGAE.
- 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 our latest blogs about cross-platform mobile app development.
More to Explore
The Quest for Knowledge Continues. Fuel Your Curiosity.