In my previous posts, I outlined my general design for this application. In this post, I will review the creation of the Service class for the Android device. This class will have one purpose: Communicate with the server to sync lists.
One of the things I hadn't decided on was how to construct the Web service. Do I want to use a SOAP style service or a REST style service? The server side for either of these is pretty well defined for me - I've written both SOAP and REST services. The unknown is how to build these on the client side. I did a little Web research, and here are a few of the places I've found that have some good information on these topics:
- It appears that Google isn't interested in including SOAP in the standard Android packages at this time, but there is a 3rd party library that provides this support: kSOAP.
- Google DOES support JSON with Android packaged, org.json.*. I also found a handy RestClient.
Given that JSON is less verbose, and the processing will take fewer resources, I think its a better choice for a constrained device like Android. I also like that I have a RestClient class that I can use, but I can also see what's going on easily inside it. So, the transport protocol for my application will be JSON.
Let's talk about the code. Below is my first attempt at creating the ListService. It's not fully fleshed out. This first version is written to put in the Multithreading and get a basic framework for the application. I will next work on pulling out the data from the JSON objects and store them locally (Using the local HSQL db Android provides). It's also clear I need to understand Intents and bindings much better before I will be able to complete the app. Let's start with a discussion on the ListService. This extends the Android Service class and over rides the onCreate, onStartCommand, and stopService methods. I also added extra logging to see what's going on.
The ListService does nothing but create a RunnableListService and start it in a new Thread. It also kills the flag that keep the polling loop open when stopService is called. Here is the RunnableService:
Most of the heavy lifting is not implemented yet, but this provides the structure to poll the server. I also modified the RestClient slightly to return the JSONObject.
Next - More implementation...