How to call a SOAP web service on Android

430,122

Solution 1

Android does not provide any sort of SOAP library. You can either write your own, or use something like kSOAP 2. As you note, others have been able to compile and use kSOAP2 in their own projects, but I haven't had to.

Google has shown, to date, little interest in adding a SOAP library to Android. My suspicion for this is that they'd rather support the current trends in Web Services toward REST-based services, and using JSON as a data encapsulation format. Or, using XMPP for messaging. But that is just conjecture.

XML-based web services are a slightly non-trivial task on Android at this time. Not knowing NetBeans, I can't speak to the tools available there, but I agree that a better library should be available. It is possible that the XmlPullParser will save you from using SAX, but I don't know much about that.

Solution 2

org.apache.http.impl.client.DefaultHttpClient comes in the Android SDK by default. That'll get you connected to the WSDL.

HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("http://www.example.com/" + URL);
HttpResponse response = httpClient.execute(httpGet, localContext);

Solution 3

It's true that due to it's overhead SOAP is not the best choice for data exchange with mobile devices. However, you might find yourself in situation in which you do not control the format of server output.

So, if you have to stick with SOAP, there is a kSOAP2 library patched for Android here:
http://code.google.com/p/ksoap2-android/

Solution 4

To call a web service from a mobile device (especially on an Android phone), I have used a very simple way to do it. I have not used any web service client API in attempt to call the web service. My approach is as follows to make a call.

  1. Create a simple HTTP connection by using the Java standard API HttpURLConnection.
  2. Form a SOAP request. (You can make help of SOAPUI to make a SOAP request.)
  3. Set doOutPut flag as true.
  4. Set HTTP header values like content-length, Content type, and User-agent. Do not forget to set Content-length value as it is a mandatory.
  5. Write entire the SOAP request to the output stream.
  6. Call the method to make a connection and receive the response (In my case I used getResonseCode).
  7. If your received response code as
    1. It means you are succeeded to call web service.
  8. Now take an input stream on the same HTTP connection and receive the string object. This string object is a SOAP response.
  9. If the response code is other than 200 then take a ErrorInput stream on same HTTPobject and receive the error if any.
  10. Parse the received response using SAXParser (in my case) or DOMParaser or any other parsing mechanism.

I have implemented this procedure for the Android phone, and it is successfully running. I am able to parse the response even if it is more than 700 KB.

Solution 5

SOAP is an ill-suited technology for use on Android (or mobile devices in general) because of the processing/parsing overhead that's required. A REST services is a lighter weight solution and that's what I would suggest. Android comes with a SAX parser, and it's fairly trivial to use. If you are absolutely required to handle/parse SOAP on a mobile device then I feel sorry for you, the best advice I can offer is just not to use SOAP.

Share:
430,122
BobbyShaftoe
Author by

BobbyShaftoe

Updated on August 01, 2022

Comments

  • BobbyShaftoe
    BobbyShaftoe almost 2 years

    I am having a lot of trouble finding good information on how to call a standard SOAP/WSDL web service with Android. All I've been able to find are either very convoluted documents and references to "kSoap2" and then some bit about parsing it all manually with SAX. OK, that's fine, but it's 2008, so I figured there should be some good library for calling standard web services.

    The web service is just basically one created in NetBeans. I would like to have IDE support for generating the plumbing classes. I just need the easiest/most-elegant way to contact a WSDL based web service from an Android-based phone.

  • BobbyShaftoe
    BobbyShaftoe over 15 years
    Yeah, I think I will have to build a REST proxy. It seems pretty strange that Google has no interest in providing SOAP support. I tried the kSoap method, it's really not even a serious alternative. It is, at best, an ugly had that requires much scouring of newsgroups.
  • BobbyShaftoe
    BobbyShaftoe over 15 years
    Yeah, this would be the route where I would have to manually parse everything, I wouldn't get an object oriented approach.
  • Neil
    Neil over 15 years
    You mean you wouldn't get a free lunch. Manual parsing doesn't have anything to do with OO. I could parse everything on paper with my most advanced tool being an HB1 pencil and it would still be OO.
  • Neil
    Neil over 14 years
    The reason is probably that SOAP is very verbose and doesn't serve the constraints of mobile computing well.
  • MGOwen
    MGOwen over 14 years
    This answer would be improved if someone could suggest the best alternative to calling web services with an Android App. Once people find this question and read it, that's what most of them will be looking for.
  • Roo
    Roo over 14 years
    SOAP processing is also memory and processor intensive compared to a more concise format like JSON.
  • pcjuzer
    pcjuzer over 13 years
    I had the pleasure to attend your presentation at JUM XVII. Budapest about this Android stuff. Keep up the good work!
  • rustyx
    rustyx almost 13 years
    Finally a first useful answer after repeating either kSoap or SAX
  • rustyx
    rustyx almost 13 years
    I believe by OO the author meant Java-XML binding. What would be your next step in this approach to parse the response? And if it has hundreds of elements, organized in a tree structure?
  • rustyx
    rustyx almost 13 years
    BTW, I would expect at least an HTTP POST not a GET if you want to have any luck with SOAP. Also, where's the Accept and SOAPAction headers? If you really want to go commando it will take much more than these 4 lines to consume a SOAP service.
  • Neil
    Neil almost 13 years
    If I had my druthers I wouldn't use SOAP for a web serivce to start with. I'd go RESTful with XML or better yet JSON. You are writing a client on a mobile device. Do you really want to use the device itself to parse WSDL, automatically create the supporting classes and then the proxy itself before even making its first web service call? How often are you going to do this? What's the application exactly? O.o
  • Admin
    Admin almost 13 years
    @MGOwen The best alternative I can see is a proxy. Implement a REST-ful interface that proxies the SOAP envelopes through WSDL2Java.
  • Tushar
    Tushar almost 13 years
    I tried the demo version. Didn't work for me for a simple service. Is isn't worth spending so many $$$$$.
  • Akash Kava
    Akash Kava almost 13 years
    @MGOwen please check my answer, currently it is best and only alternative as customers get support for the license fees paid.
  • Bjarke Freund-Hansen
    Bjarke Freund-Hansen over 12 years
    @NeilD: What if you were forced by circumstances to use SOAP, e.g. the webserver uses SOAP and you have not control over this?
  • Neil
    Neil over 12 years
    @bjarkef: kSOAP 2 seems to be a popular choice, as per the answer above. Although there's nothing innately unmanageable about consuming SOAP without it, it's just more work. You could also write a proxy server and decouple the SOAP web service in question and your mobile client, but that may turn out to be a much larger task than simply adopting kSOAP or some other similar library.
  • kinghomer
    kinghomer over 11 years
    KSoap library can't manage huge request or response. All data is loaded on memory, so could happen that App crashes and OutOfMemory exception occurs.
  • Noman
    Noman over 11 years
    HttpTransportSE where can i get this class ??
  • Martin Nuc
    Martin Nuc about 11 years
    Do you know how to deal with Date? Apache CXF translate it to XMLGregorianCalendar which I am not able to create in Android.
  • Hasmukh
    Hasmukh almost 11 years
    Hello, i am using your code but i am getting following error, Response::AuthenticateTestResponse{AuthenticateTestResult=an‌​yType{resultCode=Err‌​or; messages=anyType{MessagesTypeMessage=anyType{code=E00014; text=Merchant Authentication is required.; }; }; }; } can you help me please?
  • user435779
    user435779 over 10 years
    Yeah, this is not really answering the OP's question. Turns out to be useful to me (in my case, I don't even really care that much about the SOAP response, I just need to make sure the service is alive; so even though it's disappointing there's no built in SOAP library, a refresher in how to do a basic POST in Android is handy) but it's not really answering this question.
  • PaulProgrammer
    PaulProgrammer over 10 years
    That would work only if axis were compatible with Android. It's not (something about javax.* packages in the axis dependencies).
  • Amitsharma
    Amitsharma almost 10 years
    hi Amit i m also amit ..... Can u tell me how response we compare when we got response is successful or not so how and what conditions to check response ......
  • Amitsharma
    Amitsharma almost 10 years
    @ Wajdi hh how to compare response res from giver response result :- success ....."responce =1~Successfull".... so how we compare that result from condition which are from server side ........
  • droidster.me
    droidster.me over 7 years
    how to add array in xml of request property?
  • Adnen Chouibi
    Adnen Chouibi over 7 years
    did you work with session Authentification on IceSoap ?
  • Edijae Crusar
    Edijae Crusar over 7 years
  • Edijae Crusar
    Edijae Crusar over 7 years
    please help me on this issue stackoverflow.com/questions/41573829/…
  • Tori
    Tori over 7 years
    two questions on this: 1. How is this called from main activity? 2. What should be the value of StringUrlShipment that's passed as a parameter?
  • Tori
    Tori over 7 years
    on the above comment, ignore question #1.
  • Hitesh Sahu
    Hitesh Sahu almost 7 years
    There is a special place in hell for those who force developers to use SOAP services instead of REST API.
  • Lapenkov Vladimir
    Lapenkov Vladimir over 6 years
    From main activity you should use AsyncTask to call this code. Inside this AsyncTask class implement doInBackground to launch this code
  • Sneha Sarkar
    Sneha Sarkar about 6 years
    Is there any way to send XML headers without using ksoap?