One of the most common functionalities required in mobile applications is to call a web service to retrieve data. This process involves requesting the web service with parameters, receiving the response and parsing it to obtain data.
Today the most common web services types are SOAP and REST. Android does not provide a built in SOAP client, there are many third party libraries that can be used, but we'll see how to call a SOAP web service with native android APIs.

Requesting SOAP web service:
Before proceeding to the code, let's take a look at the SOAP structure:


a soap request can be something like this:

POST /InStock HTTP/1.1
Host: www.example.org
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.w3schools.com/GetItems"

<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Header>
  <m:Trans xmlns:m="http://www.w3schools.com/transaction/"
  soap:mustUnderstand="1">234
  </m:Trans>
</soap:Header>
<soap:Body>
  <m:GetPrice xmlns:m="http://www.w3schools.com/prices">
    <m:Item>Apples</m:Item>
  </m:GetPrice>
</soap:Body></soap:Envelope>


the SOAP request/response is sent as a SOAP Envelope which consists of a SOAP Header and a SOAP Body.

  1. SOAP Header: optional component of the envelop, contains application specific information, such as authentication.
  2. SOAP Body: the actual message sent to/received from the service.
  3. The header can contain a SOAP Action which identifies the desired function to be called by the service.
Calling the service:
to call the SOAP web service you have to do the following:
First: construct the SOAP envelope manually like this:
String envelope="<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"+
  "<soap:Body>"+
    "<GetItems xmlns=\"http://tempuri.org/\">"+
      "<startDate>%s</ startDate>"+
      "<getAll>%s</getAll>"+
    "</Items>"+
  "</soap:Body>"+
"</soap:Envelope>";

where %s are place holders where you substitute request parameters in like this
String requestEnvelope=String.format(envelope, "10-5-2011","true");


Second: call the web service like this:
String CallWebService(String url,
    String soapAction,
   String envelope)  {
  final DefaultHttpClient httpClient=new DefaultHttpClient();
  // request parameters
  HttpParams params = httpClient.getParams();
     HttpConnectionParams.setConnectionTimeout(params, 10000);
     HttpConnectionParams.setSoTimeout(params, 15000);
     // set parameter
  HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), true);
  
  // POST the envelope
  HttpPost httppost = new HttpPost(url);
  // add headers
     httppost.setHeader("soapaction", soapAction);
     httppost.setHeader("Content-Type", "text/xml; charset=utf-8");
     
     String responseString="";
     try {
      
      // the entity holds the request
   HttpEntity entity = new StringEntity(envelope); 
   httppost.setEntity(entity);
   
   // Response handler
   ResponseHandler rh=new ResponseHandler() {
    // invoked when client receives response
    public String handleResponse(HttpResponse response)
      throws ClientProtocolException, IOException {
     
     // get response entity
     HttpEntity entity = response.getEntity();
     
     // read the response as byte array
           StringBuffer out = new StringBuffer();
           byte[] b = EntityUtils.toByteArray(entity);
           
           // write the response byte array to a string buffer
           out.append(new String(b, 0, b.length));        
           return out.toString();
    }
   };
   
   responseString=httpClient.execute(httppost, rh); 

  } 
     catch (Exception e) {
      Log.v("exception", e.toString());
  }
  
     // close the connection
  httpClient.getConnectionManager().shutdown();
  return responseString;
 }

after calling this function, you will have the response as a String, something like this:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetItemsResponse xmlns="http://tempuri.org/">
      <GetItemsResult>
        
        <Items>
          <Item>
            <name>string</name>
            <description>string</ description >
          </iPhoneCategory>
          <iPhoneCategory>
            <name>string</name>
            <description>string</ description >
          </ Item >
        </Items>
      </GetItemsResult>
    </ GetItemsResponse >
  </soap:Body>
</soap:Envelope>


this response needs to be parsed to extract the data.