I want to send JSON string to the server every 60 seconds with HttpURLConnection
API. I am running the app on my smartphone device, which is connected to the laptop via USB cable. With this URL http://zzzzz.byethost8.com/connection.php
I am getting the code 500
as output of getResponseCode()
. I even tried it with the wamp server, but I am not getting any output there.
For WAMP I used this URL: http://192.168.134.45/connection.php
where 192.168.134.45
is my Wi-Fi IP address.
JSON String:
{
"latitude":80.86898504,
"longitude":20.66561187,
"time":"26.04.2015 12:45:11",
"route":4
}
The implementation of doInBackground()
method:
protected Void doInBackground(String... params) {
// TODO Auto-generated method stub
try {
System.out.println("The output of : doInBackground " +params[0]);
//URL myUrl = new URL("http://byethost8.com/connection.php");
URL myUrl = new URL("http://192.168.182.15/connection.php");
HttpURLConnection conn = (HttpURLConnection) myUrl.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
conn.setRequestProperty("Content-Type", "application/json");
System.out.println("The output of getResponsecode: "+conn.getResponseCode());
conn.connect();
// create data output stream
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
// write to the output stream from the string
wr.writeBytes(params[0]);
wr.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
PHP connection file with the default WAMP setting.
<?php
$json = json_decode(file_get_contents('php://input', true));
//hotname-username-password-datebase.
$db = new mysqli("sql209.byethost8.com", "b8_16138121", "fadi88", "b8_16138121_busTracker");
echo "You are in!";
if ($db->connect_errno) {
die("We are sorry, you could not be connected to the server,
please check your connection setting!");
}
?>