Android Studio中Java读取XML的images字段URL失败求助
我在Android Studio开发应用,能从返回的JSON中提取ean、title、upc等字段,但获取images字段里的URL时遇到问题。尝试修改代码后触发了IndexOutOfBoundsException错误,附上两段代码和报错信息,求解决方法。
原正常运行代码
public void onResponse(String response) { // Display response string. //Log.d("response", response); output =""; try { JSONObject jsonresponse = new JSONObject(response); JSONArray jsonarray = jsonresponse.getJSONArray("items"); JSONObject jsonobject = jsonarray.getJSONObject(0); String name = jsonobject.getString("title"); Toast.makeText(context, "Description"+ name, Toast.LENGTH_SHORT).show(); output = name; //Toast.makeText(context, "Imagen:"+output, Toast.LENGTH_SHORT).show(); volleyResponseListener.onResponse(output); } catch (JSONException e) { e.printStackTrace(); } }
尝试的报错代码
public void onResponse(String response) { // Display response string. //Log.d("response", response); try { JSONObject jsonresponse = new JSONObject(response); JSONArray jsonarray = jsonresponse.getJSONArray("items"); JSONObject jsonobject = jsonarray.getJSONObject(0); JSONArray imghttp = jsonobject.getJSONArray("images"); JSONObject imgobject = imghttp.getJSONObject(0); String httpimage = imgobject.getString("images"); Toast.makeText(context, "Articulo "+ httpimage, Toast.LENGTH_SHORT).show(); volleyResponseListener.onResponse(output); } catch (JSONException e) { e.printStackTrace(); } }
报错信息
2022-10-28 13:18:38.006 9846-9846/com.example.weatherapp W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7898)
2022-10-28 13:18:38.006 9846-9846/com.example.weatherapp W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2022-10-28 13:18:38.006 9846-9846/com.example.weatherapp W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
2022-10-28 13:18:38.006 9846-9846/com.example.weatherapp W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
2022-10-28 13:18:38.007 9846-9846/com.example.weatherapp W/System.err: Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
2022-10-28 13:18:38.007 9846-9846/com.example.weatherapp W/System.err: at java.util.ArrayList.get(ArrayList.java:437)
2022-10-28 13:18:38.007 9846-9846/com.example.weatherapp W/System.err: at org.json.JSONArray.get(JSONArray.java:289)
解决方案
1. 先确认JSON结构是否匹配
报错核心是images数组为空(Size:0),但你直接去取第0个元素导致索引越界。先打印完整的接口返回日志,确认items[0].images的真实结构:
Log.d("JSON_DEBUG", response);
如果日志显示images是空数组,要排查接口返回是否正常;如果是字段名错误(比如实际是image单数),要对应调整代码里的字段名。
2. 增加空值判断,避免崩溃
不管接口返回是否有数据,都要加判断逻辑防止崩溃,同时修正可能的字段名错误:
public void onResponse(String response) { try { JSONObject jsonresponse = new JSONObject(response); JSONArray jsonarray = jsonresponse.getJSONArray("items"); if (jsonarray.length() > 0) { JSONObject jsonobject = jsonarray.getJSONObject(0); // 先判断images字段是否存在 if (jsonobject.has("images")) { JSONArray imghttp = jsonobject.getJSONArray("images"); // 判断数组是否有元素 if (imghttp.length() > 0) { JSONObject imgobject = imghttp.getJSONObject(0); // 注意:这里字段名大概率不是"images",换成实际的URL字段名比如"url" String httpimage = imgobject.optString("url", "无图片"); Toast.makeText(context, "图片地址: "+ httpimage, Toast.LENGTH_SHORT).show(); volleyResponseListener.onResponse(httpimage); } else { Toast.makeText(context, "暂无图片数据", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(context, "未找到图片字段", Toast.LENGTH_SHORT).show(); } } } catch (JSONException e) { e.printStackTrace(); Toast.makeText(context, "JSON解析失败", Toast.LENGTH_SHORT).show(); } }
3. 修正字段名错误
你的报错代码中imgobject.getString("images")明显不合理——图片数组的子元素里不会再叫images,通常是url、image_url这类字段,这也是解析失败的潜在原因,必须根据真实的JSON结构调整。
内容的提问来源于stack exchange,提问作者hbtousa

