donglue1886 2016-11-17 22:12
浏览 132

无法从Android应用程序向mysql数据库插入数据

Hey I am trying to insert data to my sql data base and I cant figure out why it isn't working, when I run the php script on the host alone its working but when I try to do it from the android app it just doesn't do nothing I couldn't find the problem so I thought you guys might help me

here it the android code:

  @Override
    protected String doInBackground(String... params) {

        String insertUrl = "http://localhost/insert.php";

        club_name = params[1];
        String publisher_name = params[2];
       String club_contact = params[3];
       String club_location = params[4];
       String club_hours = params[5];
       String club_age = params[6];
       String club_description = params[7];

        try {
            URL url = new URL(insertUrl);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
            String data = URLEncoder.encode("age","UTF-8")+"="+URLEncoder.encode(club_name,"UTF-8")+"&"+
                    URLEncoder.encode("number","UTF-8")+"="+URLEncoder.encode(publisher_name,"UTF-8");
                  //  URLEncoder.encode("publisher_phone","UTF-8")+"="+URLEncoder.encode(club_contact,"UTF-8")+"&"+
                   // URLEncoder.encode("location","UTF-8")+"="+URLEncoder.encode(club_location,"UTF-8")+"&"+
                   // URLEncoder.encode("hour_open","UTF-8")+"="+URLEncoder.encode(club_hours,"UTF-8")+"&"+
                   // URLEncoder.encode("age","UTF-8")+"="+URLEncoder.encode(club_age,"UTF-8")+"&"+
                   // URLEncoder.encode("description","UTF-8")+"="+URLEncoder.encode(club_description,"UTF-8");
            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();
            InputStream inputStream = httpURLConnection.getInputStream();
            inputStream.close();



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

        return "Party has been posted!";
    }

and the php file:

<?php
require "connection.php";

$club_name = $_POST["club_name"];
$publisher_name = $_POST["publisher_name"];
$publisher_phone = $_POST["publisher_phone"];
$location = $_POST["location"];
$hour_open = $_POST["hour_open"];
$age = $_POST["age"];
$description = $_POST["description"];

$sql = "insert into apps(club_name,publisher_name,publisher_phone,location,hour_open,age,description) values ('$club_name','$publisher_name','$publisher_phone','$location','$hour_open','$age','$description')";
$query = mysqli_query($conn,$sql);

?>

connection php:

<?php
$db_name = "ori";
$mysql_username = "root";
$mysql_password = "";
$server_name = "localhost";
$conn = mysqli_connect($server_name,$mysql_username,$mysql_password,$db_name);

if($conn)
{
    //echo "connects";
}
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 sub地址DHCP问题
    • ¥15 delta降尺度计算的一些细节,有偿
    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错
    • ¥15 Matlab编程问题
    • ¥15 训练的多模态特征融合模型准确度很低怎么办
    • ¥15 kylin启动报错log4j类冲突
    • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大