I want to output the echo of the PHP script in the logcat but nothing comes back. The JSON String {"mac": "10:A5:D0:06:C6:E9", "latitude":84.16898451,"longitude":3.16561387,"route": 1}
is there in the doInBackground method I can see it in the output. The output of the getResponseCode is 200
but I am trying now to read the echo of the PHP script but nothing is appearing the Logcat.
On the server side the table is being created but no data is being inserted into it.
doInbackground method:
protected Void doInBackground(String... params) {
// TODO Auto-generated method stub
BufferedReader reader = null;
try {
System.out.println("The output of : doInBackground " + params[0]);
URL myUrl = new URL("http://byethost.com/index.php");
HttpURLConnection conn = (HttpURLConnection) myUrl.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.connect();
System.out.println("The output of getResponsecode: "
+ conn.getResponseCode());
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(params[0]);
StringBuilder sb = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
sb.append(line + "
");
}
System.out.println("The output of the StringBulder: "
+ sb.toString());
wr.close();
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if (reader != null) {
try {
reader.close();
return null;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
PhP script:
<?php
$data = json_decode ( file_get_contents ( 'php://input', true ) );
$con = new mysqli ( "domin.com", "username", "password", "database" );
$mac = $data->{'mac'};
$latitude = $data->{'latitude'};
$longitude = $data->{'longitude'};
$route = $data->{'route'};
// check whether route's table exist.
$results = $con->query ( "SHOW TABLES like 'bus' " ) or die ( mysqli_error () );
if (($results->num_rows) == 1) {$sql = "REPLACE INTO bus(mac, route, latitude, longitude)
VALUES( ?, ?, ? , ? )";
$stmt = $con->prepare($sql);
if(false === $stmt){
echo "prepare() failed: ";
}
$rc = $stmt->bind_param("ssss",$mac,$route, $latitude,$longitude);
echo $rc;
if ( false===$rc ) {
echo "bind_param() failed: ";
}
$rc = $stmt->execute();
echo "execute failed.";
if ( false===$rc ) {
die('execute() failed: ' . htmlspecialchars($stmt->error));
}
$stmt->close();
echo "Table exist";
} else {
$create = "CREATE TABLE bus
(id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
mac VARCHAR(30) NOT NULL UNIQUE,
route int(11) ,
latitude FLOAT(10,6) NOT NULL ,
longitude FLOAT(10,6) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)" ;
$stmt = $con->prepare($create) or die ( $con->error );
$stmt->execute();
$stmt->close();
}