我想将值从Java传递到php。 我想在php中显示的示例内容是:
“ 2019-11-28 13:06:43纬度:13.54,经度:129.0”
我遇到的错误是:
“注意:未定义的索引:第7行的C:\ xampp \ htdocs \ location.php中的纬度”
“注意:未定义的索引:第7行的C:\ xampp \ htdocs \ location.php中的经度”
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'];
}
?>