While accessing data found on the device (data stored in an SQLite database, data stored on internal storage, data stored on external storage or data stored in Shared Preferences) might be fine for some apps, very often, an app might need to send and receive data from a remote server. The data can be either a plain text, XML, JSON or image.
When developing an Android application, we should avoid performing long running operations on the UI thread as this could result in a poor user experience. Sending a request and waiting for the response might take time and thus, as of Android 3.0, the Android system will crash if a network request is made within the UI thread. One way of dealing with network requests is therefore to use AsyncTask which basically allows you to implement multi threading and to perform background operations outside the UI thread. The results are then passed back to the UI thread. Using AsyncTask can however be tedious as you have to do a lot of things manually such as writing code to access the network.
Recently, Google has released a new framework called Volley which handles a lot of the tedious work for you. It also offers various advantages over AsyncTask such as caching, retry mechanism and various request types.
Last FM, a popular music website, provides the Last.fm API which allows developers to build programs using Last FM data which includes details about artists and tracks among others. Once you create an API account and get an API key, the Last.fm API is free to use. The web service allows you to call methods that respond in REST style XML or JSON format.
Here, we use Volley and send a request to Last.fm API to get a list of top chart artists. The response obtained will be a JSON object which we display in a simple TextView.
When using Volley, network operations are managed by a RequestQueue to which you pass request objects. A recommended practice is to create the RequestQueue as a singleton, thus ensuring that the RequestQueue lasts the lifetime of your app.
Also, in order for an application to be able to access the network, we should add the android.permission.INTERNET permission in our AndroidManifest.xml file.
Now that we have a JSON object response, we need to parse it and retrieve data that is of interest to us. In LastFMAppReloaded, we retrieve the name of the first artist returned in the JSON response and display it in a TextView.
For this, we use the JSONObject and JSONArray classes and some of their methods such as getJSONObject() and getJSONArray().
The Last FM Api also returns the urls for pictures of the different artists. In LastFMAppReloadedwithImage, we also retrieve the image for the first artist returned in the JSON response.
Song Explorer v9
Previously, we created the Song Explorer app which basically retrieved a list of artists from a database and displayed it in a listview (RecyclerView). Then on selecting one of the artists, we could get more information about that artist as well as a list of tracks for that particular artist.
Now that we know how to perform network requests with Volley, in Song Explorer 9, we’ll retrieve the list of artists from the Last FM web service instead of from the database.
You’ll notice that selecting a particular artist from the list returns an error. This is because the artist id we used in the database is different from the artist id used by the Last FM api. We’ll fix that in Song Explorer v10.
Song Explorer v10
The Last FM Api also has a method which allows us to retrieve a list of top tracks for a particular artist. In Song Explorer v10, when selecting a particular artist from the list, we are redirected to another activity just like in the previous versions of Song Explorer but this time, we display the artist name, the artist image and a list of top tracks for that artist.
Song Explorer v11
Finally, just like we did for the ForexWithStyle app, we’ll modify the style and add some colors to the Song Explorer app.