0

I am trying to send post data but getting error, unabel to create json object. Please help.

I am sending this from java: o am getting error i need please let me know if their is any correction in php. i don't know php

JSON:

{
    "tagtrack_scans": {
        "product_id": "1",
        "scanned_as": "12334556",
        "email": "aabce@gmail.com"
    }
}

PHP code:

 <?php
    $json_data=$_POST['tagtrack_scans'];
    $data=json_decode($json_data, true);

    echo $data['email'];
    echo $data['scanned_as'];
    echo $data['product_id'];

    $dbhost = 'localhost:2082';
    $dbuser = 'user';
    $dbpass = 'password';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    if(! $conn )
    {
      die('Could not connect: ' . mysql_error());
    }
    echo 'Connected successfully';
    mysql_select_db( 'tagtrack_scans' );
    if(! $conn )
    {
      die('Could not connect: ' . mysql_error());
    }
    echo 'Connected to tagtrack_scans';

    $sql="INSERT INTO tagtrack_scans (email, scanned_as, product_id)
    VALUES ($email, $canned_As, $product_id)";

    if (!mysqli_query($con,$sql))
    {
      die('Error: ' . mysqli_error($con));
    }
    echo "1 record added";

    mysql_close($conn);
?>

Java code used to send the JSON to PHP:

JSONObject jsonObjSend = new JSONObject();
JSONObject jsonObj = new JSONObject();

try {
    // Add key/value pairs

    jsonObjSend.put("email", "ane@gmail.com");
    jsonObjSend.put("canned_As", "12334556");
    jsonObjSend.put("product_id", "1");

    jsonObj.put("tagtrack_scans", jsonObjSend);

    // Add a nested JSONObject (e.g. for header information)
    // JSONObject header = new JSONObject();
    // header.put("deviceType","Android"); // Device type
    // header.put("deviceVersion","2.0"); // Device OS version
    // header.put("language", "es-es"); // Language of the Android client
    // jsonObjSend.put("header", "");

    // Output the JSON object we're sending to Logcat:
    Log.i("MainActivity", jsonObj.toString(2));

} catch (JSONException e) {
     e.printStackTrace();
}

JSONObject jsonObjRecv = HttpClient.SendHttpPost("http://abc.php", jsonObj);
nstCactus
  • 5,141
  • 2
  • 30
  • 41

1 Answers1

0

According to this answer, you're not getting your JSON data the right way.

Try this instead:

<?php
    $json_data=file_get_contents("php://input");

    $data=json_decode($json_data, true);
    if(isset($data['tagtrack_scans'])){
        $data = $data['tagtrack_scans'];
    }
    else{
        die('Unexpected JSON received');
    }

    echo $data['email'];
    echo $data['scanned_as'];
    echo $data['product_id'];

    $dbhost = 'localhost:2082';
    $dbuser = 'user';
    $dbpass = 'password';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    if(! $conn )
    {
      die('Could not connect: ' . mysql_error());
    }
    echo 'Connected successfully';
    mysql_select_db( 'tagtrack_scans' );
    if(! $conn )
    {
      die('Could not connect: ' . mysql_error());
    }
    echo 'Connected to tagtrack_scans';

    $sql="INSERT INTO tagtrack_scans (email, scanned_as, product_id)
    VALUES ($email, $canned_As, $product_id)";

    if (!mysqli_query($con,$sql))
    {
      die('Error: ' . mysqli_error($con));
    }
    echo "1 record added";

    mysql_close($conn);
?>

Please keep in mind that this is quick'n dirty and untested. You should improve it before using it in production.

Community
  • 1
  • 1
nstCactus
  • 5,141
  • 2
  • 30
  • 41