1

Buen dia, tengo un problema al momento de parsear un Json en respuesta de un php, cuando lo quiero intentar con un JsonArrayRequest, me sale el error

D/ContentValues: ERROR RESPUESTA en JSON: org.json.JSONException: Value [] of type org.json.JSONArray cannot be converted to JSONObject

en este tengo que enviar unos parametros, con los cuales no los envia con el metodo anterior mencionado, sin embargo, lo que estoy intentando ahora, es hacer la solicitud con StringRequest, que en realidad obtengo el mismo error, pero en lugar de los corchetes vacios, recibo el json completo, aqui el codigo que me envia el json completo:

public class MainLista extends AppCompatActivity {

    TextView txt1;
    ListView listaPerfil;
    ArrayAdapter adapter;
    String LINK = "*********/ejemplomostrar.php";
    private List<Datos> items;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_lista);

        listaPerfil = findViewById(R.id.listaPerfil);
        txt1 = findViewById(R.id.txt1);

        adapter = new AdaptadorDatos(this);
        listaPerfil.setAdapter(adapter);

        llamadoJson();

    }

    public void llamadoJson() {

        StringRequest sr = new StringRequest(Request.Method.POST, LINK,
                response -> {
            txt1.setText(response);
                    Log.d("RESPONSE:", response);
                    try {
                        JSONObject jsonObject = new JSONObject(response); //Aqui me marca el error
                        String success = jsonObject.getString("");
                        JSONArray jsonArray = jsonObject.getJSONArray("");

                        if (success.equals("1")) {
                            for (int i = 0; i <jsonArray.length(); i++) {
                                JSONObject o = jsonArray.getJSONObject(i);
                                String nombre = o.getString("nombre");
                                Datos datos = new Datos(nombre);
                                items.add(datos);
                            }
                            adapter = new AdaptadorDatos(getApplicationContext());
                            listaPerfil.setAdapter(adapter);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(), "error json", Toast.LENGTH_LONG);
                    }
                }, error -> {
            Toast.makeText(MainLista.this, "ERROR DE CONEXION", Toast.LENGTH_LONG).show();
        }) {

            @Override
            protected Map<String, String> getParams() {

                Map<String, String> params = new Hashtable<>();
                params.put("email", Globales.USER_EMAIL);
                params.put("contrasena", Globales.USER_PASS);
                return params;
            }
        };
        RequestQueue rq = Volley.newRequestQueue(this);
        rq.add(sr);
    }
}

El Json que quiero parsear:

[{"nombre":"Ricardo","apellido":"Hern\u00e1ndez","telefono1":"8672557050","email":"[email protected]","fechanacimiento":"0000-00-00","contrasena":"123456789","imagen":"*******uploads\/usuario.jpg"}]

Como podria hacerle para que parsee el json correctamente, espero puedan ayudarme

1 respuesta 1

0

El problema es que la respuesta no es un JSONObject, es un JSONArray, revisa esta respuesta:

Parser Json volley request

debes obtener el JsonArray y posteriormente el objeto, realiza el cambio:

    try {
        //JSONObject jsonObject = new JSONObject(response); //Aqui me marca el error
       // String success = jsonObject.getString("");
        JSONArray jsonArray = new JSONArray(response);

        //if (success.equals("1")) {
            for (int i = 0; i <jsonArray.length(); i++) {
                JSONObject o = jsonArray.getJSONObject(i);
                String nombre = o.getString("nombre");
                Datos datos = new Datos(nombre);
                items.add(datos);
            }
            adapter = new AdaptadorDatos(getApplicationContext());
            listaPerfil.setAdapter(adapter);
        //}
    } catch (JSONException e) {
        e.printStackTrace();
        Toast.makeText(getApplicationContext(), "error json", Toast.LENGTH_LONG);
    }
5
  • Obtengo esto en el Logcat: D/ContentValues: ERROR RESPUESTA en JSON: org.json.JSONException: Value [] of type org.json.JSONArray cannot be converted to JSONObject el 9 mar. 2021 a las 2:59
  • Cual es el valor de response que obtienes inicialmente?
    – Jorgesys
    el 9 mar. 2021 a las 11:41
  • cuando lo pongo en el textview me da el arreglo Json el 9 mar. 2021 a las 16:05
  • 1
    Ya me funciono, muchas gracias el 9 mar. 2021 a las 17:09
  • Excelente @RicardoVillanueva , saludos
    – Jorgesys
    el 9 mar. 2021 a las 17:14

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.