0
我想每60秒使用HttpURLConnection
API将JSON字符串发送到服务器。我正在智能手机设备上运行应用程序,该设备通过USB电缆连接到笔记本电脑。通过这个URL http://zzzzz.byethost8.com/connection.php
我得到代码500
作为getResponseCode()
的输出。我甚至用wamp服务器尝试过,但我没有得到任何输出。Android:将httpURLConnection连接到服务器
对于WAMP我用这个网址:http://192.168.134.45/connection.php
其中192.168.134.45
是我的Wi-Fi IP地址。
JSON字符串:
{
"latitude":80.86898504,
"longitude":20.66561187,
"time":"26.04.2015 12:45:11",
"route":4
}
doInBackground()
方法的实现:
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使用默认设置WAMP连接文件。
<?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!");
}
?>
感谢您的回答,以及if($ json ['metodo'] ==“inserisciLuogo”)中的metodo和inserisciLuogo是什么? –
请你可以告诉我你使用的JSON字符串吗? –
我对多个请求使用相同的php页面,所以我在json的开头添加了一个标志来标识我想要运行的方法。 这是json样本: {“metodo”:“inserisciLuogo”,“纬度”:“21”,“纵向”:“32”,“nome”:“prova”,“indirizzo”:“indirizzo di prova” “Utente_idUtente”: “1”} – Filippo