This tutorial outlines how to call Soap Web Services from Android Eclipse Projects in just few steps using wsclient.android.jar libraries provided by WSClient++.
First step is to generate code from the available wsdl location. WSDL is Web Service Description Language specified in XML, which allows us to interpret schema used in transfer of objects between local and remote endpoints. To generate code, open WSClient++ and create a new configuration file which will specify WSDL URL, Folder to store generated files and select type as Java and choose Android as the target. As shown in following picture.
Now if you look at Platform Libraries, there is a link called “Source and Library”, please click this link, it will open a folder that contains required wsclient.android.jar library that you should include in your eclipse project.
WSClient++ generates following sets of classes as below. So lets assume, following simple Inventory Web Service that allows us to Add/Delete/List products.
Synchronous Web Service Client
Synchronous Web Service Client class will be named as “NamedService”, which in our case will be “InventoryService”. Which will have synchronous methods which will make soap calls to destination server and will return the native representation of soap response in terms of custom types or native types as described by the WSDL. Synchronous client will wait for method call to finish, however it may return an error in case of timeout. But this may halt the user interface so we recommend using Asynchronous Web Service Client for most practical purposes.
Asynchronous Web Service Client
Asynchronous Web Service Client class will be named as “NamedServiceAsync” which in our case will be “InventoryServiceAsync”. It will have all methods same as that of Synchronous Web Service Client, but these methods will return nothing. Instead you will have to pass anonymous class named “MethodResult” that will implement onResult and onError methods.
Request/Response and Custom Types
For every custom xml type, WSClient++ will generate corresponding Java class and it will marshal every request in xml as expected by Soap. You can simply invoke and access these objects as your other Java classes, and WSClient++ library will do xml parsing and processing for you in background.
Invoking Web Service
After including generated files and adding wsclient.android.jar files in your project, you will be able to access web service in your project as shown below.
For Asynchronous calls, please define following methods which will implement responses of your web service and this class will be the delegate passed to web service.
Following sample illustrates same service called synchronously.
There are more advantages of WSClient++ outlined below,
- Every class implements IParcelable interface, which makes it easier to transfer objects between different activities.
- Asynchronous method responses are automatically executed on Android UI thread to avoid extra thread management code.
- Generated code does not use reflection, it executes faster and uses very small memory footprint compared to other soap clients.
- Your web service class can be inherited and you can override some default behavior like you can create your own Http Client and do its initializations to allow certificates etc.
Try free download now,
Please click on http://wsclient.neurospeech.com/download to download your trial edition for free and test the web service integration with Android device.