Я пытаюсь отправить данные (идентификатор ученика #) из приложения Android в файл PHP через тело HTTP-запроса POST. Затем PHP-файл отправит данные (просто строка) обратно в приложение. Тем не менее, мой php, похоже, не может прочитать мои данные POST (идентификатор студента #) из запроса.Данные POST, отправленные из приложения android, не получаемые файлом PHP
Мой файл .java:
JSONObject jsonParam = new JSONObject();
DataOutputStream printout;
String idIN = params[0];
jsonParam.put("id_in", idIN);
BufferedReader input;
String result;
URL url = null;
HttpURLConnection urlConnection = null;
url = new URL("http://10.0.2.2/project/connector.php");
urlConnection = (HttpURLConnection) url.openConnection();
//prepare request
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setReadTimeout(10000);
urlConnection.setConnectTimeout(15000);
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
printout = new DataOutputStream(urlConnection.getOutputStream());
//printout.write(jsonParam);
printout.writeUTF(URLEncoder.encode(jsonParam.toString(),"UTF-8"));
//this returns: {"id_in":"1010101"}
Log.d("json out: ", jsonParam.toString());
printout.flush();
printout.close();
int response = -1;
response = urlConnection.getResponseCode();
input = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
result = input.readLine();
// This returns 200
Log.d("response code: ", result);
urlConnection.disconnect();
МОЙ .PHP FILE:
<?php
header('Content-Type: text/html; charset=utf-8');
$id_in = "";
echo "test1";
$id_in = trim($_POST['id_in']);
echo "test2";
if (isset($_POST['id_in'])) {
echo "good";
}else{
echo "bad";
}
mysqli_close($myconn);
Мой Android приложение получает код 200 ответа и получает "test1" в ответ , но не «test2», поэтому проблема должна возникать, когда мой PHP-файл пытается прочитать данные POST: $id_in = trim($_POST['id_in']);
Вы не можете использовать POST() для JSON, вам нужно использовать 'file_get_contents ('php: // input')' –