Inside your JsonHttpResponseHandler
you get your JSON in onSuccess()
so you need to cache data from there, while you have two options : store in sqlite
or use file caching
, onfailure()
is called when you don't get your object ex no connection or timeout.
public class JSONCache
{
public static void writeToCache( String fileName ,JSONObject jObject )
{
ObjectOutput out = new ObjectOutputStream(new FileOutputStream(new File(fileName)));
out.writeObject( jObject );
out.close();
}
public static JSONObject readFromCache(String fileName )
{
ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(fileName)));
JSONObject jsonObject = (JSONObject) in.readObject();
in.close();
retrun jsonObject;
}
}
and in your response handler
WhtsnxtBase.get(dateje, null, new JsonHttpResponseHandler() {
@Override
public void onSuccess(JSONObject timeline)
{
...
JSONCache.writeToCache( "cache_file_path" , timeline );
//updateUI(timeline);
}
@Override
public void onFailure(Throwable e)
{
Log.e("MyActivity", "OnFailure!", e);
JSONCache.readFromCache( "cache_file_path" );
}
@Override
public void onFailure(Throwable e, String response)
{
Log.e("MyActivity", "OnFailure!", e);
JSONObject timeline = JSONCache.readFromCache( "cache_file_path" );
//updateUI(timeline);
}
@Override
public void onFailure(Throwable e, JSONArray errorResponse)
{
Log.e("MyActivity", "OnFailure!", e);
JSONObject timeline = JSONCache.readFromCache( "cache_file_path" );
//updateUI(timeline);
}
});