ERROR: org.json.JSONException: Value ... of type java.lang.String can not be converted to JSONArray

4

I have this error:

  

org.json.JSONException: Value i201600010 of type java.lang.String   can not be converted to JSONArray

My code is as follows:

@Override
public void onClick(View view) {
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            final String resultado = enviarDatos(String.valueOf(asignatura.getSelectedItemPosition()+1),
                    String.valueOf(seccion.getSelectedItemPosition()+1),
                    String.valueOf(grupo.getSelectedItemPosition()+1));
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    int r = obtenerJSON(resultado);
                    if(r>0){
                        Toast.makeText(PreIngresoNotas.this, "CORRECTO", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(PreIngresoNotas.this, "Ha fallado", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    });
    thread.start();
}

public String enviarDatos(String cur, String sec, String gru){

    String linea = "";
    StringBuilder sb = null;
    int respuesta = 0;

    try{

        URL url = new URL("http://192.168.1.55/proyecto/listalumno.php?curso="+cur+"&seccion="+sec+"&grupo="+gru);

        HttpURLConnection conexion = (HttpURLConnection) url.openConnection();
        respuesta = conexion.getResponseCode();

        sb = new StringBuilder();

        if(respuesta==HttpURLConnection.HTTP_OK){

            InputStream inputStream = new BufferedInputStream(conexion.getInputStream());
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

            while ((linea = bufferedReader.readLine())!=null){
                    sb.append(linea + "\n");

            }
            inputStream.close();
        }

    }catch (Exception e){
        e.printStackTrace();
    }
    return sb.toString();
}


public int obtenerJSON(String response){
    int res = 0;

    try {
        JSONArray array = new JSONArray(response);
        if(array.length()>0){
            res = 1;
        }

        /*for (int i=0;i<array.length();i++){
            usuario.setText(array.getJSONObject(i).getString("usuario"));
            nombre.setText(array.getJSONObject(i).getString("nombre"));
            apellido.setText(array.getJSONObject(i).getString("apellido"));
        }*/

    }catch (Exception e){
        e.printStackTrace();
      }
        return res;
    }
}

The error obviously takes me to the ObtenerJSON method.

In the part of the thread, I do not know if I'm doing the following correctly, if it's wrong this part would appreciate correction, for example in

asignatura.getSelectedItemPosition()+1

the variable asignatura is a spinner, and I'm putting the getSelectedItemPosition() to get the position of the spinner element at the time of clicking the button, and I increase it 1 since the elements capture it from 0.

It will be better understood in the following image that my PHP bounces me. Although I guess it's fine because in my error it shows me the "i201600010" whose value is the field of a table in my MySQL.

With this image I want to let you know that I increase 1 to getSelectedItemPosition so you can take the value of 1, 2, 3 ....

This is the code in PHP:

<?php

 $conexion = mysqli_connect("localhost","root","mysql","android");

 $curso = mysqli_real_escape_string($conexion,$_REQUEST["curso"]);
 $seccion = mysqli_real_escape_string($conexion,$_REQUEST["seccion"]);
 $grupo = mysqli_real_escape_string($conexion,$_REQUEST["grupo"]);



 $query = mysqli_query($conexion,"select * from alumno where idcurso =  '$curso' and idsec = '$seccion' and idgru = '$grupo'") or  die(mysqli_error($conexion));



 while($fila = mysqli_fetch_array($query, MYSQLI_ASSOC)){       
    echo $fila["usuario"].",".$fila["nombre"].",".$fila["apellido"]."<br/>";    
  }

 ?>
    
asked by Jorge Requez 24.11.2016 в 07:03
source

2 answers

2

You have to analyze the answer, which you try to convert to JsonArray:

  JSONArray array = new JSONArray(response);

Review the answer and depending on that remember that the answer .Json can be of two types:

  

- If the .json starts with { it is considered as a Json object.

     

- If the .json starts with [ it is considered as a Json fix.

Because of the error apparently obtained, the response (response) is not really a JSONArray .

    
answered by 24.11.2016 / 16:12
source
0

I already found the solution, I added this to my php.

Thanks for the help.

while($fila = mysqli_fetch_array($query, MYSQLI_ASSOC)){        

$idalu = $fila["idalu"];
$usuario = $fila["usuario"];
$contra = $fila["contra"];
$nombre = $fila["nombre"];
$apellido = $fila["apellido"];
$idcurso = $fila["idcurso"];
$idsec = $fila["idsec"];    
$idgru = $fila["idgru"];


$alumnos[] = array("id" => $idalu, "usuario" => $usuario, "ocontra" =>  $contra, 
             "nombre" => $nombre, "apellido" => $apellido, "idcurso" =>    $idcurso,
             "idsec" => $idsec, "idgru" => $idgru);

}

$json = json_encode($alumnos);
echo $json;
    
answered by 25.11.2016 в 20:29