【问题标题】:getting new data from a json file using volley使用 volley 从 json 文件中获取新数据
【发布时间】:2021-04-08 21:19:17
【问题描述】:

我有一个 android java 应用程序,我需要一些来自 json 文件的数据,所以我使用 volley 从 json 获取数据 我的问题是,当我更改 json 文件上的数据时,我应该卸载应用程序并重新安装,然后获取新数据,否则即使从 ram 中删除旧数据,它也会保留旧数据

   RequestQueue requestQueue = Volley.newRequestQueue(this);

jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, LINK, null, new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {


        try {

            JSONArray jsonArrayVariabls = response.getJSONArray("variables");
            JSONObject var = jsonArrayVariabls.getJSONObject(0);
            Variables.name = var.getString("name");
            Variables.adresse=var.getString("adresse");
            
                     callBack.onSuccess();

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {

    }
});

jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

requestQueue.add(jsonObjectRequest);

【问题讨论】:

  • 因为你没有使用实时数据,所以实时数据 mvvm 架构会更新它
  • 我将数据存储在闪屏的静态变量中,所以只有在我打开应用程序时才需要来自 json 的数据
  • 而不是静态变量。从应用程序类访问
  • 您的意思是创建一个将数据存储在列表中的类并使用DAO?
  • 但这里的问题是,当我重新打开应用程序时,上面的代码正在执行,但即使在记录 var.getString("name") 时我仍然得到旧值,我得到旧值数据

标签: android android-volley


【解决方案1】:

问题来自缓存,因此您需要在使用此行请求之前清除它

        requestQueue.getCache().clear();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    • 1970-01-01
    • 2020-05-11
    相关资源
    最近更新 更多