Using a Loader for loading data from a HTTPRequest

Hello Little Androids,

For two weeks I have been doing a single thing : investigating and developing the way to do HTTP requests to an API in order to get the content from it and show it as a list.

Unfortunately I am not enabled to post the code, but if you follow the steps and links I give you, I am sure you will get to the solution.

Elementary vocabulary:
HTTP → It is the protocol used for all the transactions in the w.w.w.
API → It is the way to access to the database of our server. In our case we will get data from the server.
Json → It is the most used way to exchange data in an easy and a fast way.

I have followed this steps to achieve my goal:

1. Do the HTTP request.

There are a lot of libraries that implement this (Volley, loopj, etc). But talking to my colleagues I decided to do it with loopj. It does the request asyncronously.
Download loopj library here

2. Take the Json from the response and create and ArrayList with this data.

It is very easy, you just have to use the Gson library that does it all for you.
Gson Library documentation

There are others like Jackson that are also good.

Gson gson = new Gson();
Search search = gson.fromJson(json, YourClass.class);

3. Download an Image from the field “String url” of our code.

You can see how I have done this in the following link.

With these three first steps the app did what it was required. However, it was not a proper solution: LOADERS. Why?

  • AsyncTask doesn’t store the data, so if you wanted to do a scrollView it had to charge again the pictures shown.
  • AsyncTask doesn’t follow the Lifecycle of the Fragment/Activity, so if you change the orientation of your device, the AsyncTask will be done again.

So the next step was to…

4. Implement the 3 points above with Loaders.

Loader Links:

And finally, the post that has saved my life!!! It tells you how to implement a loader for rest clients.

However, I have changed it a little, because I have done all the treatment of the data (not only the http request, but also the treatment of the Json Object and the download of images from a url) inside the loader.

5. Finally, I will try to do the lazy load.

Lazy load is what facebook or twitter do with their data. They give it in “packets of 10 items”. When I finish it I will tell you more about it.

If you have any doubt don’t hesitate to contact me :D,



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s