Loading Data From the Internet
Explore how to load data from the internet in an Android app by using OkHttp to perform HTTP GET requests and Gson to parse JSON into Java objects. Understand how to implement a background thread for network calls and update the UI thread with retrieved blog articles, enabling a dynamic and responsive Travel Blog details screen.
We'll cover the following...
Overview #
Most of the mobile applications show data that is loaded from the backend web service. The most popular way of communication is via REST, and the most popular format of communication is JSON.
In our Travel Blog Application, we are going to load data from the Internet via simple HTTP GET request, parse a JSON file and display the data from the JSON file in a user-friendly manner.
Dependencies #
While it’s possible to use a Java HTTP client to load data from the Internet, on Android most of the projects use a popular, open-source OkHttp developed by square.
To convert the JSON objects to Java objects we are going to use Gson library, which works nicely with OkHttp.
Let’s add the new library ...