duancheng6500 2016-10-27 13:11
浏览 85
已采纳

Android HttpUrlConnection无法正常工作

I have been trying to send some information from an android application using the HttpUrlConnection to a PHP script online. I am very new to both PHP and have never used the HttpUrlConnection before so I am quite stuck as to why this is not working. There are no errors being thrown either.

my php script is

<?php

//get connection details from the app and then connect to db
$db_host = $_POST['db_host'];
$db_user = $_POST['db_user'];
$db_pass = $_POST['db_pass'];
$db_name = $_POST['db_name'];

$con = mysqli_connect($db_host, $db_user, $db_pass, $db_name);

if (!$con) {
    die("Connection failed: " . mysqli_connect_error());
}

$device_group_id = $_POST['device_group_id'];

//create the new device group as a table in the database
$create_table_query = "CREATE TABLE" . $device_group_id . "(
        device_id varchar(24),
        device_alias varchar(24),
        device_longitute int,
        device_latititute int,
        device_group_id varchar(24)
)";

if (mysqli_query($con, $create_table_query)) {
    echo "Table MyGuests created successfully";
} else {
    echo "Error creating table: " . mysqli_error($con);
}

mysqli_close($con);

?>

and the Asynctask method doInBackground() is

@Override
protected Object doInBackground(Object[] params) {
    String result = "";
    try{
        //get values to pass to the PHP script
        Pair<String, String> dbHost = new Pair<>("db_host", DbConnectionInfo.DB_HOST);
        Pair<String, String> dbUser = new Pair<>("db_user", DbConnectionInfo.DB_USER);
        Pair<String, String> dbPass = new Pair<>("db_pass", DbConnectionInfo.DB_PASS);
        Pair<String, String> dbName = new Pair<>("db_host", DbConnectionInfo.DB_NAME);
        Pair<String, String> deviceGroupId = new Pair<>("device_group_id", "new_device_group");

        URL url = new URL("http://tracme.net16.net/create_device_group.php");
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

        // prepare request
        urlConnection.setRequestMethod("POST");
        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(true);
        urlConnection.setReadTimeout(10000);
        urlConnection.setConnectTimeout(15000);
        urlConnection.setChunkedStreamingMode(0);

        //upload request
        OutputStream outputStream = urlConnection.getOutputStream();
        OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");
        writer.write(dbHost.first + "=" + dbHost.second);
        writer.write(dbUser.first + "=" + dbUser.second);
        writer.write(dbPass.first + "=" + dbPass.second);
        writer.write(dbName.first + "=" + dbName.second);
        writer.write(deviceGroupId.first + "=" + deviceGroupId.second);
        writer.close();
        outputStream.close();

        // read response
        BufferedReader in = new BufferedReader(
                new InputStreamReader(urlConnection.getInputStream()));

        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) { response.append(inputLine); }
        in.close();

        result = response.toString();

        // disconnect
        urlConnection.disconnect();

    }catch(MalformedURLException e) {
        Log.e("Malformed URL Exception", "Malformed URL Exception");
    }catch (IOException e) {
        Log.e("IOException", "IOException");
    }

    return result;
}
  • 写回答

1条回答 默认 最新

  • dongpu3347 2016-10-27 13:32
    关注

    In your code :

    $device_group_id = $_POST['device_group_id'];
    
    //create the new device group as a table in the database
    $create_table_query = "CREATE TABLE" . $device_group_id . "(
        device_id varchar(24),
        device_alias varchar(24),
        device_longitute int,
        device_latititute int,
        device_group_id varchar(24)
    )";
    mysqli_close($con);
    

    You are just creating a vairable name $create_table_query which just holds the query.There is no code that actually executes the query. Add this code before mysqli_close($con);

    if ($con->query($create_table_query) === TRUE) {
        echo "<br>Table created<br>";
    } else {
        echo "<br>Error creating table: " . $con->error. "<br>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面