duandou8120 2018-07-13 07:02
浏览 48
已采纳

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

I want to send data to the database.My PHP Files are OK,because I had tested that with postman and it send data to the database.But when I used Android app for that it shouldn't send data to the database.Here is my java method for the sending data.Reply me with the solution please.

 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条回答 默认 最新

  • douxian4888 2018-07-14 01:58
    关注

    Sending Data to server using POST method Uri.Builder class for creating query String.As follows

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

    For your question specific your post method will be as follows

    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();
        }
    

    I hope its work for you

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化