This tutorial outlines difference (comparison) between WSClient++ and KSoap tool to connect web services.

Sample Web Service

Lets consider a very simple web service, that will return simple “Hello World” string.

KSoap Web Service Call for Android

KSoap2 Call on Android
  1. private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
  2. private static final String METHOD_NAME = "HelloWorld";
  3. private static final String NAMESPACE = "http://tempuri.org/";
  4. private static final String URL = "http://localhost/TestWeb/WebService.asmx";
  5.  
  6. public object GetResponse() throws Exception
  7. {
  8.     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
  9.     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
  10.     envelope.dotNet=true;
  11.     envelope.setOutputSoapObject(request);
  12.  
  13.     HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
  14.  
  15.     androidHttpTransport.call(SOAP_ACTION, envelope);
  16.  
  17.     Object result = (Object)envelope.getResponse();
  18. }

Wow, you can see just to call one method, there is 18 lines of code. Well unfortunately you have to write this manually.

WSClient++ Web Service call for Android

WSClient++ generates source code that does all SOAP parsing/encoding for you on the fly. And the code that you have to use in your project will be as small as below,

WSClient++ call on Android
  1. public object GetResponse() throws Exception
  2. {
  3.     HelloWorldService service = new HelloWorldService();
  4.     service.setBaseUrl("http://localhost");
  5.     return service.HelloWorld();
  6. }

The total lines of code is now 6, and that too, actual service call is as small as just one line. However, in order to write KSoap call on asynchronous, you have to wrap your code around AsyncTask and write more code, where else with WSClient++ it is very easy to write asynchronous call, which works perfect without any hassle.

Async call on Android
  1. public void GetResponseAsync() throws Exception
  2. {
  3.     HelloWorldService service = new HelloWorldService();
  4.     service.setBaseUrl("http://localhost");
  5.     service.HelloWorld(
  6.         new service.HelloWorldResponse(
  7.             {
  8.                 public void onResult(string s){
  9.  
  10.                 }
  11.                 public void onError(Exception ex){
  12.  
  13.                 }
  14.             }
  15.     )
  16.     );
  17. }

Comparison

  WSClient++ KSoap
Automatic Code Generator YES NO
Automatic Type (Class) generation for every Soap Input/Output YES NO
Class with IParcelable Implementation YES NO
On the fly Encoding YES NO
Memory Footprint Small Big
Execution Time on Client Small Big
.NET Service Call Detection Auto Manual
Automatic Asynchronous Call Class Generator YES NO
Share
Tagged with:
 

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++.

Code Generation

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.

WSClient-Android-Java-Blackberry-Soap-Web-Service

Additional Libraries

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-Source-Library

Generated Code

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.

Calling Web Service
  1. // Create new instance of service
  2. InventoryServiceAsync ws = new InventoryServiceAsync();
  3.  
  4. // set base url
  5. ws.setBaseUrl("http://myserver.com");
  6.  
  7. // invoke method
  8. ws.createProduct(
  9.     "ProductName", // parameter 1
  10.  
  11.     // inline anonymous class to handle response
  12.     ws.new CreateProductResult(){
  13.  
  14.         // called when call was successful and
  15.         // will return the expected result
  16.         public void onResult(Result rs){
  17.         }
  18.  
  19.         // called in case of any exception
  20.         public void onError(Exception ex){
  21.         }
  22.  
  23.     }
  24.     );

Following sample illustrates same service called synchronously.

Calling Web Service
  1. // Create new instance of service
  2. InventoryServiceAsync ws = new InventoryServiceAsync();
  3.  
  4. // set base url
  5. ws.setBaseUrl("http://myserver.com");
  6.  
  7. try{
  8.  
  9.     // invoke method
  10.     Result rs = ws.createProduct("ProductName");
  11.  
  12. }catch(Exception ex){
  13.     // error handler
  14. }

Additional Advantages

There are more advantages of WSClient++ outlined below,

  1. Every class implements IParcelable interface, which makes it easier to transfer objects between different activities.
  2. Asynchronous method responses are automatically executed on Android UI thread to avoid extra thread management code.
  3. Generated code does not use reflection, it executes faster and uses very small memory footprint compared to other soap clients.
  4. 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.

Share
Tagged with:
 

This tutorial outlines how to call Soap Web Services from iOS or Mac OS XCode Projects in just few steps using Native Objective-C libraries and additional source code provided by WSClient++.

Code Generation

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 Objective-C and choose iOS or Mac OS as per your choice. As shown in following picture.

WSClientCocoaScreen

Generated Code

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 set a delegate which will have implementations of expected protocol, that will have actions for each method’s response and OnError.

Request/Response and Custom Types

For every custom xml type, WSClient++ will generate corresponding Objective-C class and it will marshal every request in xml as expected by Soap. You can simply invoke and access these objects as your other Objective-C classes, and NSWSDL.* files will do xml parsing and processing for you in background.

Invoking Web Service

After including generated files and NSWSDL.* (and NSXMLDocument.* for iOS)  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.

Delegte Methods
  1. -(void) onError: (NSError*) error{
  2. //   … process error…
  3.  
  4. }
  5.  
  6. -(void) onCreateProduct:
  7.     (InventoryServiceAsync*)service
  8.     result:(Product*) r{   
  9. //… process result… you will get returned header here…
  10.  
  11. }

In the same class, we will call method to invoke the service as below,

Calling Web Service
  1. -(void) callService{
  2.     InventoryServiceAsync* ws = [InventoryServiceAsync service];
  3.     // set base url for entire application
  4.     [BaseWebService setGlobalBaseUrl: @""http://domain.com" ];
  5.     // //set base url only for this service
  6.     // [ws setBaseUrl: @"http://domain.com"];
  7.     [ws setDelegate: self]; // dont forget to set delegate
  8.     [ws CreateProduct: @”Sample”];
  9. }

So you see, calling Soap Web Service is very easy as you are calling your other native Objective-C API.

Download today your free trial at http://wsclient.neurospeech.com

Share
Tagged with: