Having begun learning Android development a little while back, and today I made some excellent progress. In order to get all my knowhow straight, I planned to create a simple app that would show me a list of people in a group. The list of people will be shown using a ListView control on the Android UI. The App would fetch this information from a RESTful Web Service written in ASP.NET C# on the Mono platform. I plan to use JSON as my choice of data communication format. Internally, the ASP.NET service will pull this information from a PostgreSQL Server.
Here is how I approached the task. I quickly threw in a table on my PostgreSQL installation which contained the names of people I want to show. On Monodevelop, I created a ASP.NET project with a simple ASPX page that dumps out a JSON for the list of users. To fetch the data from PostgreSQL we need a library called npgsql. Its pretty slick and gets the job done. Using standard mechanisms, I was able to pull data from the database and convert into a readable JSON.
The most important and challenging part (for me) came after this. That is, how to consume this service in my Android application. Coming from C# Windows Forms background, I am used to doing this in a line or two and if the call is small enough, I don’t even bother making it Async. But Android really wanted me to make the network call on a separate thread, and it did make sense! They enforced a good practice from the very beginning. Alright, so my quick reading led me AsyncTask<> class. I found some really nice tutorials on it’s implementation, specially this one: http://www.vogella.com/articles/AndroidPerformance/article.html
I faced an interesting situation here. I was trying to access my local ASP.NET server via a standard URL format like http://localhost:8080/ but that always threw an exception at me that the connection was refused. Then I tried browsing that URL from the Android emulator and that too failed. It took a while but it did strike me then, that localhost would be a loopback address on the emulator itself, not the host development machine. A quick search showed me that the emulator then uses the IP 10.0.2.2 for loopback on the host machine. Modifying my url to http://10.0.2.2:8080/ worked on the Android browser. Pretty sure it would run in my code as well, I ran my App, which to my not-my-surprise, threw the same exception.
Digging further over the internet, I realized I had missed a very basic step. Asking permissions on Android to use the Internet service. So all I had to do was put in a line in the Manifest file like:
The next I ran the App, and viola! it worked! But hey, that didn’t work all the way till showing it on the UI as a list. It only pulled the JSON string into the variable.
I now had to parse the JSON and appropriately get it into a Java consumable format. Gson by Google came to my rescue, which is a nice little framework for working with JSON on Java. I added references to it on my project and wrote some test code to see how it works. It was simple, clean and perfect. You use a Gson class, initialize it, and simply call the fromJson method to convert a JSON string to the desirable type.
Now came another roadblock, i.e. to bind it to the list view. The way ListView works is that it has an Adapter for it which can be of any custom implementation type. I used a standard ArrayAdapter
I am following a self imposed Agile Development methodology and am attempting to follow a Sprint cycle. Sprints are short burst of all round developments that enable us to understand the end-to-end process from the very beginning. For example, in my really silly simple project, I learned how to get PostgreSQL installed on my Linux box, how to get it working on C# in Mono and finally how to consume it on an Android App. This allowed me to do an integrated test of all three work environments that will form the major part of my upcoming days in deep development…
I will sign off now and log my next effort!