How to call a SOAP web service on Android
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.
- Create a simple HTTP connection by
using the Java standard API
HttpURLConnection
. - Form a SOAP request. (You can make help of SOAPUI to make a SOAP request.)
- Set doOutPut flag as true.
- 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.
- Write entire the SOAP request to the output stream.
- Call the method to make a connection and
receive the response (In my case I used
getResonseCode
). - If your received response code as
- It means you are succeeded to call web service.
- Now take an input stream on the same HTTP connection and receive the string object. This string object is a SOAP response.
- If the response code is other than
200 then take a
ErrorInput
stream on same HTTPobject and receive the error if any. - 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.
BobbyShaftoe
Updated on August 01, 2022Comments
-
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 over 15 yearsYeah, 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 over 15 yearsYeah, this would be the route where I would have to manually parse everything, I wouldn't get an object oriented approach.
-
Neil over 15 yearsYou 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 over 14 yearsThe reason is probably that SOAP is very verbose and doesn't serve the constraints of mobile computing well.
-
MGOwen over 14 yearsThis 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 over 14 yearsSOAP processing is also memory and processor intensive compared to a more concise format like JSON.
-
pcjuzer over 13 yearsI had the pleasure to attend your presentation at JUM XVII. Budapest about this Android stuff. Keep up the good work!
-
rustyx almost 13 yearsFinally a first useful answer after repeating either kSoap or SAX
-
rustyx almost 13 yearsI 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 almost 13 yearsBTW, 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 almost 13 yearsIf 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 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 almost 13 yearsI tried the demo version. Didn't work for me for a simple service. Is isn't worth spending so many $$$$$.
-
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 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 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 over 11 yearsKSoap 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 over 11 yearsHttpTransportSE where can i get this class ??
-
Martin Nuc about 11 yearsDo you know how to deal with Date? Apache CXF translate it to XMLGregorianCalendar which I am not able to create in Android.
-
Hasmukh almost 11 yearsHello, i am using your code but i am getting following error, Response::AuthenticateTestResponse{AuthenticateTestResult=anyType{resultCode=Error; messages=anyType{MessagesTypeMessage=anyType{code=E00014; text=Merchant Authentication is required.; }; }; }; } can you help me please?
-
user435779 over 10 yearsYeah, 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 over 10 yearsThat would work only if axis were compatible with Android. It's not (something about javax.* packages in the axis dependencies).
-
Amitsharma almost 10 yearshi 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 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 over 7 yearshow to add array in xml of request property?
-
Adnen Chouibi over 7 yearsdid you work with session Authentification on IceSoap ?
-
Edijae Crusar over 7 yearsplease help here stackoverflow.com/questions/41573829/…
-
Edijae Crusar over 7 yearsplease help me on this issue stackoverflow.com/questions/41573829/…
-
Tori over 7 yearstwo 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 over 7 yearson the above comment, ignore question #1.
-
Hitesh Sahu almost 7 yearsThere is a special place in hell for those who force developers to use SOAP services instead of REST API.
-
Lapenkov Vladimir over 6 yearsFrom main activity you should use AsyncTask to call this code. Inside this AsyncTask class implement doInBackground to launch this code
-
Sneha Sarkar about 6 yearsIs there any way to send XML headers without using ksoap?