Data loading from web
– Android TV application hands on tutorial 16


* You can see the JSON video data used in this post at here: 

Manage data online, keep updated.

In the previous chapter, Background data loading – Android TV application hands on tutorial 15, I introduced LoaderManager and Loader class which helps to load/prepare (maybe time consuming) data in background. One of the example of “time consuming” data preparation is loading data from network. If you can provide the data from the web, app can always show updated, latest information.

In this chapter I will implement web data loading, to show our video contents information. We will prepare data in json format, and upload it to the web ( for now). It means that the video contents can be modified by just changing this json file, and without modifying any java source code.

(NOTE: the sample app works correctly only when your Android TV is connected to the internet from this chapter.)

Video data preparation

By proceeding to show video contents data dynamically from web, I changed the video source. I was using the video contents from PEXELS VIDEOS, this is public domain videos so that we can use it freely.

I summarized Finding videos, photos, musics which you can use freely for introduction of web pages which distributes CC licensed media contents.

Video data list in JSON format

Until this chapter, I was preparing video data using MovieProvider class. It prepares Movie items in hard-coded way. Instead, we want to prepare data in more organized way and JSON format is used to prepare video data. This is the real video list data in JSON format.

I don’t cover JSON format itself in detail in this post. For those who are not familiar with JSON yet,  I will put some of the links how to parse JSON below.

It is not so difficult, and you may also get feeling by looking this post’s sample code for how to parse JSON data. JSON data is consisting of either JSONObject or JSONArray (this relation is similar to the variable and array in usual program language).

There are some useful JSON analyze tool on the web like below

You can try copy and paste this JSON to visually understand what kind of data structure it has, which helps you to understand how you can parse JSON data more easily.

Data loading trigger – VideoItemLoader

Let’s proceed to implementation. As explained in previous chapter, data preparation is now done in loadInBackground method in VideoItemLoader.

Now we want to modify this method to prepare data from the web.
* It may take some time for loading data, which is suitable to do it in background! This is the purpose that we introduced Loader in previous chapter.

Before that modify loadInBackground  method as follows. New class VideoProvider is introduced here for the web data loading.

As you can see VideoLoader class only triggered to do the video data loading. Real loading procedure is done in VideoItemProvider class.

Data loading process – VideoItemProvider

Create a java new class called VideoItemProvider in package. Below is the whole source code of this class.


This class owns a static member LinkedHashMap<String, List<Movie>> sMovieList. It is the data that we want to download from web, and the loading is done in buildMedia method. This method will get the data in following procedure. 

  1. Get JSON data
    JSON data is obtained from the web, URL is defined as

    parseUrl(String url) method, which is called beginning of buildMedia method, is accessing this URL and returns JSONObject.

  2. Parse JSON data
    Parsing JSON data is done inside buildMedia method, by using getJSONObject, getJSONArray, getString, getInt methods etc which are methods of JSONObject & JSONArray class.

    You can compare with above code and this JSON checking by Online JSON Viewer to understand how the data is parsed. Note that JSONObject is enclosed by { }, and JSONArray is enclosed by [ ].

  3. Construct Movie item and put it to sMovieList
    At the end of buildMedia method, movie instance is created from parsed data and is added to sMovieList.



Make sure again that app has a permission to access Internet, otherwise app fails to download video list data from web.


Build and run!


Once you have launched the app, you can notice the video contents has changed from previous chapter’s implementation. This is of course because the data source is completely changed from hardcoded source to JSON formatted source on the web.

Now I can change/update video data anytime without any modification of JAVA source code.


Best data management architecture across application?

In previous chapter, I commented that Loader class is disappointing in the sense that Loader instance and its member cannot be shared among other Activities. We want to use video list data ontained from web. But accessing the internet to prepare data is time consuming and costly operation, we want to access web as less time as possible. So how we can manage data efficiently by reusing data among Activities? The implementation is originally done in Google’s sample source code and I just followed this implementation (and I try to explain its meaning here). 

Since data should be independent from Activity, we can just have a class which handles/manages data. VideoProvider class, introduced in this chapter, is  exactly doing this. There is a static member sMovieList declared as,   

it is the data we have downloaded from the web. And once the data is downloaded, from next time of call of buildMedia method don’t access web but simply returns already created data


Loader class only triggers the timing of data loading, and the real data is managed by VideoProvider class. Its static instance can be referenced from all the activities in this application and we can access same data among activities.

Sponsored Links

Leave a Reply

Your email address will not be published.