• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to create weservice client for android..

Hi
Can anybudy tell me how to create java client (webservice) for android from a wsdl link. I am using ksoap2 tool but there is a limitation with this we can't handle complex data with it. And my project requirment is to handel complex data like multidimention array etc.



Thanks In Advance
Vaibhav Singh.
 
Well you need to understand what a soap request is made of:
Code:
POST /InStock HTTP/1.1
Host: www.example.org
Content-Type: application/soap-xml; charset=utf-8
Content-Length: 299
 
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
  <soap:Header>
  </soap:Header>
  <soap:Body>
    <m:GetStockPrice xmlns:m="http://www.example.org/stock">
      <m:StockName>IBM</m:StockName>
    </m:GetStockPrice>
  </soap:Body>
</soap:Envelope>

The message has a soap:envelope with a soap:header and a soap:body.

The content type of a soap requests is application/soap-xml

Now to send a soap request with Android you have to use a URLConnection and 2 streams.
An inputstream and an outputstream.

Code:
URL u = new URL(url.toString());
URLConnection cnn = u.openConnection();
cnn.setDoOutput(true);
cnn.setDoInput(true);

((HttpURLConnection)cnn).setRequestMethod("POST");
cnn.setRequestProperty("Content-Type", "application/soap-xml; charset=utf-8");
cnn.setRequestProperty("SOAPAction", method);

OutputStream outStream = cnn.getOutputStream();
outStream.write(soapXML);

InputStream in = cnn.getInputStream();

1. you need to open the URLConnection.
2. you set the requestMethod to POST.
3. you set the content-type to application/soap-xml.
4. you set a SOAPAction header to the method you want to use. (This is not shown on the previous example which I have from wikipedia.)
5. you write the soapXml envelope to the outputstream.
6. you read the response from the inputstream.

You can parse the response with an XmlParser if you want to.

I have combined the send and receive code into a class called Webservice. And I made a class called SoapMessage to handle the construction of a Soap envelope.
Then I send the SoapMessage using the send method of the Webservice class and I add an OnReceiveListener to wait for the response.

This is not the most sophisticated method and probably not the most complete method to send and receive Soap messages but it works for me.

I hope I help you with this.
 
Hi thanks for ur post.

Is there any tool by which I can generate java client classes. Actually my wsdl file is too large and I cant handel all of its methodes manually
Thanks
 
Not that I know of. I've created a custom xmlhandler for every method I need. I don't need many methods :)
 
hi
I have this soap reqest code:---


HTTP/1.1 200 OKContent-Type: application/soap+xml; charset=utf-8Content-Length: length<?xml version="1.0" encoding="utf-8"?><soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <GetModuleListResponse xmlns="http://tempuri.org/"> <GetModuleListResult> <ws_module> <module_id>int</module_id> <module_name>string</module_name> </ws_module> <ws_module> <module_id>int</module_id> <module_name>string</module_name> </ws_module> </GetModuleListResult> </GetModuleListResponse> </soap12:Body></soap12:Envelope>//****************************************************//
public​
org.tempuri.Ws_module[] getModuleList() throws Exception {

methodName = "GetModuleList";
String soapAction =
NAMESPACE + methodName;

SoapObject _client =
new SoapObject(NAMESPACE, "GetModuleList");
SoapSerializationEnvelope _envelope =
new SoapSerializationEnvelope(SoapEnvelope.VER11);
_envelope.
dotNet = true;
_envelope.
bodyOut = _client;
AndroidHttpTransport _ht =
new AndroidHttpTransport(Configuration.getWsUrl());
_ht.call(soapAction, _envelope);

// SoapObject response = (SoapObject) _envelope.bodyIn;​
resultRequestSOAP = (SoapObject)_envelope.getResponse();
length = resultRequestSOAP.getPropertyCount();

Ws_module[] module =
new Ws_module[length];
for (int i = 0; i < length; i++) {

module = (Ws_module)
resultRequestSOAP.getProperty(i);

}
return module;

}


//******************************************//
And i need this result from this But I am getting class cast exception at
module = (Ws_module) resultRequestSOAP.getProperty(i);
this line can any budy help me

 
Back
Top Bottom