以一种很好的方式缓存json(loopj)

时间:2013-07-23 20:43:30

标签: android offline-caching loopj

我使用Android的异步Http客户端来加载我的JSON数据。(https://github.com/h-r/android-async-http-with-caching

它完美无缺,但如果用户没有连接,我想缓存json文件。

我知道如何检查用户是否有互联网连接等,但是我使用了一个版本的loopj的异步Http客户端,它为httpresponse的

启用了缓存

问题是我的代码只是一个错误..

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

          });

这是网址(看看它的日期等等。http://calvijn.tk/mobiel/2013-07-19

缓存不起作用有人知道如何解决它,或者如何正确使用缓存库?

如果不可能这样,有没有人知道一个好的开源缓存管理器或者以正确方式缓存JSON文件的东西。

2 个答案:

答案 0 :(得分:2)

我建议将这些JSON响应解析为本地java对象: https://code.google.com/p/google-gson/

我使用GSON将JSON对象序列化为Java类。这样可以更轻松地处理应用中的数据。

如果您想在用户离线时正确保留数据,您可能需要考虑使用Android的SQLite数据库来保存大量对象。对于简单的事情,您可以使用ORMLite。

现在我正在使用ORMLite并构建了一个自定义内容提供程序来处理本地数据。这允许我使用SimpleCursorAdapter和LoaderManagers。

答案 1 :(得分:2)

JsonHttpResponseHandler内,您在onSuccess()中获得了JSON,因此您需要从中缓存数据,而您有两种选择:存储在sqlite或使用file caching,如果没有连接或超时,则会调用onfailure()

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

并在您的响应处理程序中

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

                }
          });