Client

This brings us to the final piece, the client. In order to execute on a mobile phone, a developer might choose to use the MIDlet framework, as I have. Interacting with the AlertServlet amounts to just a few lines of code. Below is a snippet of code from AlerterClient.java (available from the example code.) For the purposes of this example assume the service resides on a host running at http://somehost.com:8181/services.

Once again, we register the SystemAlert class with the ClassMap. Secondly, we create a SoapObject for the sole purpose of making the request — your application might wish to send some custom KvmSerializable object instead. We then create an HttpTransport object passing it the destination address for our Service; notice that the “service” parameter is also appended. Lastly, we initiate the transaction via method tx.call(). If all is well a SystemAlert object is returned; and if something should go wrong, the SOAP message will contain a SOAPFault element instead of its normal payload. Subsequently, an exception will be thrown from within the call() method.

As you can see, developing an application like this is not so difficult. By leveraging KSOAP for your wireless application, you can help make it a more powerful and reliable one. Since much of the infrastructure is provided, you as the developer can spend more time focusing on the important aspects of development such as the business logic.