Get last JSON array element with HTTP request

Viewed 89

Is it possible to take the last / penultimate element of a JSON Array file hosted on GitHub, without downloading the entire file?

Because the file is 10 MB, I only need the last two cells of the array, in fact every time I go to get the information it takes a lot of time to load due to the weight of the file.

File link: https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-json/dpc-covid19-ita-regioni.json

I retrive the information with this code:

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);
1 Answers

As far as I know, there is no way to "jump" to the end of the call response.

Still, I would approach this, this way:

If the data changes constantly:

  • Recieve the data at a fixed interval and not every time I need it.
  • Get the data and save it somewhere (through an Object or cache the part of the file that you need in a temporary place).

If the data does not change

  • Fetch the data when the application starts running and use an Object to keep the data needed
Related