Consuming SOAP Web service in Android Example

    In this tutorial, you'll learn how to consume web services using the popular kSOAP library in an Android application. kSOAP is a reputable library loved by developers who have the need to parse WSDL (Web Service Definition Language) and SOAP (Simple Object Access Protocol) messages.

The kSOAP library is alightweight, and efficient SOAP client library. The library is open source, easy to use, and it can save you from the hassle of coding a custom parser. It is one of the more trusted SOAP libraries currently available and it's frequently updated, which is a good sign for any open source project.

1. Get K-Soap2 library and import to project

- Launch Eclipse and start a new Android Project.
- Download lastest ver sion (jar file) at Google Code.
- Put this jar file in libs folder of your project.
Now, k-soap2 library is ready to use.
And in this tip, we will use W3School webservice example (Temperature converter) at:  http://www.w3schools.com/webservices/tempconvert.asmx

2. Defining activity layout

Make a simple xml file for our app:

3. Programmatically coding

   For call a soap method using ksoap we need these 4 things (URL ADRESS, SOAP ACTION ADRRESS, NAMESPACE, METHOD NAME). You can find those information using the WSDL.
(Soap Action = Namespace + Method name).
Define 4 features in ConstantString.java:
   Create an AsyncTask to consuming Web service in background thread. In doInBackground(), we create and properties for our SoapObject, create HttpRequest, Envelop, sending request and processing the response... In onPostExcute(), call back to our activity:
    Source code for WebServiceCall.java with static method  callWSThreadSoapPrimitive() used in doInbackground() of AsyncTask:
    Finally, in our activity, call back data from background thread and show to views.
    Remember adding Internet permission in AndroidManifest.xml:

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

Some screen shots (click for full size):
pic name pic name pic name

Share


Previous post
« Prev Post
Next post
Next Post »