使用HttpURLConnection从Android发送HTTP POST

使用HttpURLConnection从Android发送HTTP POST,android,mysql,database,http-post,httpurlconnection,Android,Mysql,Database,Http Post,Httpurlconnection,我正在尝试从Android应用程序向我的PHP编码服务器发送httppost请求,该服务器将从请求接收到的数据放入MySQL数据库。如果我使用HttpClient和HttpPost方法,一切正常,但我决定尝试HttpURLConnection类,因为据说它比旧的HttpClient和HttpPost类更优化,更新得多,不幸的是,我无法让它以这种方式工作。我没有收到任何错误或异常,并且设备正在连接到网络,但什么也没有发生,给定的值没有写入数据库。请告诉我我做错了什么,并给我一些建议。也许使用Htt

我正在尝试从Android应用程序向我的
PHP
编码服务器发送
httppost
请求,该服务器将从请求接收到的数据放入
MySQL
数据库。如果我使用
HttpClient
HttpPost
方法,一切正常,但我决定尝试
HttpURLConnection
类,因为据说它比旧的
HttpClient
HttpPost
类更优化,更新得多,不幸的是,我无法让它以这种方式工作。我没有收到任何错误或异常,并且设备正在连接到网络,但什么也没有发生,给定的值没有写入数据库。请告诉我我做错了什么,并给我一些建议。也许使用
HttpClient/HttpPost
方法更好

这是我的代码:

private void writeToDatabase(URL url, String number, String comment) throws IOException  {
        HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
        httpConnection.setConnectTimeout(15 * 1000);
        httpConnection.setReadTimeout(10 * 1000);
        httpConnection.setRequestMethod("POST");
        httpConnection.setDoInput(true);
        httpConnection.setDoOutput(true);

        List<NameValuePair> params = new ArrayList<NameValuePair>(3);
        params.add(new BasicNameValuePair("code", "****"));
        params.add(new BasicNameValuePair("number", number));
        params.add(new BasicNameValuePair("comment", comment));

        OutputStream os = httpConnection.getOutputStream();
        BufferedWriter br = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
        br.write(getQuery(params));
        br.flush();
        br.close();
        os.close();

        httpConnection.connect();
}
这就是我调用
writeToDatabase()
函数的方式和位置:

private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException {
        StringBuilder result = new StringBuilder();
        boolean first = true;

        for (NameValuePair pair : params) {
            if (first) {
                first = false;
            } else result.append("&");

            result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
            result.append("=");
            result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
        }

        return result.toString();
}
Thread thread = new Thread() {

    @Override
    public void run () {
        try {
            URL url = new URL("http://www.***.com");
            writeToDatabase(url, "****", "as pats");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
};
thread.start();
编辑:这太奇怪了。。。请阅读以下代码段中的注释:

private void writeToDatabase(URL url, String number, String comment) throws IOException  {
        HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
        httpConnection.setConnectTimeout(15 * 1000);
        httpConnection.setReadTimeout(10 * 1000);
        httpConnection.setRequestMethod("POST");
        httpConnection.setDoInput(true);
        httpConnection.setDoOutput(true);

        httpConnection.connect();

        List<NameValuePair> params = new ArrayList<NameValuePair>(3);
        params.add(new BasicNameValuePair("code", "****"));
        params.add(new BasicNameValuePair("number", number));
        params.add(new BasicNameValuePair("comment", comment));

        OutputStream os = httpConnection.getOutputStream();
        BufferedWriter br = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
        br.write(getQuery(params));
        br.flush();
        br.close();
        os.close();
        //Everything works just fine with these lines below, but without them, it doesn't work... Why is that?
        BufferedReader in = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            Log.i("ilog", inputLine);
        }

        httpConnection.disconnect();
}
然后一切都完美地工作了。但为什么呢?有人能给我解释一下吗?我很感兴趣

编辑2:即使我设置了
httpConnection.setDoOutput(false)只要我阅读响应,它仍然可以正常工作。即使
setDoOutput()
设置为
false
,也会将值写入数据库。我完全糊涂了…

这个例子显示了connect()首先,将POST数据写入输出流,检查响应代码,关闭输出流


我同意斯维伦的观点。在不检查响应代码的情况下,将在POST发送到服务器之前重置tcp连接。但是没有人讨论这个问题。有什么原因吗

if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                inputStream = conn.getInputStream();
            }

你看过connection.getResponseCode()了吗?没有,但是我检查了html日志,没有遇到任何请求……我的代码只有在检查响应代码时才有效。但为什么呢?请看我编辑的帖子。
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                inputStream = conn.getInputStream();
            }