I'm new in JSON parsing also for Android, I tried a tutorial found in Internet and how to parse an UnNamed Array this is my Json Array (https://webservicesocs.herokuapp.com/api/DispoMachines) that i want to parse and display it in Android in ListView And this is my code in Android using Volley Library :
String ServiceURL = "https://webservicesocs.herokuapp.com/api/DispoMachines";
machinesListVeiw = (ListView) findViewById(R.id.machines_lv);
final String result = "";
final ArrayList<Machine> machines = new ArrayList<>();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, ServiceURL, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray json = new JSONArray(result);
for(int i=0;i<json.length();i++){
JSONObject e = json.getJSONObject(i);
Machine machine = new Machine();
machine.setMachineId(e.getString("MachineId"));
machine.setStatut(e.getString("Statut"));
machine.setMachineImage(e.getString("MachineImage"));
machine.setTempsResteEnMinutes(e.getString("TempsResteEnMinutes"));
machines.add(machine);
}
MachineAdapter arrayAdapter = new MachineAdapter(AvaiableMachinesActivity.this, machines);
machinesListVeiw.setAdapter(arrayAdapter);
} catch (JSONException e) {
Toast.makeText(AvaiableMachinesActivity.this, "BAD JSON ", Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(AvaiableMachinesActivity.this, "BAD REQUEST", Toast.LENGTH_SHORT).show();
}
});
AppController.getInstance(AvaiableMachinesActivity.this).addToRequestQueue(jsonObjReq);
**I get the error of BAD REQUEST, so it's an ErrorResponse due to Volley Library **
your json response is start with JSONArray you can change your request to JSONArray.
Check if you have
internet permission
in themanifest
and usegetString("Staut")
instead ofgetString("Statut")
... it will through NPException