在Android Studio中使用Volley解析ArrayJson时出错

时间:2018-02-05 13:43:16

标签: android json rest parsing android-volley

我也是Android的JSON解析新手,我尝试了在Internet上找到的教程以及如何解析UnNamed数组这是我的Json数组(https://webservicesocs.herokuapp.com/api/DispoMachines)我要解析并显示它在ListView中的Android中 这是我使用Volley Library的Android代码:

 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);

**我收到了BAD REQUEST错误,因此Volley Library **错误响应**

2 个答案:

答案 0 :(得分:0)

检查Description Invoice Forecast vs Forecast USA 300 325 92% East 150 175 86% Product Grouping 1 125 125 100% Product 1 50 75 67% Product 2 75 50 150% Product Grouping 3 25 50 50% Product 3 25 50 50% West 150 150 100% Product Grouping 1 75 100 75% Product 1 25 50 50% Product 2 50 50 100% Product Grouping 3 75 50 150% Product 3 75 50 150% 中是否有internet permission并使用manifest代替getString("Staut") ...它将通过 NPException

答案 1 :(得分:0)

您的json响应是从JSONArray开始的,您可以将您的请求更改为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);