In Hour 10, "Building Forms to Collect User Input," you created the settings screen and stored the data in SharedPreferences. Now you will modify this screen to upload a copy of the player settings to the server (in addition to storing it in SharedPreferences). With this particular implementation, the client device always has the last word in the data. The application server simply stores a copy of the settings. This particular application does not have two-way synchronization; that would be beyond the scope of this book and take quite a bit longer than an hour to explain.
To keep the code readable and demonstrate important networking functionality, we've taken some liberties in the application architecture and have not always used the network in the most efficient fashion. However, the methods we have used allow us to demonstrate important features such as background updating, progress indicators, and more features of the AsyncTask class.
To communicate with the application server, you can leverage the HttpClient package (org.apache.http) included in the Android SDK. This package provides utilities for handling a wide variety of HTTP networking scenarios within your application.
You will learn how to use HttpGet to post query variables in the same way a web form submission would work, using the HTTP GET method. Then you'll learn how to use HttpPost to post form variables and upload the avatar graphic, in the same way a web form might use the HTTP POST method.
The application server was written with HTML web forms in mind. In fact, the server was tested using a standard HTML form before the Android client was written. By developing a web client before the Android client, you ensure that the client/server communication protocols used are standard and cross-platform compatible. When you use this procedure, you know that any platform—including Android—that can handle web form-style GET and POST methods will be compatible with this application server. This way, the application can rely on the Apache HTTP libraries— primarily the org.apache.http.client package.
The application server developed for use with this book is not part of the code on the website for this book. However, it is available for your use, in open source form, at http://code.google.com/p/triviaquizserver/.
Network upload operations can take some time. Therefore, all network-related calls should be handled asynchronously, separately from the main user interface thread. This can be accomplished by using the Java Thread class or by using the Android AsyncTask class. Also, make sure to inform the user of lengthy operations by using a mechanism such as a ProgressBar control. For more information, see Hour 15.
Was this article helpful?