Can';无法从java到php获取值
我想把值从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中未定义的索引:经度” 爪哇Can';无法从java到php获取值,java,php,Java,Php,我想把值从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中未定义的索引:经度” 爪哇 公共类PostServer扩展异步任务 { 私有HashMap参数; 私用int min; 公共PostServer(双lat、双lon、整数
公共类PostServer扩展异步任务
{
私有HashMap参数;
私用int min;
公共PostServer(双lat、双lon、整数分钟){
参数=新的HashMap();
参数put(“纬度”,纬度);
参数put(“经度”,lon);
分钟=分钟;
}
@凌驾
受保护的字符串背景(字符串…字符串){
StringBuilder后参数=新StringBuilder();
布尔值优先=真;
试一试{
URL=新URL(“http://172.17.9.47/location.php");
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(真);
connection.setRequestMethod(“POST”);
对于(Map.Entry postParams:parameters.entrySet()){
如果(第一){
第一个=假;
}
否则{
后参数。追加(“&”);
append(URLEncoder.encode(postParams.getKey(),“UTF-8”);
后参数。追加(“=”);
append(URLEncoder.encode(String.valueOf(postParams.getValue()),“UTF-8”);
}
}
postParameters.append(“&min=”);
后参数追加(最小值);
OutputStreamWriter oStream=新的OutputStreamWriter(connection.getOutputStream());
write(postParameters.toString());
oStream.flush();
oStream.close();
BufferedReader bReader=新的BufferedReader(新的InputStreamReader(connection.getInputStream());
StringBuilder结果=新建StringBuilder();
弦线;
而((line=bReader.readLine())!=null){
结果。追加(行);
}
返回result.toString();
}捕获(IOE异常){
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串s){
发送广播(“经度:+lon+”纬度:+lat);
super.onPostExecute(s);
}
}
PHP
没有“纬度”和“经度”post参数。你是如何执行你的php代码的?我是个新手,不知道你能不能帮我。这应该能帮你:var_dump(file_get_contents)的内容是什么php://input"));代码>?
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
$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'];
}
?>