How to create weservice client for android..


Last Updated:

  1. vaibhav.dec

    vaibhav.dec Member This Topic's Starter

    Joined:
    Aug 26, 2010
    Messages:
    6
    Likes Received:
    0
    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.
     

    Advertisement
  2. id0001

    id0001 Well-Known Member

    Joined:
    Apr 20, 2010
    Messages:
    142
    Likes Received:
    23
    I wrote my own class for handling soap data. It's just some xml.
     
    vaibhav.dec likes this.
  3. vaibhav.dec

    vaibhav.dec Member This Topic's Starter

    Joined:
    Aug 26, 2010
    Messages:
    6
    Likes Received:
    0
    Hi, Thanks for ur suggestion.

    Can u plz tell me how.

    Thanks
     
  4. id0001

    id0001 Well-Known Member

    Joined:
    Apr 20, 2010
    Messages:
    142
    Likes Received:
    23
    Well you need to understand what a soap request is made of:
    Code (Text):
    1.  
    2. POST /InStock HTTP/1.1
    3. Host: www.example.org
    4. Content-Type: application/soap-xml; charset=utf-8
    5. Content-Length: 299
    6.  
    7. <?xml version="1.0"?>
    8. <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
    9.   <soap:Header>
    10.   </soap:Header>
    11.   <soap:Body>
    12.     <m:GetStockPrice xmlns:m="http://www.example.org/stock">
    13.       <m:StockName>IBM</m:StockName>
    14.     </m:GetStockPrice>
    15.   </soap:Body>
    16. </soap:Envelope>
    17.  
    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 (Text):
    1.  
    2. URL u = new URL(url.toString());
    3. URLConnection cnn = u.openConnection();
    4. cnn.setDoOutput(true);
    5. cnn.setDoInput(true);
    6.  
    7. ((HttpURLConnection)cnn).setRequestMethod("POST");
    8. cnn.setRequestProperty("Content-Type", "application/soap-xml; charset=utf-8");
    9. cnn.setRequestProperty("SOAPAction", method);
    10.  
    11. OutputStream outStream = cnn.getOutputStream();
    12. outStream.write(soapXML);
    13.  
    14. InputStream in = cnn.getInputStream();
    15.  
    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.
     
    vaibhav.dec likes this.
  5. vaibhav.dec

    vaibhav.dec Member This Topic's Starter

    Joined:
    Aug 26, 2010
    Messages:
    6
    Likes Received:
    0
    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
     
  6. id0001

    id0001 Well-Known Member

    Joined:
    Apr 20, 2010
    Messages:
    142
    Likes Received:
    23
    Not that I know of. I've created a custom xmlhandler for every method I need. I don't need many methods :)
     
  7. vaibhav.dec

    vaibhav.dec Member This Topic's Starter

    Joined:
    Aug 26, 2010
    Messages:
    6
    Likes Received:
    0
    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

     
  8. id0001

    id0001 Well-Known Member

    Joined:
    Apr 20, 2010
    Messages:
    142
    Likes Received:
    23
    shouldn't your ws_model element in your xml request be capitalized?
     
  9. vaibhav.dec

    vaibhav.dec Member This Topic's Starter

    Joined:
    Aug 26, 2010
    Messages:
    6
    Likes Received:
    0
    Hello Friend
    Thanks for ur quick reply

    Here Ws_module is a class type in which I want to typecast.
     

Share This Page

Loading...