Problems loading data in a list

0

I find a medium rare problem, when loading data from a JSON in a list of items codigo , descripcion , precio and ubicacion in the list there are articles that have promotions and others do not.

When I want to fill the list with all the articles (with or without promotion) I do not fill in the list adapter, because the first article has no promotion, but I do load the articles without asking for promocion works.

My code:

  protected String doInBackground(String... strings) {
        prod = new ArrayList<>();
        lista_eligida = (ListView) findViewById(R.id.lista);

        String url = "http://danbijann.freeiz.com/tareas.json";
        HttpHandler sh = new HttpHandler();
        JSONObject jsonStr = sh.makeServiceCall(url);

        try {
            JSONObject jsonObj = new JSONObject(String.valueOf(jsonStr));
            Log.e("JSON", String.valueOf(jsonStr));
            JSONArray contacts = jsonObj.getJSONArray("PEDIDOS");
            for (int i = 0; i < contacts.length(); i++) {
                JSONObject c = contacts.getJSONObject(i);

                String codig = c.getString("codigo");
                String des = c.getString("descrip");
                String prec = c.getString("precio");
                String cantidad = c.getString("cantidad");
                String ubica = c.getString("ubica");
                String prueba = c.getString("prueba");
                String promocion = c.getString("promocion");
                String prueba2 = c.getString("prueba2");

                HashMap<String, String> contacto = new HashMap<>();
                contacto.put("codigo", codig);
                contacto.put("descrip", des);
                contacto.put("precio", prec);
                contacto.put("cantidad", cantidad);
                contacto.put("ubica", ubica);
                contacto.put("preuba", prueba);
                contacto.put("promocion", promocion);
                contacto.put("prueba2", prueba2);

                Log.d("ADAP", codig);
                Log.d("ADAP", des);
                Log.d("ADAP", prec);
                Log.d("ADAP", cantidad);
                Log.d("ADAP", ubica);

                prod.add(contacto);
            }
        } catch (final JSONException e) {
                e.getMessage();
        }
        return null;
    }
    protected void onPostExecute(String result) {
        if (pDialog.isShowing())
            pDialog.dismiss();
        super.onPostExecute(result);
        final ListAdapter adapter = new SimpleAdapter(Prueba.this, prod,
                R.layout.item_carrito, new String[]{ "codigo", "descrip", "precio", "cantidad","ubica","prueba","promocion","prueba2"},
                new int[]{R.id.txtcodigo, R.id.txtdescrip, R.id.txtprecio, R.id.cantidad, R.id.txtubica, R.id.txtprueba, R.id.txtpromo, R.id.txtprueba2});
        lista_eligida.setAdapter(adapter);
                registerForContextMenu(lista_eligida);

    }
    
asked by user62207 13.09.2018 в 20:28
source

2 answers

2

The problem is that when doing getString () of a non-existent field the execution fails and is caught by the catch then all the rest of the code inside the try does not execute and the Hasmap is never created. Whenever you read fields of a Json make sure you validate if they exist.

This way of validating is what I like the most:

String promocion = (json.has("promocion") && !objJson.isNull("promocion"))?
   c.getString("promocion"):"";

But the old and reliable:

String promocion = "";
if(objJson.has("promocion") && !objJson.isNull("promocion")) {
   promocion = c.getString("promocion");
}

One piece of advice, do not do it alone with the field that fails but with all the fields that you read from the Json to have a more robust code.

    
answered by 14.09.2018 / 03:32
source
1

It is because in your service you do not send the data of the promotion and you are wanting to obtain that data what you can do is to make use of the .has method of the Json, something like that should be.

protected String doInBackground(String... strings) {
        prod = new ArrayList<>();
        lista_eligida = (ListView) findViewById(R.id.lista);

        String url = "http://danbijann.freeiz.com/tareas.json";
        HttpHandler sh = new HttpHandler();
        JSONObject jsonStr = sh.makeServiceCall(url);

        try {
            JSONObject jsonObj = new JSONObject(String.valueOf(jsonStr));
            Log.e("JSON", String.valueOf(jsonStr));
            JSONArray contacts = jsonObj.getJSONArray("PEDIDOS");
            for (int i = 0; i < contacts.length(); i++) {
                JSONObject c = contacts.getJSONObject(i);

                String codig = c.getString("codigo");
                String des = c.getString("descrip");
                String prec = c.getString("precio");
                String cantidad = c.getString("cantidad");
                String ubica = c.getString("ubica");

                String prueba = "";
                if(objJson.has("prueba") && !objJson.isNull("prueba")){
                    prueba = c.getString("prueba");
                }

                String promocion = "";
                if(objJson.has("promocion") && !objJson.isNull("promocion")) {
                    promocion = c.getString("promocion");
                }

                String prueba2 = "";
                if(objJson.has("prueba2") && !objJson.isNull("prueba2")) {
                    prueba2 = c.getString("prueba2");    
                }


                HashMap<String, String> contacto = new HashMap<>();
                contacto.put("codigo", codig);
                contacto.put("descrip", des);
                contacto.put("precio", prec);
                contacto.put("cantidad", cantidad);
                contacto.put("ubica", ubica);
                contacto.put("preuba", prueba);
                contacto.put("promocion", promocion);
                contacto.put("prueba2", prueba2);

                Log.d("ADAP", codig);
                Log.d("ADAP", des);
                Log.d("ADAP", prec);
                Log.d("ADAP", cantidad);
                Log.d("ADAP", ubica);

                prod.add(contacto);
            }
        } catch (final JSONException e) {
            e.getMessage();
        }
        return null;
    }
    protected void onPostExecute(String result) {
        if (pDialog.isShowing())
            pDialog.dismiss();
        super.onPostExecute(result);
        final ListAdapter adapter = new SimpleAdapter(Prueba.this, prod,
                R.layout.item_carrito, new String[]{ "codigo", "descrip", "precio", "cantidad","ubica","prueba","promocion","prueba2"},
                new int[]{R.id.txtcodigo, R.id.txtdescrip, R.id.txtprecio, R.id.cantidad, R.id.txtubica, R.id.txtprueba, R.id.txtpromo, R.id.txtprueba2});
        lista_eligida.setAdapter(adapter);
        registerForContextMenu(lista_eligida);

    }

In this way you verify if the field exists and also if it is not in null

    
answered by 13.09.2018 в 21:39