Home » Android » Generating Java from WSDL for use on Android with ksoap2-android SOAP client?

Generating Java from WSDL for use on Android with ksoap2-android SOAP client?

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have to access a existing SOAP webservice from an Android application. I have been provided some WSDL files describing the webservice. Reading some other answers here on SO, it seems ksoap2-android is the way to go, with respect to which SOAP client to use.

The next issue is then how to generate the Java classes needed from the WSDL files, and this is where I am coming up short. As far as I can see there are the following options:

  1. AXIS2 code generator
  2. WSDL2ksoap
  3. JAX-WS wsimport tool

I initially tried #1, with the AXIS2 eclipse plugin for wsdl2code generator. The wizard did successfully generate a lot of Java code, however it also changed my android project to some kind of webservice project, and I was never able to get anything that was generated to compile, let alone work with ksoap2-android. Has anybody has success with this?

I am not able to run wsdl2ksoap successfully, as it seems to require a running webservice, and all I have at the current point in time is WSDL files. Likewise from reading the webpage, it seems to be a project in its initial stages, and not really ready for prime time.

JAX-WS wsimport I have not had a chance to try yet. However I am unsure if what it generates will work with ksoap2-android?

Question: How can I generate Java files from WSDL files, for use on Android with ksoap2-android SOAP client library?

Thanks a lot in advance.

(PS: Yes, the choice is SOAP, it is suboptimal for Android use, but I cannot change that.)

How to&Answers:

I found this tool to auto generate wsdl to android code,

http://www.wsdl2code.com/example.aspx

Here is the code:

public void callWebService()    {
    SampleService srv1 = new SampleService();
    Request req = new Request();
    req.companyId = "1";
    req.userName = "userName";
    req.password = "pas";
    Response response =  srv1.ServiceSample(req);
}

Answer:

I had similar situation (I had only wsdl file without working webservice). I’ve used

http://easywsdl.com/

to generate classes for android without any problem. This tool uses ksoap library. The great thing with this tool is that it supports WCF extensions and types like data contract with IsReference attribute or Guid.

Answer:

My conclusion after quite a bit of researching is that there is no such (mature) tool available, unfortunately. Neither AXIS2 or JAX-WS will work on Android, and WSDL2ksoap is simply too immature for any real use.

However there is a proprietary tool called wsclient++ that will do the job really well. (Read update below, when put to real use, it does not stand the distance at all.) It does not use the ksoap2-android client library, it has it’s own.

The client library is a bit crude as it has a hard dependency on the http transport, making (unit) testing a bit complicated. But it can be modified quite easily to allow DI, as the source is available in the distributed jar file.

The wsdl to java generator however works just perfect, and will save us tons of time.

Update
After working with wsclient++ for a while, it is clear that the generated classes are really crude, and does not handle error cases at all. (Every method declares throws Exception).

We are no longer using wsclient++, and I would not recommend anyone to use it!
We have not really found any working alternative, unfortunately. :/

In the end we converted our WSDL files using AXIS2, and then wrote a bunch of custom script to strip and transform the generated java files to something that will build on android using ksoap2-android library. Very hackish, and needs tons of manual labor to run. Unfortunately. If you find a better way, or one comes up, please provide a new answer.

Answer:

I use Apache CXF tool just to create dto, and i wrote a class to perform a basic unmarshalling based on name of elements

Answer:

A bit late on this, but there is a ksoap2 stub generator under development, and I successfully used it to create the stubs.

http://ksoap2-stub-gen.sourceforge.net/

Also someone made it availabe as an online service (i.e. you give your WSDL’s URL and the service will return a zip file containing the stubs).

http://www.davidgouveia.net/2011/04/online-stub-generator-for-android-applications-using-ksoap2/

Answer:

I have used for iPhone too some auto-generated classes I wanted to see here too.

wsdl2code is one of the similar what I have used at iPhone. Give an url with wsdl file you will get some classes to download. For me the hardest part it was to download the required parts. It took more than 2 minutes of searching 🙂 ksoap2-android-assembly-3.0.0-jar-with-dependencies.jar needed to download ad drag-and-drop to ADT ( Eclipse) . It is super easy, especially if you have used the counterpart at iPhone. – a similar tool I have used.

However in my case I am not happy at all with the solution, because I see I am using cannon, a set of cannons to shot a sparrow. In my case it should be used a HTTP Post and not including dependencies from other libraries.

To be honest I don’t care to much, because once the server side believe we have unlimited battery power and unlimited data plan, than I close my eyes and I don’t care about marshaling-unmarshaling overheads, which use the CPU ( battery ) increase the data transmitted over network.

In worse case it should be a JSON + HTTP POST not SOAP for mobiles…

I would suggest to talk at server side guys and explain for they why it will not good if they do 2 click on wizards and we do other click on forms to get the generated code. At least while the application is not a huge one, even than should be budget to optimise for mobile a few interfaces implementations.