SOAP is a protocol specification for exchanging structured information in the implementation of Web Services in computer networks. This XML-based protocol consists of three parts:
- an envelope, which defines the message structure and how to process it
- a set of encoding rules for expressing instances of application-defined datatypes
- a convention for representing procedure calls and responses
However, Android does not provide any sort of SOAP library. We can either write our own or use some library like kSOAP 2. The ksoap2-android library provides a lightweight and efficient SOAP client library for the Android platform.
Now, Let us implement KSOAP2 library in Android project.
to sum up what we will do:
- Make a call to a SOAP web service.
- Read and parse the output
- Display the web server message in the Activity
Before calling actual web service we need to add following:
- Add Internet persmission to Android Manifest
<manifest xlmns:android...>…<uses-permission android:name=“android.permission.INTERNET” /><application ...</manifest>
2. Add ksoap2-android-assembly-2.5.0-jar-with-dependencies .jar or above to android project.
Now, Let’s get started to create a SOAP request:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
//Initialize soap request + add parameters SoapObject request = new SoapObject(NAMESPACE, methodName); //Use this to add parameters request.addProperty("username", SOAP_USER_NAME); request.addProperty("apiKey", SOAP_USER_NAME); //Declare the version of the SOAP request SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.setOutputSoapObject(request); System.setProperty("http.keepAlive", "false"); //Needed to make the internet call HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); //Passing Parameters in request PropertyInfo stringArrayPropertyInfo = new PropertyInfo(); stringArrayPropertyInfo.setName("attributes"); stringArrayPropertyInfo.setValue(param); stringArrayPropertyInfo.setType(param.getClass()); request.addProperty(stringArrayPropertyInfo); try { //this is the actual part that will call the webservice androidHttpTransport.call(AppCredentials.NAMESPACE, envelope); //saving data received from response response = envelope.getResponse().toString(); return response; } catch (Exception e) { return ""; } |
For ksoap2 library:
https://github.com/simpligility/ksoap2-android
Stay update for more!