我正在編寫一個Android應用程序,我想發送一些JSON數據到PHP服務器。 POST請求確實去了服務器,但在我的server.php腳本中,我檢查了$ _POST變量,它是空的。 TCP/IP監視器不在Eclipse ADT中,wireshark不顯示localhost請求,所以我看不到實際發送的內容。那麼是否有人知道發送的內容以及如何在PHP中訪問它?或者我在代碼中犯了一個錯誤?
JSONObject json = new JSONObject();
try {
json.put("dog", "cat");
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpURLConnection urlConnection = null;
try {
URL url = new URL("http://10.0.2.2/server.php");
urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
OutputStreamWriter os = new OutputStreamWriter(urlConnection.getOutputStream(), "UTF-8");
os.write(json.toString());
os.close();
}
你從服務器得到什麼迴應? – mt0s
檢查$ _REQUEST,看看你得到了什麼。 –
@ t0s它發回我的server.php頁面的HTML。 – sonicboom