Wednesday, September 5, 2012

SOAP Calls With SOAPIntentService

WikiSOAP, originally defined as Simple Object Access Protocol, is a protocol specification for exchanging structured information in the implementation of Web Services in computer networks. It relies on Extensible Markup Language (XML) for its message format, and usually relies on otherApplication Layer protocols, most notably Hypertext Transfer Protocol (HTTP) and Simple Mail Transfer Protocol (SMTP), for message negotiation and transmission.

Being a developer, various server interaction methodologies come across you. SOAP being one of them when high security and reliability of transmission is concerned during server communication. Though it adds overhead (large payloads) it largely being used for enterprise level apps.

By default, SOAP isn't bundled with Android (unlike JSON). We have external libraries available which can be used to achieve this task.

One of these is kSoap2, official project http://code.google.com/p/ksoap2-android/

Lets talk about SOAPIntentService


Android Docs : IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.

This "work queue processor" pattern is commonly used to offload tasks from an application's main thread. The IntentService class exists to simplify this pattern and take care of the mechanics. To use it, extend IntentService and implement onHandleIntent(Intent). IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.

All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), but only one request will be processed at a time.

SOAPIntentService working

SOAP Intent Service, accepts a call and executes it on another thread, and responds back to caller via 
Broadcast Receiver

It's also designed to cache on failure when POST is set as request type.

You can acquire source from here : https://github.com/shardul/Android/tree/master/SoapIntentService

Usage


// initialise request helper with appropriate parameters from wsdl definition
SOAPRequestHelper soapRequestHelper = new SOAPRequestHelper(namespace,
    action, methodName, url);

// set properties for request using SOAPNameValuePair instances
SOAPNameValuePair[] props = new SOAPNameValuePair[2];
props[0]=new SOAPNameValuePair("param1","value1");
props[1]=new SOAPNameValuePair("param2","value2");

soapRequestHelper.setProperties(props);


// optionally set headers for request using SOAPNameValuePair instances

SOAPNameValuePair[] headers = new SOAPNameValuePair[2];
headers[0]=new SOAPNameValuePair("header1","headervalue1");
headers[1]=new SOAPNameValuePair("header2","headervalue2");

soapRequestHelper.setHeaders(headers);


//perform a call to SOAPIntentService using

soapRequestHelper.performSOAPRequest(this, responseReceiver);


responseReceiver defined above is a BroadcastReceiver and is an optional parameter. It should only be used when caller do bothers about the response. responseReceiver will be internally registered thus it should accompany a context.unregisterReceiver(responseReceiver) when service call is dealt with.

Heres how responseReceiver code definition should look like


private class ResponseReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
       // initialise response received
 SOAPResponseHelper soapResponseHelper = new SOAPResponseHelper(
    intent);

        //check if theres an exception
 if (!soapResponseHelper.isException()) {
  responseMap = soapResponseHelper.getResponseMap();
  showDialog(DIALOG_RESPONSE);
 } else {
 Toast.makeText(SoapIntentActivityActivity.this,
 soapResponseHelper.getException().toString(),Toast.LENGTH_LONG).show();
 }
 }

}

Additionally caching can be enabled on the call by setting requestType as post of SOAPRequetHelper class


soapRequestHelper.setRequestType(SOAPRequestHelper.REQUEST_TYPE_POST)

And don't forget to include internet permission in your AndroidManifest.xml


<uses-permission android:name="android.permission.INTERNET" />


For further implementation details, you can browse sample code here.

https://github.com/shardul/Android/blob/master/SoapIntentService/src/com/shardul/android/soapservice/activities/SoapIntentActivityActivity.java

Feel free to email me or post comments regarding implementation and enhancements.

No comments:

Post a Comment