使用 HTTP 请求获取最后一个 JSON 数组元素
Get last JSON array element with HTTP request
是否可以获取 GitHub 上托管的 JSON 数组文件的最后一个/倒数第二个元素,而无需下载整个文件?
因为文件有 10 MB,我只需要数组的最后两个单元格,实际上每次我去获取信息时,由于文件的重量,加载时间很长。
文件link: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);
据我所知,没有办法“跳转”到呼叫响应的末尾。
不过,我会这样处理:
如果数据不断变化:
- 以固定的时间间隔接收数据,而不是每次我需要它的时候。
- 获取数据并将其保存在某处(通过
Object
或在临时位置缓存您需要的文件部分)。
如果数据没有变化
- 在应用程序启动时获取数据运行并使用
Object
来保留所需的数据
是否可以获取 GitHub 上托管的 JSON 数组文件的最后一个/倒数第二个元素,而无需下载整个文件?
因为文件有 10 MB,我只需要数组的最后两个单元格,实际上每次我去获取信息时,由于文件的重量,加载时间很长。
文件link: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);
据我所知,没有办法“跳转”到呼叫响应的末尾。
不过,我会这样处理:
如果数据不断变化:
- 以固定的时间间隔接收数据,而不是每次我需要它的时候。
- 获取数据并将其保存在某处(通过
Object
或在临时位置缓存您需要的文件部分)。
如果数据没有变化
- 在应用程序启动时获取数据运行并使用
Object
来保留所需的数据