Thursday, November 10, 2011

Soap Request in android

Well you need to understand what a soap request is made of:.

<?xml version="1.0"?>
<soap:Envelope xmlns:soap="">
    <m:GetStockPrice xmlns:m="">

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.

URL u = new URL(url.toString());
URLConnection cnn = u.openConnection();

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

OutputStream outStream = cnn.getOutputStream();

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 in android 2.2 perfectly. As SAX Parser not getting proper response.

SAX Parser example coming soon in next Post

source :

No comments: