Bad Request while parsing ArrayJson using Volley i

2019-08-23 10:45发布

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 **

2条回答
放荡不羁爱自由
2楼-- · 2019-08-23 11:17

your json response is start with JSONArray you can change your request to JSONArray.

 String ServiceURL = "https://webservicesocs.herokuapp.com/api/DispoMachines";
    machinesListVeiw = (ListView) findViewById(R.id.machines_lv);
    final String result = "";
    final ArrayList<Machine> machines = new ArrayList<>();

JsonArrayRequest req = new JsonArrayRequest(urlJsonArry,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());

                    try 
                    {
                        // Parsing json array response
                        // loop through each json object

              for(int i=0;i<response.length();i++)
              {
                  JSONObject e= (JSONObject) response.get(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) {
                        e.printStackTrace();

                    }

                    hidepDialog();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());

                }
            });

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(req);
查看更多
Evening l夕情丶
3楼-- · 2019-08-23 11:30

Check if you have internet permission in the manifest and use getString("Staut") instead of getString("Statut")... it will through NPException

查看更多
登录 后发表回答