我想知道为什么不将数据发送到MySQL?

时间:2018-07-13 07:02:47

标签: php android mysql

我想将数据发送到数据库。我的PHP文件还可以,因为我已经用邮递员进行了测试,然后将数据发送到数据库。但是当我使用Android应用程序时,它不应将数据发送到数据库这是我发送数据的Java方法,请给我答复解决方案。

 public void insertData(String name,String category,int quantity){
        String url = "http://192.168.172.2/foods/insert.php";
    try {
        String data = URLEncoder.encode("name", "UTF-8")
                + "=" + URLEncoder.encode(name, "UTF-8");

        data += "&" + URLEncoder.encode("category", "UTF-8") + "="
                + URLEncoder.encode(category, "UTF-8");

        data += "&" + URLEncoder.encode("quantity", "UTF-8")
                + "=" + URLEncoder.encode(String.valueOf(quantity), "UTF-8");


        URL url1 = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
        writer.write(data);
        writer.flush();

        Log.d("datainsert","success"+data);

    } catch (MalformedURLException e) {
        Log.d("datainsert","Error"+e.getMessage());

    } catch (IOException e) {
        Log.d("datainsert","Error"+e.getMessage());
   }

<?PHP
require "config.php";

global $connect;

if (isset($_POST['name']) and isset($_POST['category']) and isset($_POST['quantity'])) {
$name = $_POST['name'];
$category = $_POST['category'];
$quantity = $_POST['quantity'];
$intq = (int)$quantity;


if ($_SERVER['REQUEST_METHOD']== "POST") {

$query = "INSERT INTO food_details(food_name,category,quantity) VALUES('$name','$category',$intq);";

  if($q = mysqli_query($connect,$query)){
    echo "successfully inserted";
  }else{
  //echo  mysqli_error(mysqli_query($connect,$query));
    echo "not success";
    echo "Error: " . $query . "<br>" . $connect->error;
  }
}
}

mysqli_close($connect);
?>

1 个答案:

答案 0 :(得分:0)

使用POST方法Uri.Builder类将数据发送到服务器以创建查询字符串。如下

Uri.Builder builder = new Uri.Builder()
        .appendQueryParameter("firstParam", paramValue1)
        .appendQueryParameter("secondParam", paramValue2)
String query = builder.build().getEncodedQuery();

对于特定问题,您的发帖方法如下

try {
        URL url = new URL("http://192.168.172.2/foods/insert.php");
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setReadTimeout(10000);     //change according to need 
        conn.setConnectTimeout(15000);  //change according to need 
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);

        Uri.Builder builder = new Uri.Builder()
                .appendQueryParameter("name", name)
                .appendQueryParameter("category", category)
                .appendQueryParameter("quantity", quantity);
        String query = builder.build().getEncodedQuery();

        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(os, "UTF-8"));
        writer.write(query);
        writer.flush();
        writer.close();
        os.close();

        conn.connect();

        //get response from server
        String response = conn.getResponseMessage();
        Log.d("URl->", response);

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

希望它对您有用