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


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:

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

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

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


        URL url = new URL(""+cur+"&seccion="+sec+"&grupo="+gru);

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

        sb = new StringBuilder();


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

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


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

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

    try {
        JSONArray array = new JSONArray(response);
            res = 1;

        /*for (int i=0;i<array.length();i++){

    }catch (Exception e){
        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


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:


 $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

2 answers


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

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