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);