1

I try to send data from my app Android to a db mysql going from a php file.

But it always returns me this error:

[Sat Mar 24 12:56:42.061845 2018] [php7:notice] [pid 1035] [client 192.168.1.151 :56068] PHP Notice: Undefined index: ID_ in /var/www/html/Find_First.php on line 18

The following is the android code : the speakers I use to send

HttpURLConnection con = utilityBOperation.Connect(urlAddress);
      //  Log.v(TAG,"Sender/ send: valore del risultato della connessione al server"+ con);

        if (con==null){
            Log.v(TAG,"Sender: Send : Il risultato del HttpURLConnection è nullo : " + con);
            return null;
        }
        try {
            Log.v(TAG,"Sender: Send : Il risultato del HttpURLConnection non è nullo : " );
            OutputStream os = con.getOutputStream();

            // Write
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
            bw.write(new DataPackager(Id_String,GRM_Family,GRM_Prodoct,MK_Nation,MK_Company).packData());

            Log.v(TAG,"Sender: Send : Il packData è stato inviato ");

            bw.flush();

            // RELEASE RES
           // Verifico i valori dolpo l' invio dei dati
            Log.v(TAG,"Sender: Send : Il risultato del HttpURLConnection " +
                            "Dopo l'invio dello streaming dati : " + con);
            Log.v(TAG,"Sender: Send : Il risultato del outputstreaming : " + os);

            bw.close();
            os.close();

            //HAS IT BEEN SUCCESSFUL?

            int responseCode=con.getResponseCode();

            Log.v(TAG,"Sender: Send : Il valore della risposta dell 'invio al server è:   " + responseCode);
            Log.v(TAG,"Sender: Send : Il valore della risposta dell 'invio al server è:   " + con.getResponseMessage());
            Log.v(TAG,"Sender: Send : Il valore della risposta dell 'invio al server getErrorStream():   " + con.getErrorStream());

            if (responseCode== HTTP_OK){

                //GET EXACT RESPONSE

                Log.v(TAG,"Sender: Send : Il responsecode è  Http_Ok  " + HTTP_OK );
                BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
                StringBuffer response = new StringBuffer();

                Log.v(TAG,"Sender: Send : Recupero il valore dello StringBUffer : " + response);

                String line;

                //READ LINE BY LINE
                while ((line=br.readLine())!= null){

                    response.append(line);
                }
                // RELEASE RES
                br.close();

                String JsonData = response.toString();
                JSONObject JsonOB = new JSONObject(JsonData);
                JSONArray parentArray = JsonOB.getJSONArray("response");
                JSONObject finalOB = parentArray.getJSONObject(0);

                Log.v(TAG,"Sender: Send : Il valore del Json che ricevo : " + finalOB);

                Find_Place.Rest_Data_to_Json(finalOB);
                return response.toString();

            }else {
                Log.v(TAG,"Sender: Send : La risposta dell 'invio al server è negativa :   " + responseCode);

        }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

-- the class where I create the connection

public static HttpURLConnection Connect(String urlAddress) {

        // TODO: Sono nel connect

        HttpURLConnection con;
        try {

            Log.v(TAG, "utilityBOopreation:Connect: entrato nel connect");

            URL url = new URL(urlAddress);
            con = (HttpURLConnection) url.openConnection();

            // set propertis
            con.setRequestMethod("POST");
            con.setConnectTimeout(1000);
            con.setReadTimeout(2000);
          //  con.setRequestProperty("USER-AGENT","Mozilla/5.0");
            con.setDoInput(true);
            con.setDoOutput(true);
          //  Log.v(TAG, "utilityBOopreation:Connect: Valore della connessione :  " + con);

            //return con;


        } catch (MalformedURLException e) {
            e.printStackTrace();
            Log.v(TAG, "utilityBOopreation:Connect: La connessione non è andata a buon fine : " + e);
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            Log.v(TAG, "utilityBOopreation:Connect: La connessione non è andata a buon fine : " + e);
            return null;
        }
        return con;

the class where I create the mo json

public String packData(){

        Log.v(TAG,"DataPackager:packData: Entro nel packData");

        JSONObject jo= new JSONObject();
        StringBuffer packedData =new StringBuffer();


        try {
            jo.put("ID",Id);
            jo.put("GRM",GRM);
            / .
                 altri campi
             ./


            Boolean FirstValue=true;
            Iterator it=jo.keys();
            Log.v(TAG,"DataPackager:packData: Creo il mio file Json ... " + jo);


            do {
                String Key=it.next().toString();
                String value=jo.get(Key).toString();
                if (FirstValue){
                    FirstValue=false;
                }else {
                    packedData.append("&");
                }

                packedData.append(URLEncoder.encode(Key,"UTF-8"));
                packedData.append("=");
                packedData.append(URLEncoder.encode(value,"UTF-8"));

            }while (it.hasNext());
            Log.v(TAG,"DataPackager:packData: Il packData Prima di mandarlo via stream ... " + packedData);

            return packedData.toString();

        } catch (JSONException | UnsupportedEncodingException e) {
            e.printStackTrace();
            Log.v(TAG,"DataPackager:packData:non sono riuscito a creare il json ... " + e.getMessage());
        }
        return null;
    }

and this is the php file on the server,Only the receiving part of the POST

$host = 'localhost';
$username = 'user';
$pwd = '';
$db = 'mioDB';

if (isset($_POST)){

  $ID = $_POST['ID'];
  $GRM = $_POST['GRM'];
  /
 altri valori
  /
  $response = array();

  $connect = mysqli_connect($host,$username,$pwd,$db) or die ('Unabletp connect');
B. Desai
  • 16,414
  • 5
  • 26
  • 47
Andrea
  • 128
  • 3
  • 13

1 Answers1

0

As your data received in json. They will not retrived using POST. You have to use file_get_contents("php://input");

$data_json = file_get_contents("php://input");
$data = json_decode($data_json,true);

Then do following

$ID = $data['ID'];
$GRM = $data['GRM'];
B. Desai
  • 16,414
  • 5
  • 26
  • 47
  • hello, dispaice me but it does not work yet !! I will insert another comment with also apache error.log !!! can you tell me where I'm wrong? I think the problem is in android !! thank you!! I'm really desperate – Andrea Mar 30 '18 at 15:32
  • GRAZIE @ B.Desai !!! I have made your changes and errors have disappeared !!! thank you so much!! the problem is that when I try to print the value of my variables that I should receive in the Post I'm empty! In fact, in my answer I have included both the modified code and the print of both the value of the variables and the query string that I use to create my DB! I think the problem is in my android code because I know that I do not send anything! Can you give it a look and help me? – Andrea Apr 03 '18 at 09:33