The best way to caching json

2019-01-30 20:20发布

问题:

My application should work not only in online but also in offline mode. For that reason I am considering find the best way for cashing data. I't like use SharedPreference for store data but in android documentation writen Maximum size in characters allowed for a preferences value is 8192. I don't know this is ok or not? I tried to pass out of this idea trying to use FileCashing or sqLite cashing.

So what you think guys what is the best SharedPreference vs FileCashing or vs SqLiteCaching?

回答1:

I personally like to do this the following way. Create a SQLite database that can hold your content. Then, bind the user interface directly to the database using Adapters & Content Providers that send a notification whenever the data is changed so that the UI can update itself. The last piece in the equation is some form of synchronization service that downloads content and saves it to the database asynchronously. That way, you can manage your data very easily because it is all in the same place. The only part you'll have to figure out for your app is how you decide when to update or remove the data from the database.

Adapters

ContentProvider

Synchronization



回答2:

Save the json in cache directory as file....

Save:

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

Retrieve:

// Load in an object
ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(new File(getCacheDir(),"")+"cacheFile.srl")));
JSONObject jsonObject = (JSONObject) in.readObject();
in.close();


回答3:

Based on your requirement I would recommend SQLite data base.

Since shared preference is suitable for configuration storage - often small data/strings.

File cache is hard to manage, so I recommend SQLite - easy to manage and ability to store mass data.

Considering the performance, if the number of index is not that huge, SQLite database should have the acceptable performance. E.g. only several ms slower than a file cache.

You might be able to combine these two approaches together. Use random access file with index-offset stored in SQLite.



回答4:

I have used Internal Storage which store file in Application package directory that can't be accessible by not rooted device.

Here the class which can create, read and delete the file

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

You can create, read and delete the file by calling ReadWriteJsonFileUtils class methods as follows:

For creating file:

try {
    new ReadWriteJsonFileUtils(context).createJsonFileData(file_name, data);
} catch (Exception e) {
    e.printStackTrace();
}

For reading file:

String jsonString = new ReadWriteJsonFileUtils(context).readJsonFileData(file_name);

For deleting single file

new ReadWriteJsonFileUtils(context).deleteFile(file_name);

For deleting all file

new ReadWriteJsonFileUtils(context).deleteFile();