-1

This is what I have tried: The database is in my localhost and I am using my own IP.

StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    try{
                    JSONArray array = new JSONArray(response);

                        for (int x = 0; x<array.length();x++){

                            JSONObject ob = array.getJSONObject(x);

                            Alumnos list = new Alumnos();
                            list.setUsername(ob.getString("username"));
                            list.setJob(ob.getString("job"));
                            list.setAge(ob.getInt("age"));

                            alumnos.add(list);

                        }

                        updateRecycler(alumnos);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
            }


            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.i("Error: ", error.toString());
        }
    }

    );
    queue.add(stringRequest);
}

I try to show my results in a Recycler view but I get the next error:

10-03 21:41:28.033 10077-10077/? W/System.err: org.json.JSONException: Value {"result":[{"userid":"1","username":"Luis","job":"developer","age":"23"},{"userid":"2","username":"Alejandro","job":"agronomo","age":"24"},{"userid":"3","username":"Gabriel","job":"Redes","age":"24"}]} of type org.json.JSONObject cannot be converted to JSONArray

I think I might be missing something veri obvious because I get the Array in the Logcat.

Luis Aguilar
  • 645
  • 9
  • 26

1 Answers1

2
  1. You are receiving a JSONObject ({})
  2. Your data is inside a JSONArray ([]) i.e. result so use

JSONObject obj = new JSONObject(response);
JSONArray array = obj.getJSONArray("result");
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68