Apart from XML, JSON is a very common format used in API responses. Its simplicity has helped to gain quite the adoption in favor of the more verbose XML. Additionally, JSON can easily be combined with REST producing clear and easy to use APIs. Android includes support for JSON in its SDK as someone can find in the JSON package summary. However, using those classes, a developer has to deal with low level JSON parsing, which in my opinion is tedious and boring. For this reason, in this tutorial, I am going to show you how to perform automatic JSON parsing.For this purpose we are going to use the Google Gson library. From the official site:
Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.
There are a few open-source projects that can convert Java objects to JSON. However, most of them require that you place Java annotations in your classes something that you can not do if you do not have access to the source-code. Most also do not fully support the use of Java Generics. Gson considers both of these as very important design goals.
Excellent, exactly what we need. Before delving into code, you might want to take a look at the Gson User Guide and bookmark the Gson API Javadocs. Let’s get started by downloading Gson, with the current version being 1.6. We need the gson-1.6.jar from the distribution.
Let’s proceed with creating an Eclipse project named “AndroidJsonProject” as follows:
There are many tutorials around the web that explain some stuff about web development in Java using servlets and JSP pages, however I have never found aconcise, simple enough for beginers, tutorial. A tutorial like that should explain the whole process of creating a simple web app, including the frontend, the backend, and most importantly, the ways that someone can use to interact with both of them. It is not enough to show how to get information from the server, it is also important to kn0w how to differentiate between the information in a structured way, as well as to know how to make changes to the backend through the application environment.
What we want to achieve by this post here, is to guide through the whole process of creating a complete “toy” web application. It is a “toy” application in the sense that it does only two things and we are using no extra features to make the environment beautiful. The purpose of the application will be simple:
- Add a band name with a list of albums (separated by commas) and press the “Submit” button to add them to the database.
- Press the “Show bands!” button to get a list of the bands, or the “Show bands and albums!” button to get a list of bands with their albums.
The look of the app is as bare-bones as possible, but the code behind it is everything you need to start creating your own dynamic web applications, which are most commonly called CRUD applications (Create, Read, Update, Delete). They are called that way because all their functionality can be abstracted to these very basic commands.
Before we start creating the application step-by-step, let’s take a look at all the tools that we are going to work with in this example:
- Eclipse Luna
- Java 7
- Tomcat 7 (web application server)
- Gson 2.3 (Google Java library)
Getting Started with Google GSON http://ow.ly/BwS86