How to call a SOAP web service on Android [closed]

2018-12-31 01:40发布

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.

26条回答
千与千寻千般痛.
2楼-- · 2018-12-31 02:19

I am sure you could make a little SOAP client with Axis. Axis installation instructions.

查看更多
若你有天会懂
3楼-- · 2018-12-31 02:20

Few months ago I was working with jax-ws web service in j2ee application, There we were using CXF wsdl2java to generate WS client stub from the WSDL file and with those client stubs we consumed the web services. Few weeks ago, when I was trying to consume the web service in the same way in android platform I couldn't, because the android jar has not all the "jax-ws" supporting classes in it. That time I didn't find any such tool ( if I wasn't failed to google efficiently) to meet my requirement --

  • Get the client stub from the WSDL.
  • And call the Service with some argument (java business request object).
  • Get the Response Business Object.

So, I developed my own Android SOAP Client Generation Tool. Where you have to follow these steps :

  • From WSDL Get WS Client Stub, Put it in your project.
  • Say for Some Service "ComplexOperationService", Instantiate the Service, Get the Endpoint port and call the service method, and get the response from the Web service :

eg:

ComplexOperationService service = new ComplexOperationService( );
ComplexOperation port= service.getComplexOperationPort();    
SomeComplexRequest request = --Get some complex request----;    
SomeComplexResp resp = port.operate( request  );
  • You don't need to care about the service class/req/response classes or any other classes and the method as well, as you know its all are generated from WSDL.
  • And of course you needn't be aware of the soap action/envelop/namespace etc. Just call the method as we, developers do all the time.
查看更多
人气声优
4楼-- · 2018-12-31 02:21

You can have a look at WSClient++

查看更多
何处买醉
5楼-- · 2018-12-31 02:22

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/

查看更多
孤独总比滥情好
6楼-- · 2018-12-31 02:25

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.

查看更多
浮光初槿花落
7楼-- · 2018-12-31 02:28

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);
查看更多
登录 后发表回答