有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法从服务器上的MySql数据库获取响应

我正在开发一个可以写入数据库的应用程序,我只是想澄清一下为什么会发生这样的事情。我有一个AsyncTask类,用于发布和获取信息。它很好用。但在onPostExecute()中,如果php脚本响应,则会出现空指针异常。如果我把回声注释掉,一切正常。有人能解释一下为什么会这样,也许能纠正我做错的事情吗?我提供的代码就是导致错误的原因

@Override
protected void onPostExecute(String result) {
    switch (result){
        case "Registration Success...":
            Toast.makeText(context, result, Toast.LENGTH_SHORT).show();
            break;
        case "One row inserted":
            Toast.makeText(context, result, Toast.LENGTH_SHORT).show();
            break;
        default:
            alertDialog.setMessage(result);
            alertDialog.show();
            context.startActivity(new Intent(context, MainMenuActivity.class));
            break;
    }
}

php

<?php

require "init.php";

$name = $_POST["name"];
$email = $_POST["email"];
$mobile = $_POST["mobile"];

$sql = "insert into product_info values('$name', '$email', '$mobile');";

if(mysqli_query($con, $sql)){
      echo "<br><h3>One Row inserted</h3> // if I leave this like this it causes the error. 
} else {
      //echo "Error in insertion". mysqli_error($con);
}
?>

共 (0) 个答案