将字符串从Android发布到PHP
首先,提前感谢您提供的任何帮助。我目前正在尝试将我的android应用程序连接到mysql数据库(Xampp)。下面是我在Android中使用的代码将字符串从Android发布到PHP,php,android,mysql,Php,Android,Mysql,首先,提前感谢您提供的任何帮助。我目前正在尝试将我的android应用程序连接到mysql数据库(Xampp)。下面是我在Android中使用的代码 public void submitRecipe(View view) throws IOException { URL u = new URL("http://localhost/phpconnect.php"); String data = "recipe="+"mar"; HttpURLConnection
public void submitRecipe(View view) throws IOException
{
URL u = new URL("http://localhost/phpconnect.php");
String data = "recipe="+"mar";
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestMethod("POST");
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
wr.close();
write(数据)之后,应用程序总是崩溃。我知道我没有用这个小代码段发布任何有意义的信息,但我现在只是想让它正常工作。Php代码如下。提前感谢您提供的任何帮助,因为我是Android新手,正在尝试让我的数据库连接正常工作
<?php
if(empty($_POST['recipe'])
{
echo "BAD";
}
else
{
$alpha = "recipe";
// Try to connect to the database.
$database=mysqli_connect("localhost","Michael","12345");
mysqli_select_db($database,"customers");
// If failed
if (mysqli_connect_errno()) {
// The database cannot be connected to.
echo "Database could not be connected to";
return;
}
$query = mysqli_query($database,"Select * From recipes Where Url LIKE '%" + $alpha + "%'");
if($query==FALSE)
{
}
else
{
while($row=mysql_fetch_assoc($query))
{
$output[]=$row;
}
echo "GOOD";
mysqli_close($database);
}
}
?>
您的崩溃与服务器端代码无关——您正试图在主(UI)线程上执行网络调用,因此。尝试在AsyncTask的doInBackground中运行它