【发布时间】:2017-05-15 14:52:09
【问题描述】:
我想通过我的 Android 应用程序在网站上发送一个变量,所以我尝试了这个:
OutputStreamWriter writer = null;
URLConnection connexion = null;
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
String data = msg.getData().getString("receivedData");
long t = System.currentTimeMillis();
if(t-lastTime > 100) { // Pour eviter que les messages soit coupes
ReceptionDetect.append("\n");
lastTime = System.currentTimeMillis();
}
ReceptionDetect.append(data);
try {
// Encodage des paramètres de la requête
String donnees = URLEncoder.encode("Variable="+data);
// On a envoyé les données à une adresse distante
URL url = new URL("http://MyWebsite/myPhp.php");
connexion = url.openConnection();
connexion.setDoOutput(true);
//connexion.setChunkedStreamingMode(0);
// On envoie la requête ici
writer = new OutputStreamWriter(connexion.getOutputStream());
// On insère les données dans notre flux
writer.write(donnees);
// Et on s'assure que le flux est vidé
writer.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try{writer.close();}catch(Exception e){}
}
}
};
在我的网站上使用这个 php:$_GET['Variable'];
echo 'Le détecteur a répondu : ';
echo ($Variable);
或者也可以在php中打开一个txt文件:
<?php
$_GET['Variable'];
// Open the text file
$f = fopen("TextRecevoir.txt", "w");
// Write text
fwrite($f, $_GET["Variable"]);
// Close the text file
fclose($f);
// Open file for reading, and read the line
$f = fopen("TextRecevoir.txt", "r");
?>
但它不起作用,应用程序启动但php文件中没有写入任何内容。 如果有人可以帮助我,我不明白为什么它不起作用。 谢谢你
-> 是的,我将互联网权限放在清单中。
【问题讨论】:
-
为什么不用凌空抽射?
-
好吧,我已经看过开发人员 (developer.android.com/training/volley/simple.html) 上的教程,我目前正在尝试但没有成功,但我也想知道为什么我的代码不起作用。
-
老实说,复制粘贴 volley 代码比调试代码要快得多。当涉及到您的代码时,您在哪里将参数附加到您的网址?
-
最后我的代码是对的,错误在 php 文件中...
标签: javascript php android url