Thursday 12 June 2014

Consuming WCF SVC XML Web Service with Attributes in Android

In today’s ingeniously connected world, no Android app is complete without internet access. Being a mobile app consultant at Truiton, sometimes I feel, I keep doing stuff that no one else has done ever before, for e.g. take topic of this tutorial Consuming WCF SVC XML Web Service with Attributes in Android. I searched internet for hours, hoping to find some info about how to invoke a DotNet based WCF SOAP web service with attributes in XML, but found nothing. Eventually I figured out a way and thought to share it with the community by means of this Consuming WCF SVC XML Web Service with Attributes in Android tutorial.
While searching for ways on how to consume WCF SOAP web service, I found an open source project for Android named Ksoap2. This comes in a form of .jar file which can be included in any Android app project, this is a lightweight and efficient SOAP client library, at least this is what it’s page claims. I found various tutorials explaining on how to consume XML soap web services but none of them explains how to send attributes with input request data. In fact, I looked up the wiki for ksoap2, hoping to find a way to add attributes to request XML, but found nothing. While searching for an answer, I started to think why should I include a third party library for such a small task of consuming a WCF SVC XML web service with attributes in Android. Hence I dropped searching in that direction.
Finally I found another way through which I could invoke a WCF SVC XML web service with attributes in Android, i.e. via HttpClient, HttpPost, HttpResponse, and HttpEntity classes oforg.apache.http package. This way we can create a simple http request and send it to the desired url. Now since we are not using Ksoap2 library, there is still a question to be answered i.e. how will we parse the response? Answer to that is simple XML SAX parser, with document builder class. The main advantage of this approach is that we can send and parse attributes in request and response respectively. Lets have a look at my request for Soap xml web service:
Since we are not using any library to consume DotNet WCF soap web service with attributes in request, you need to create this request manually, which is quite simple to do. Have a look at the request above, in this request we are invoking Login method of SVC XML web service on Andorid. Here have a look at the highlighted part, i.e. the CREDENTIALS tag, it has three attributes. One of the main reasons we are using thisHttpPost class is because of the attributes.
Lets have a look at the class which is consuming WCF SVC XML web service with attributes in Android:
Since the release of Honeycomb we cannot use network on main thread, therefore I usedAsyncTask to call the WCF SVC XML web service with attributes on Android. This tutorial can also be treated as a tutorial for AsyncTask.

As you can see the, the code above has SOAP_ACTION and URL variable, specifying the url and soap action for soap xml web service. After all the the variable instantiations we are creating a HttpPost request, which has soap xml attributes. When the request is sent, response is captured in a string type variable response. Then we useDocumentBuilderFactory methods with SAX parser InputSource class to create a xml document which is used to parse xml response with help from org.w3c.dom.node class. The response XML is stored iteratively in a HashMap called Data.
Next we can pass on this data to onPostExecute method and call method in class from it. Just to show the concept of AsyncTask, I have created a method doWhateverYouWant().
Have a look at the login method web service response:
One more thing don’t forget to add the internet access permission in the manifest, as while consuming WCF SVC XML web service with attributes on Android we will be interacting with internet.