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条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥40 python,计算机程序运行结果很奇怪
      • ¥15 有关时间计算器的问题
      • ¥200 Chrome浏览器自动保存密码需要解密!来人
      • ¥18 关于#c语言#的问题:如何把下面几个代码放在一起编序号,然后输入对应数字运行对应代码,还可以返回重输
      • ¥30 windows下无法运行go test的问题
      • ¥15 多个Rigify骨骼 导入一起 后 没有 控制器面板
      • ¥15 想问一下sprak ada语言的pre和post怎么设
      • ¥15 使用python将分子模拟输出.gro文件
      • ¥15 港诡实录的背包系统拆解
      • ¥15 宽带IP网络路由器的初始路由表和最终路由表