如何将最近2个月的数据存储在本地chache(不在本地数据库中)的日期愿望中

发布于 2025-02-01 12:58:14 字数 408 浏览 4 评论 0原文

需要将过去七个月的数据存储在本地(不在本地数据库中)。首先我需要先存储 两个月的日期明智的数据在本地。之后,如果数据超过两个月,则需要再次致电 API和存储接下来两个月的当地日期数据,再次持续两个月,最后1个 月份是seveb月份的一个月。 与(2+2+2+1)相同。

这是以下响应:

[
    {
        "value": 3.0,
        "date": "16032022"
    },
    {
        "value": 50.0,
        "date": "17032022"
    }
] 

将数据存储在本地之后,我需要从本地缓存(不是数据库)每周获取数据,这意味着已经通过了一周的开始日期和一周的终点。工作方式应该是什么?

如何在本地和从那里获取每周存储?

Need to store past SEVEN months data in local(not in local database). First I need to store first
TWO months date wise data in local. After that if TWO months data exceeds then again need to call
api and store next TWO months data in local date wise and again for TWO months and lastly for 1
month as it is last SEVEB month data.
Same as (2+2+2+1).

this is the response below:

[
    {
        "value": 3.0,
        "date": "16032022"
    },
    {
        "value": 50.0,
        "date": "17032022"
    }
] 

After storing data in local, I need to get the data from local cache (not database) weekly basis, that means have pass the startDate of week and endDate of week. what should be the way to work?

How to store locally and from fetch there weekly basis?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱*していゐ 2025-02-08 12:58:14

要将此信息保存在本地缓存中,您可以使用共享首选项这可以节省相对较小的键值集合。
文档在这里

如何保存数据:

//Config your Shared Precerence
String sharedPreferenceName = "com.example.myapp.PREFERENCE_FILE_KEY"
Context context = getActivity();
SharedPreferences sharedPref =
context.getSharedPreferences(sharedPreferenceName, Context.MODE_PRIVATE);

//Save data
String preferenceKey = "data_id";
String defaultValue = "";
String jsonData = "[\n" +
            "    {\n" +
            "        \"value\": 3.0,\n" +
            "        \"date\": \"160/32/022\"\n" +
            "    },\n" +
            "    {\n" +
            "        \"value\": 50.0,\n" +
            "        \"date\": \"17/03/2022\"\n" +
            "    }\n" +
            "]";
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(preferenceKey, jsonData);
editor.apply();

//get data
String data = sharedPref.getString(preferenceKey, defaultValue);
Gson gson = new Gson();
JsonArray jsonArray = gson.fromJson(data, JsonArray.class);

//Here iterate the array and validate the date
for (int i = 0; i < jsonArray.size(); i++) {
String dateString = jsonArray.get(i).getAsJsonObject().get("date").getAsString();
Date date = new SimpleDateFormat("dd/MM/yyyy").parse(dateString);
Date currentDate = new Date();
//to this (get days elapsed) use joda-time-2.5
// https://mvnrepository.com/artifact/joda-time/joda-time
//implementation group: 'joda-time', name: 'joda-time', version: '2.10.14'
int daysElapsed = Days.daysBetween(
        new LocalDate(date.getTime()),
        new LocalDate(currentDate.getTime())).getDays();
        
if (daysElapsed > 60) {
    //the data exceeds TWO months
    //Call the API
} else {
    //do nothing
}
}

To save this info in local cache you can use Shared Preferences this can save relatively small collection of key-values.
The documentation Here

Example how save the data:

//Config your Shared Precerence
String sharedPreferenceName = "com.example.myapp.PREFERENCE_FILE_KEY"
Context context = getActivity();
SharedPreferences sharedPref =
context.getSharedPreferences(sharedPreferenceName, Context.MODE_PRIVATE);

//Save data
String preferenceKey = "data_id";
String defaultValue = "";
String jsonData = "[\n" +
            "    {\n" +
            "        \"value\": 3.0,\n" +
            "        \"date\": \"160/32/022\"\n" +
            "    },\n" +
            "    {\n" +
            "        \"value\": 50.0,\n" +
            "        \"date\": \"17/03/2022\"\n" +
            "    }\n" +
            "]";
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(preferenceKey, jsonData);
editor.apply();

//get data
String data = sharedPref.getString(preferenceKey, defaultValue);
Gson gson = new Gson();
JsonArray jsonArray = gson.fromJson(data, JsonArray.class);

//Here iterate the array and validate the date
for (int i = 0; i < jsonArray.size(); i++) {
String dateString = jsonArray.get(i).getAsJsonObject().get("date").getAsString();
Date date = new SimpleDateFormat("dd/MM/yyyy").parse(dateString);
Date currentDate = new Date();
//to this (get days elapsed) use joda-time-2.5
// https://mvnrepository.com/artifact/joda-time/joda-time
//implementation group: 'joda-time', name: 'joda-time', version: '2.10.14'
int daysElapsed = Days.daysBetween(
        new LocalDate(date.getTime()),
        new LocalDate(currentDate.getTime())).getDays();
        
if (daysElapsed > 60) {
    //the data exceeds TWO months
    //Call the API
} else {
    //do nothing
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文