Android 缓存json的最佳方法

Android 缓存json的最佳方法,android,json,android-sqlite,android-preferences,Android,Json,Android Sqlite,Android Preferences,我的应用程序不仅应该在联机模式下工作,而且应该在脱机模式下工作。出于这个原因,我正在考虑寻找兑现数据的最佳方式。我不喜欢对存储数据使用SharedReference,但在android文档中,preferences值允许的最大字符大小是8192。我不知道这行不行?我试图放弃使用FileCashing或sqLite cashing的想法 那么,你们认为与文件兑现或SqLiteCaching相比,什么是最好的共享参考 根据您的要求,我推荐使用SQLite数据库 因为共享首选项适用于配置存储—通常是小

我的应用程序不仅应该在联机模式下工作,而且应该在脱机模式下工作。出于这个原因,我正在考虑寻找兑现数据的最佳方式。我不喜欢对存储数据使用SharedReference,但在android文档中,preferences值允许的最大字符大小是8192。我不知道这行不行?我试图放弃使用FileCashing或sqLite cashing的想法


那么,你们认为与文件兑现或SqLiteCaching相比,什么是最好的共享参考

根据您的要求,我推荐使用SQLite数据库

因为共享首选项适用于配置存储—通常是小数据/字符串

文件缓存很难管理,所以我建议使用SQLite—易于管理,并且能够存储大量数据

考虑到性能,如果索引数量不是那么大,SQLite数据库应该具有可接受的性能。例如,仅比文件缓存慢几毫秒


您可以将这两种方法结合起来。使用索引偏移量存储在SQLite中的随机访问文件。

我个人喜欢以下方法。创建一个可以保存内容的SQLite数据库。然后,使用适配器和内容提供程序将用户界面直接绑定到数据库,每当数据发生更改时,这些适配器和内容提供程序都会发送通知,以便UI能够自我更新。等式的最后一部分是某种形式的同步服务,它下载内容并将其异步保存到数据库。这样,您可以非常轻松地管理数据,因为数据都在同一个位置。对于你的应用程序,你唯一需要弄清楚的是你如何决定何时更新或从数据库中删除数据


将缓存目录中的json另存为文件

保存:

// Instantiate a JSON object from the request response
JSONObject jsonObject = new JSONObject(json);
// Save the JSONOvject
ObjectOutput out = new ObjectOutputStream(new FileOutputStream(new File(getCacheDir(),"")+"cacheFile.srl"));
out.writeObject( jsonObject );
out.close();
// Load in an object
ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(new File(getCacheDir(),"")+"cacheFile.srl")));
JSONObject jsonObject = (JSONObject) in.readObject();
in.close();
try {
    new ReadWriteJsonFileUtils(context).createJsonFileData(file_name, data);
} catch (Exception e) {
    e.printStackTrace();
}
String jsonString = new ReadWriteJsonFileUtils(context).readJsonFileData(file_name);
检索:

// Instantiate a JSON object from the request response
JSONObject jsonObject = new JSONObject(json);
// Save the JSONOvject
ObjectOutput out = new ObjectOutputStream(new FileOutputStream(new File(getCacheDir(),"")+"cacheFile.srl"));
out.writeObject( jsonObject );
out.close();
// Load in an object
ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(new File(getCacheDir(),"")+"cacheFile.srl")));
JSONObject jsonObject = (JSONObject) in.readObject();
in.close();
try {
    new ReadWriteJsonFileUtils(context).createJsonFileData(file_name, data);
} catch (Exception e) {
    e.printStackTrace();
}
String jsonString = new ReadWriteJsonFileUtils(context).readJsonFileData(file_name);

我使用了内部存储,它将文件存储在应用程序包目录中,非根设备无法访问该目录

这里是一个可以创建、读取和删除文件的类

public class ReadWriteJsonFileUtils {
    Activity activity;
    Context context;

    public ReadWriteJsonFileUtils(Context context) {
        this.context = context;
    }

    public void createJsonFileData(String filename, String mJsonResponse) {
        try {
            File checkFile = new File(context.getApplicationInfo().dataDir + "/new_directory_name/");
            if (!checkFile.exists()) {
                checkFile.mkdir();
            }
            FileWriter file = new FileWriter(checkFile.getAbsolutePath() + "/" + filename);
            file.write(mJsonResponse);
            file.flush();
            file.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String readJsonFileData(String filename) {
        try {
            File f = new File(context.getApplicationInfo().dataDir + "/new_directory_name/" + filename);
            if (!f.exists()) {
                onNoResult();
                return null;
            }
            FileInputStream is = new FileInputStream(f);
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            return new String(buffer);
        } catch (IOException e) {
            e.printStackTrace();
        }
        onNoResult();
        return null;
    }

    public void deleteFile() {
        File f = new File(context.getApplicationInfo().dataDir + "/new_directory_name/");
        File[] files = f.listFiles();
        for (File fInDir : files) {
            fInDir.delete();
        }
    }

    public void deleteFile(String fileName) {
        File f = new File(context.getApplicationInfo().dataDir + "/new_directory_name/" + fileName);
        if (f.exists()) {
            f.delete();
        }
    }
}
您可以通过调用ReadWriteJsonFileUtils类方法创建、读取和删除文件,如下所示:

用于创建文件:

// Instantiate a JSON object from the request response
JSONObject jsonObject = new JSONObject(json);
// Save the JSONOvject
ObjectOutput out = new ObjectOutputStream(new FileOutputStream(new File(getCacheDir(),"")+"cacheFile.srl"));
out.writeObject( jsonObject );
out.close();
// Load in an object
ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(new File(getCacheDir(),"")+"cacheFile.srl")));
JSONObject jsonObject = (JSONObject) in.readObject();
in.close();
try {
    new ReadWriteJsonFileUtils(context).createJsonFileData(file_name, data);
} catch (Exception e) {
    e.printStackTrace();
}
String jsonString = new ReadWriteJsonFileUtils(context).readJsonFileData(file_name);
用于读取文件:

// Instantiate a JSON object from the request response
JSONObject jsonObject = new JSONObject(json);
// Save the JSONOvject
ObjectOutput out = new ObjectOutputStream(new FileOutputStream(new File(getCacheDir(),"")+"cacheFile.srl"));
out.writeObject( jsonObject );
out.close();
// Load in an object
ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(new File(getCacheDir(),"")+"cacheFile.srl")));
JSONObject jsonObject = (JSONObject) in.readObject();
in.close();
try {
    new ReadWriteJsonFileUtils(context).createJsonFileData(file_name, data);
} catch (Exception e) {
    e.printStackTrace();
}
String jsonString = new ReadWriteJsonFileUtils(context).readJsonFileData(file_name);
用于删除单个文件

new ReadWriteJsonFileUtils(context).deleteFile(file_name);
new ReadWriteJsonFileUtils(context).deleteFile();
用于删除所有文件

new ReadWriteJsonFileUtils(context).deleteFile(file_name);
new ReadWriteJsonFileUtils(context).deleteFile();

配置数据的SharedReferences和海量数据的SQLite。如果不了解有关用例的更多详细信息,这是不可能回答的。即便如此,在某种程度上,这也是一个意见问题。假设我有一些数据(即城市列表),我希望我的应用程序存储此json以备以后在internet连接不可用时使用。@fish40我必须开发相同类型的程序,您使用了什么方法(sqlite或文件缓存)你能分享你的github演示代码链接吗?我一直在想适配器和内容提供者我如何发送通知?内容提供者应该调用context.getContentResolver().notifyChange(,null);当数据已更改时。然后,讨论中的活动将使用ContentObserver通知这些更改。这取决于存储数据的敏感性。请注意,存储在sqite数据库或共享首选项上可能会有问题,只需一部根手机即可访问整个数据库谢谢您的支持@robin.“只比文件缓存慢几毫秒”是的,但请记住,您不需要json解析,它只被解析一次。@Cristiana214的方法与删除jsonobject实例的方法相同,只需将字符串写入缓存即可。检索时只检索字符串而不是JsonObject。为什么在检索时创建了两次文件类?如果JsonObject是JSONArray,从数组Json检索,我应该如何处理它?