gpt4 book ai didi

java - Android HttpURLConnection POST 不工作。

转载 作者:行者123 更新时间:2023-11-30 22:12:39 26 4
gpt4 key购买 nike

我正在尝试检查数据库中是否存在数字,但无法将数据从 android 转发到 php。数据应该是 String、JSON 还是 HashMap?如何发送?

Java

try {

URL url = new URL("http://192.168.0.106/cei/tourist_home_activities.php");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setReadTimeout(15*1000);
conn.setConnectTimeout(15*1000);
conn.setRequestProperty( "Content-Type", "application/json" );
conn.setRequestProperty("Accept", "application/json");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.connect();

OutputStream os = conn.getOutputStream();

BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));

bufferedWriter.write(*Here is where I assume I input data*);
bufferedWriter.flush();
bufferedWriter.close();
os.close();

InputStream inputStream = conn.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String line = "";

while ((line = bufferedReader.readLine())!=null)
{
response+= line;
}
bufferedReader.close();
inputStream.close();
conn.disconnect();

} catch (IOException e) {
e.printStackTrace();
}

PHP

<?php

$servername = "127.0.0.1";
$username = "root";
$password = "";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

mysqli_select_db($conn,"discover_ilhabela");

$PIN=$_POST['pin'];

$result = mysqli_query($conn, "SELECT Name FROM `monitors` WHERE `PIN`='$PIN'");

/* if (mysql_num_rows($result)==1)
{
print("1");
}
else
{
print("0");
} */

print($PIN);

?>

我试图检查是否有任何结果来确认数据库中存在该数字,但首先我只是试图返回字符串,但它返回的是空字符串。

最佳答案

您的 PHP 端点需要 $_POST['pin'],因此您应该以纯文本形式发送。引用这个:How to add parameters to HttpURLConnection using POST

关于java - Android HttpURLConnection POST 不工作。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39561711/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com