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)