【发布时间】: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