为什么我从Android应用程序中没有收到服务器中的数据?

时间:2014-04-18 17:36:39

标签: php android http http-headers httpurlconnection

这应该是我的一个愚蠢的错误,请帮我搞清楚。 我有一个应用程序,使用POST将一些消息发送到php服务器,但没有数据,

URL url = new URL("http://192.168.1.2/");

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);

OutputStream outputStream = null;
try {
    if (purchase != null) {
            outputStream = conn.getOutputStream();
            BufferedOutputStream stream = new BufferedOutputStream(outputStream);
            stream.write(postmess.getBytes()); // <<<< No effect ?!
            stream.flush();
            stream.close();
        }
   } catch (IOException e) {
    e.printStackTrace();
        } finally {
            if (outputStream != null)
                try {
                    outputStream.close();
                } catch (IOException logOrIgnore) {
                    // ...
                }
        }


        Log.d(TAG, "data 2 send:" + postmess);

        outputStream.flush();
        outputStream.close();

        // Get the response
        int responseCode = conn.getResponseCode();
        Log.d(TAG, "Sending 'POST' request to URL : " + url);
        Log.d(TAG, "Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(
                new InputStreamReader(conn.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

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

        //  result
        Log.d(TAG, "Response:" + response.toString());
服务器中的

<?php
echo "POST = " . var_dump($_POST);

这是日志:

PostHTTPS:数据2发送:“这是长文本。”

PostHTTPS:向网址发送“POST”请求:http://xx.xx.xx.xx

PostHTTPS:响应代码:200 //表示正常

PostHTTPS:响应:array(0){} POST = array(0)

0 个答案:

没有答案