Undefined index JSON Android

Asked

Viewed 91 times

0

Hi, I’m making the following mistake: I am developing an application that checks a table of an external mysql database by sending the device’s IMEI returning a hardware id, but when debugging the code in the Asynchttpresponsehandler response line shows the following message (copied and pasted creating an html file). I wonder what I’m doing wrong in my code

inserir a descrição da imagem aqui

Method that sends the IMEI to search

    public void sendIMEIgetEquip(){
    TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
    String IMEI = telephonyManager.getDeviceId();
    JSONObject json = new JSONObject();
    String path;
    try {
        json.put("imei", IMEI);
        json.put("idEquip", 0);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    AsyncHttpClient client = new AsyncHttpClient();
    RequestParams params = new RequestParams();
    params.put("imei",json);
    path = "http://"+ip+"/mytolite/equipamento/getequipbyimei.php";
    client.get(path, params, new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(String response) {
            setMyEquip(response);
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
            dialog.hide();
            if (statusCode == 404) {
                Toast.makeText(getApplicationContext(), "Recurso requisitado não encontrado", Toast.LENGTH_SHORT).show();
            } else if (statusCode == 500) {
                Toast.makeText(getApplicationContext(), "Erro de servidor", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getApplicationContext(), "Erro inesperado [verifique sua conexão]", Toast.LENGTH_LONG).show();
            }
        }
    });
}

Php code that handles json

<?php
/**
 * Updates Sync status of Users
 */
include_once './db_functions.php';
//Create Object for DB_Functions clas
$db = new DB_Functions();
//Get JSON posted by Android Application
$json = $_POST["imei"];
//Remove Slashes
echo $json;
if (get_magic_quotes_gpc()){
$json = stripslashes($json);
}
//Decode JSON into an Array
$data = json_decode($json);
//Util arrays to create response JSON
$a=array();
$b=array();
//Loop through an Array and insert data read from JSON into MySQL DB

//Store User into MySQL DB
$equip = $db->getEquipByImei($data->imei);
//Based on inserttion, create JSON response
    while ($row = mysqli_fetch_array($equip)) {
        $b["id"] = $row["id"];
        array_push($a,$b);
    }
    try{
        if($a == ''){
            echo 'jsonvazio';
        }else {
            echo json_encode($a);
        }
    }catch (Exception $e){
        echo json_last_error_msg();
        echo 'exceção: ', $e->getMessage(), "/n";
    }
?>

php method that returns the device id based on the device’s IMEI

    public function getEquipByImei($imei){
    $result = mysqli_query($this->db->connect(), "SELECT idEquipamento FROM equipamentotablet WHERE imei = '$imei'");
    return $result;
}

Note: Line 9 : $json = $_POST["Imei"]; (I believe this is where he is not finding the json index) Line 23 : $Equip = $db->getEquipByImei($data->Imei);

Thanks for your help.

1 answer

0

Good big afternoon, try using instead of the $_POST the $_REQUEST, and in the HTTP android tries to use the client.post(...).

  • It gave the same error, undefined index... somehow it’s not passing the json to the file, I think

Browser other questions tagged

You are not signed in. Login or sign up in order to post.