How to Cache Json data to be available offline?

后端 未结 7 2355
隐瞒了意图╮
隐瞒了意图╮ 2020-12-08 12:55

I have parsed the JSON Data in a listview and now I want to make it available offline. Is there a way to save the JSON data at the phone so that you can see th

7条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-08 13:16

    You can use those two methods two store you JSON file as a string in your SharedPreferences and retrieve it back:

    public String getStringProperty(String key) {
        sharedPreferences = context.getSharedPreferences("preferences", Activity.MODE_PRIVATE);
        String res = null;
        if (sharedPreferences != null) {
            res = sharedPreferences.getString(key, null);
        }
        return res;
    }
    
    public void setStringProperty(String key, String value) {
        sharedPreferences = context.getSharedPreferences("preferences", Activity.MODE_PRIVATE);
        if (sharedPreferences != null) {
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString(key, value);
            editor.commit();
            CupsLog.i(TAG, "Set " + key + " property = " + value);
        }
    }
    

    Just use setStringProperty("json", "yourJsonString") to save and getStringProperty("json") to retrieve.

提交回复
热议问题