I use an Asynchronous Http Client for Android for loading my JSON data.(https://github.com/h-r/android-async-http-with-caching)

It works perfect, but i want to cache json files if user has no connection.

I know how to check if user had internet connection etc, but i use a version of Asynchronous Http Client of loopj who has caching enabled for httpresponse's

The problem is that my code give's just a error..

    WhtsnxtBase.get(dateje, null, new JsonHttpResponseHandler() {//get url with date of json file

                @Override
                public void onSuccess(JSONObject timeline) {    
                       // code removed isn't relevant for this question           
                }

                @Override
                public void onFailure(Throwable e) {Log.e("MyActivity", "OnFailure!", e);}

                @Override
                public void onFailure(Throwable e, String response) {Log.e("MyActivity", "OnFailure!", e);}

                @Override
                public void onFailure(Throwable e, JSONArray errorResponse) {Log.e("MyActivity", "OnFailure!", e);}

          });

This is the url (to see wat kind of date it is etc..http://calvijn.tk/mobiel/2013-07-19)

The cache don't work do anybody know how to fix it, or how to use the cached library properly?

If it isnt't possible this way, does anybody know a good opensource cache manager or something to cache the JSON files the right way.

有帮助吗?

解决方案

I would recommend parsing these JSON responses into a local java object: https://code.google.com/p/google-gson/

I use GSON to serialize the JSON objects into a Java Class. This makes it easier to handle the data within your app.

If you want to persist data properly when the user is offline, you may want to look into using Android's SQLite database for persisting a large array of objects. For something simple, you can use ORMLite.

Right now I'm using ORMLite and built out a custom content provider to handle local data. This allows me to use the SimpleCursorAdapter and LoaderManagers.

其他提示

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);

                }
          });
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top