【问题标题】:Get last JSON array element with HTTP request使用 HTTP 请求获取最后一个 JSON 数组元素
【发布时间】:2021-10-22 13:42:50
【问题描述】:

是否可以获取托管在 GitHub 上的 JSON 数组文件的最后一个/倒数第二个元素,而无需下载整个文件?

因为文件是 10 MB,所以我只需要数组的最后两个单元格,实际上每次去获取信息时,由于文件的重量,加载需要很长时间。

文件链接:https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-json/dpc-covid19-ita-regioni.json

我使用此代码检索信息:

RequestQueue queue = Volley.newRequestQueue(getApplicationContext());

    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, JSON_OLD, null, response -> {
        try{
            for(int i = response.length() - 42; i < response.length() - 21; i++){
                JSONObject jsonObject = response.getJSONObject(i);
                oldRegionData.add(new RegionData(Integer.parseInt(jsonObject.getString("dimessi_guariti")),Integer.parseInt(jsonObject.getString("deceduti")), jsonObject.getString("denominazione_regione"), Integer.parseInt(jsonObject.getString("nuovi_positivi"))));
            }
            getNewData(queue);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }, error -> Log.println(Log.ERROR,"Error", "Error while performing this action"));
    queue.add(jsonArrayRequest);

【问题讨论】:

    标签: java android json http request


    【解决方案1】:

    据我所知,没有办法“跳转”到呼叫响应的末尾。

    不过,我还是会这样处理:

    如果数据不断变化:

    • 以固定的时间间隔接收数据,而不是每次需要时。
    • 获取数据并将其保存在某处(通过Object 或将您需要的文件部分缓存在临时位置)。

    如果数据没有变化

    • 在应用程序开始运行时获取数据并使用Object 保留所需的数据

    【讨论】:

      猜你喜欢
      • 2016-09-29
      • 1970-01-01
      • 2020-06-15
      • 2017-08-27
      • 1970-01-01
      • 1970-01-01
      • 2022-09-29
      • 2017-11-08
      • 2022-06-11
      相关资源
      最近更新 更多