error JSONException error No value for

1

This is my code

               String line;
               while((line = reader.readLine()) != null){
                   result.append(line);
               }
               JSONObject respuestaJSON = new JSONObject(result.toString());
               JSONObject usuario = respuestaJSON.getJSONObject("usuario");
               String resultJSON = respuestaJSON.getString("estado");

               if(resultJSON == "1"){
                   id = id + usuario.getString("id");
                   nombre = nombre + usuario.getString("nombre");
                   ap = ap + usuario.getString("ap");
                   am = am + usuario.getString("am");
                   correo = correo+ usuario.getString("correo");
                   direccion = direccion + usuario.getString("direccion");
                   numero = numero + usuario.getString("numero");
                   dispositivo = dispositivo + usuario.getString("dispositivo");
                   contraseña = contraseña + usuario.getString("contrasena");

                   }

and this is the JSON result

{
    "estado": 1
} {
    "usuario": {
        "0": "hola",
        "id": "hola",
        "1": "Hector",
        "nombre": "Hector",
        "2": "asfaf",
        "ap": "asfaf",
        "3": "alala",
        "am": "alala",
        "4": "[email protected]",
        "correo": "[email protected]",
        "5": "23255532",
        "telefono": "23255532",
        "6": "infierno",
        "direccion": "infierno",
        "7": "holamundo",
        "contrasena": "holamundo",
        "8": "1",
        "dispositivo": "1"
    }
}
    
asked by Héctor Valdes 03.05.2017 в 05:54
source

1 answer

0

The error

  

JSONException No value for

is caused by not being able to obtain the value of a key inside the .json, surely in this the point:

String resultJSON = respuestaJSON.getString("estado");

The .json you add is not valid, I think it should be:

{
    "estado": 1,
    "usuario": {
        "0": "hola",
        "id": "hola",
        "1": "Hector",
        "nombre": "Hector",
        "2": "asfaf",
        "ap": "asfaf",
        "3": "alala",
        "am": "alala",
        "4": "[email protected]",
        "correo": "[email protected]",
        "5": "23255532",
        "telefono": "23255532",
        "6": "infierno",
        "direccion": "infierno",
        "7": "holamundo",
        "contrasena": "holamundo",
        "8": "1",
        "dispositivo": "1"
    }
}

this so that the code you submit gets the data correctly.

               //Convierte respuesta a JSONObject.   
               JSONObject respuestaJSON = new JSONObject(result.toString());
               //Obtiene valor JSONObject "usuario".
               JSONObject usuario = respuestaJSON.getJSONObject("usuario");
               //Obtiene valor de key estado en JSONObject.
               String resultJSON = respuestaJSON.getString("estado");

               //if(resultJSON == "1"){ //incorrecta comparación de Strings.
               if(resultJSON.equals("1")){
                   id = id + usuario.getString("id");
                   nombre = nombre + usuario.getString("nombre");
                   ap = ap + usuario.getString("ap");
                   am = am + usuario.getString("am");
                   correo = correo+ usuario.getString("correo");
                   direccion = direccion + usuario.getString("direccion");
                   numero = numero + usuario.getString("numero");
                   dispositivo = dispositivo + usuario.getString("dispositivo");
                   contraseña = contraseña + usuario.getString("contrasena");

                   }
    
answered by 04.05.2017 в 17:05