【发布时间】:2020-06-27 09:22:06
【问题描述】:
图像包含没有数组名称的数据,我需要使用 volley 获取图像中的数据。帮我提供它的代码。
【问题讨论】:
标签: java android json android-studio mobile-application
图像包含没有数组名称的数据,我需要使用 volley 获取图像中的数据。帮我提供它的代码。
【问题讨论】:
标签: java android json android-studio mobile-application
您必须使用 JSONArrayRequest 。当您解析响应时,它是一个可以使用的 JSONArray 对象。您可以循环数组以按位置获取 JSONObject 项。
这已在此线程中介绍-> Volley - Sending a POST request using JSONArrayRequest
【讨论】:
String url="https://earthquake.usgs.gov/archive/product/nearby-cities/ci39269503/ci/1593242325224/nearby-cities.json"; JsonArrayRequest jsonArrayRequest=new JsonArrayRequest(Request.Method.GET, url, new Response.Listener() { @覆盖 公共无效 onResponse(JSONArray 响应){ StringBuilder stringBuilder=new StringBuilder();
try {
for (int i=0;i<response.length();i++){
JSONObject citiesjsonObject=response.getJSONObject(i);
stringBuilder.append("Name : "+citiesjsonObject.getString("name")+
"\n"+"Distance : "+citiesjsonObject.getString("distance")+ "\n"
+"Population : "+citiesjsonObject.getString("population"));
stringBuilder.append("\n\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(jsonArrayRequest);
【讨论】: