Well you need to understand what a soap request is made of:.
Code:
<?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>Google</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();
- You need to open the URLConnection.
- You set the requestMethod to POST.
- You set the content-type to application/soap-xml.
- 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.)
- You write the soapXml envelope to the outputstream.
- 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 : http://androidforums.com/developer-101/233069-how-create-weservice-client-android.html
No comments:
Post a Comment