Sunday, December 5, 2010

SOAP client in Android

SOAP is most common protocol used to communicate b/w client and server. SOAP is important protocol where security, platform independence, and performance concern.

To create client either we make our own interface and implement all required functionality to it like parsing and making object or we can use any third party api like ksoap2.

In ksoap2 we can easily made it possible. Here is some sample of code to demonstrate it.

// Create the outgoing message

SoapObject requestObject = new SoapObject("x", "getQuote");

// ask for the specially encoded symbol in the included service

requestObject.addProperty("symbol", "XXX");

// use version 1.1 of soap

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

// add the outgoing object as the request

envelope.setOutputSoapObject(requestObject);

new MarshalFloat().register(envelope); // not really needed for j2se version

// Create a transport layer for the J2SE platform. You should change this for

// another transport on midp or j2me devices.

HttpTransportSE transportSE = new HttpTransportSE("http://localhost:8080/axis/StockQuoteService.jws");

// turn on debug mode if you want to see what is happening over the wire.

transportSE.debug = true;

try {

// call and print out the result

transportSE.call("getQuote", envelope);

System.out.println(envelope.getResponse());

} catch (Exception e) {

// if we get an error print the stacktrace and dump the response out.

e.printStackTrace();

System.out.println(transportSE.responseDump);

}

This way we can client SOAP client for Android. kSOAP2 internally used kxml parser which is pullparser thats why this api is assumed to be highly performance api.

kSOAP is an open source project you can download it from here.