Android HttpUrlConnection 无法正常工作
Posted
技术标签:
【中文标题】Android HttpUrlConnection 无法正常工作【英文标题】:Android HttpUrlConnection not working properly 【发布时间】: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 的想法可能是正确的! 是的,我认为他发现了这个问题 - 我赞成,因为很明显没有调用查询,而且他对查询结果的附加消息也将帮助您调试。 【参考方案1】:在您的代码中:
$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)
)";
mysqli_close($con);
您只是创建了一个可变名称$create_table_query
,它只包含查询。没有实际执行查询的代码。在mysqli_close($con);
之前添加这段代码
if ($con->query($create_table_query) === TRUE)
echo "<br>Table created<br>";
else
echo "<br>Error creating table: " . $con->error. "<br>";
【讨论】:
我已经添加了这段代码,但创建的表仍然不完整。反正我不能在 phpMyAdmin 中看到! 尝试从您的 Webroswer 运行您的 php 代码,看看它是否正常工作。 让它工作了,谢谢!我已经接受了这个答案,因为我认为这确实是主要问题,尽管我也没有在我的 php 脚本中的表名之前留下空格。以上是关于Android HttpUrlConnection 无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章
为啥 Android 的 HttpUrlConnection 不支持 HTTP/2?
Android中的HttpURLConnection有线记录
[Android基础]Android中使用HttpURLConnection
Android HttpUrlConnection 无法正常工作