【发布时间】:2017-03-10 04:32:26
【问题描述】:
我一直在尝试使用 HttpUrlConnection 将一些信息从 android 应用程序发送到在线 PHP 脚本。我对 PHP 都非常陌生,以前从未使用过 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 不会输出任何内容。那么,您是否检查了数据库以查看表是否已创建?另外,当该设备返回时,您对该表有什么计划 - 当然,您不能重新创建该表。
-
本例中的字符串只是临时值。如果尚未创建,每个设备将在数据库中创建一个唯一的表。仅当设备还没有自己的表时,此脚本才会实际运行。对此的检查在 java 代码中执行。
-
我在工作,所以我回家时必须检查一下。我认为m提供的答案。 Waqas perves 的想法可能是正确的!
-
是的,我认为他发现了这个问题 - 我赞成,因为很明显没有调用查询,而且他对查询结果的附加消息也将帮助您调试。
标签: php android android-asynctask httpurlconnection