我一直在嘗試使用HttpUrlConnection將Android應用程序的某些信息發送到PHP腳本在線。我對PHP都很陌生,以前從來沒有使用過HttpUrlConnection,所以我很堅持爲什麼這不起作用。也沒有錯誤被拋出。Android HttpUrlConnection無法正常工作
我的PHP腳本是
<?php
//get connection details from the app and then connect to db
$db_host = $_POST['db_host'];
$db_user = $_POST['db_user'];
$db_pass = $_POST['db_pass'];
$db_name = $_POST['db_name'];
$con = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$device_group_id = $_POST['device_group_id'];
//create the new device group as a table in the database
$create_table_query = "CREATE TABLE" . $device_group_id . "(
device_id varchar(24),
device_alias varchar(24),
device_longitute int,
device_latititute int,
device_group_id varchar(24)
)";
if (mysqli_query($con, $create_table_query)) {
echo "Table MyGuests created successfully";
} else {
echo "Error creating table: " . mysqli_error($con);
}
mysqli_close($con);
?>
和的AsyncTask方法doInBackground()是
@Override
protected Object doInBackground(Object[] params) {
String result = "";
try{
//get values to pass to the PHP script
Pair<String, String> dbHost = new Pair<>("db_host", DbConnectionInfo.DB_HOST);
Pair<String, String> dbUser = new Pair<>("db_user", DbConnectionInfo.DB_USER);
Pair<String, String> dbPass = new Pair<>("db_pass", DbConnectionInfo.DB_PASS);
Pair<String, String> dbName = new Pair<>("db_host", DbConnectionInfo.DB_NAME);
Pair<String, String> deviceGroupId = new Pair<>("device_group_id", "new_device_group");
URL url = new URL("http://tracme.net16.net/create_device_group.php");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
// prepare request
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setReadTimeout(10000);
urlConnection.setConnectTimeout(15000);
urlConnection.setChunkedStreamingMode(0);
//upload request
OutputStream outputStream = urlConnection.getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");
writer.write(dbHost.first + "=" + dbHost.second);
writer.write(dbUser.first + "=" + dbUser.second);
writer.write(dbPass.first + "=" + dbPass.second);
writer.write(dbName.first + "=" + dbName.second);
writer.write(deviceGroupId.first + "=" + deviceGroupId.second);
writer.close();
outputStream.close();
// read response
BufferedReader in = new BufferedReader(
new InputStreamReader(urlConnection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) { response.append(inputLine); }
in.close();
result = response.toString();
// disconnect
urlConnection.disconnect();
}catch(MalformedURLException e) {
Log.e("Malformed URL Exception", "Malformed URL Exception");
}catch (IOException e) {
Log.e("IOException", "IOException");
}
return result;
}
如果不失敗,php不會輸出任何內容。所以,你檢查數據庫,看看是否創建表?此外,當該設備返回時,您對該表的計劃是什麼 - 當然,您無法重新創建表格。 – WEBjuju
這個例子中的字符串只是臨時值。如果尚未創建,每個設備將在數據庫中創建一個唯一的表。這個腳本實際上只在設備沒有自己的表時才運行。對此的檢查在java代碼中執行。 – user2145312
我在工作,所以將不得不檢查我什麼時候回家。我認爲答案由m提供。 Waqas perves可能是正確的想法! – user2145312