I want to get pass the value from java to php. the example content that i want to display in php is:
"2019-11-28 13:06:43 Latitude: 13.54, Longitude: 129.0"
error that I'm getting is:
"Notice: Undefined index: latitude in C:\xampp\htdocs\location.php on line 7"
"Notice: Undefined index: longitude in C:\xampp\htdocs\location.php on line 7"
JAVA
public class PostServer extends AsyncTask<String, String, String>
{
private HashMap<String, Double> parameters;
private int min;
public PostServer(double lat, double lon, int mins) {
parameters = new HashMap<>();
parameters.put("latitude", lat);
parameters.put("longitude", lon);
min = mins;
}
@Override
protected String doInBackground(String... strings) {
StringBuilder postParameters = new StringBuilder();
boolean first = true;
try{
URL url = new URL("http://172.17.9.47/location.php");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
for(Map.Entry<String, Double> postParams : parameters.entrySet()){
if (first) {
first = false;
}
else {
postParameters.append("&");
postParameters.append(URLEncoder.encode(postParams.getKey(), "UTF-8"));
postParameters.append("=");
postParameters.append(URLEncoder.encode(String.valueOf(postParams.getValue()), "UTF-8"));
}
}
postParameters.append("&min=");
postParameters.append(min);
OutputStreamWriter oStream = new OutputStreamWriter(connection.getOutputStream());
oStream.write(postParameters.toString());
oStream.flush();
oStream.close();
BufferedReader bReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder result = new StringBuilder();
String line;
while ((line = bReader.readLine()) != null){
result.append(line);
}
return result.toString();
} catch (IOException e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
sendBroadcast(" Longitude: "+lon +" Latitude: " + lat );
super.onPostExecute(s);
}
}
PHP
<?php
$myfile = fopen("Task5.2/location.txt", "w");
fwrite($myfile, "testing");
fclose($myfile);
if( $_POST["latitude"] || $_POST["longitude"] ) {
date_default_timezone_set("Singapore");
echo date("Y-m-d h:i:sa"). "\t";
echo "Latitude: ". $_POST['latitude']. ",\t";
echo "Longitude: ". $_POST['longitude'];
}
?>