0

Context
I have a news activity which contains a ListView . Each item in the list contains some text and one image. Now every time the user opens the activity, the application makes a request to the server for getting data. Unfortunately if the user doesn't have an active internet connection, I simply put a Toast saying "Please check your internet connection"

Requirements
I would like to show news article from the last session if the user doesn't have an active internet connection. I define session as the time when user launches the activity and exits it by pressing back button.

Libraries used
I am using Retrofit Library for making Http Requests and Picasso for loading images.

What I have tried so far
While exploring Retrofit, I found out that retrofit by default caches the response for some time. So without any internet connection also I am able to get the data. In Picasso also, I found that we can save the image to any location on the device.

How to go for its Implementation ?
Since it will be a big change in my application, I wanted to know what is the best way to enable offline reading using the above two libraries? How do other applications manage to do so? Also if I can get some references or some blogs regarding this implementation, then it would be great.

thedarkpassenger
  • 7,158
  • 3
  • 37
  • 61

3 Answers3

1

Use the internal storage of the application.

Parse the image to a byteArray and the text to a single String so you can easily convert this also to a byteArray.

Name the files so you can easily retrieve them and link them back together.

Community
  • 1
  • 1
TheTool
  • 309
  • 2
  • 12
0

You can storage the last updated data on local, with SharedPreferences or json file. Instead of show toast, you can load the last updated data from local.

Javi Chaqués
  • 141
  • 1
  • 12
  • SharedPreference is not the best choice because it's more for preferences than datas. – Cocorico Dec 21 '15 at 08:56
  • You can write a .json file and load it when the user doesn't have an active internet connection. – Javi Chaqués Dec 21 '15 at 09:29
  • Yes, you can write .json files in it, but it's a bad design. You have to read and parse all the json files to make orederby or other things. So, I recommand to start now with DB. I think he will later make other operations with items like find owner, order them, modify them and other stuff, so start now with DB is a good choice or you're assuming you will not do complex operation on items. – Cocorico Dec 21 '15 at 09:48
0

Picasso is a good choice for images, it saves images locally and reuse it automatically later.

For your items, I suggest to use a small local DataBase : in Android, we use SQLiteDataBase. here is a small tutorial : http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

So, I suggest you this pattern

  • user opens activity
  • system retrieves data from database
  • in parallel, system starts to download the new items.
  • When new items have been downloaded, you should notice user like 9gag or Facebook apps do.
Cocorico
  • 1,998
  • 1
  • 22
  • 38