Hello everyone. I have an XML file as shown below. Successfully, I was able to retrieve the title, the description and so on. I am having trouble trying to extract the jpg file off the images field.
With the code below, I'm getting an error. Please, if anyone can point me in the right direction, I will be highly glad and thankful.
"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)"
With the code below, I'm getting an error. Please, if anyone can point me in the right direction, I will be highly glad and thankful.
"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)"
Code:
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);
JSONArray imghttp = jsonobject.getJSONArray("images");
JSONObject imgobject = imghttp.getJSONObject(0);
String httpimage = imgobject.getString("images");
//JSONArray jsonArray = jsonresponse.getJSONArray("images");
// JSONObject imageobject = imagerray.getJSONObject(0);
String name = jsonobject.getString("title");
//JSONArray imagearray = jsonarray.getJSONArray(0);
Toast.makeText(context, "Articulo "+ httpimage, Toast.LENGTH_SHORT).show();
output = name;
//Toast.makeText(context, "Imagen:"+output, Toast.LENGTH_SHORT).show();
volleyResponseListener.onResponse(output);
} catch (JSONException e) {
e.printStackTrace();
}
}