duancheng6500 2016-10-27 05: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 05: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>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 没输出运行不了什么问题
  • ¥20 输入import torch显示Intel MKL FATAL ERROR,系统驱动1%,: Cannot load mkl_intel_thread.dll.
  • ¥15 点云密度大则包围盒小
  • ¥15 nginx使用nfs进行服务器的数据共享
  • ¥15 C#i编程中so-ir-192编码的字符集转码UTF8问题
  • ¥15 51嵌入式入门按键小项目
  • ¥30 海外项目,如何降低Google Map接口费用?
  • ¥15 fluentmeshing
  • ¥15 手机/平板的浏览器里如何实现类似荧光笔的效果
  • ¥15 盘古气象大模型调用(python)
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部