使用 Eclipse 将表单数据发布到 php 服务器时如何获得正确的响应?
How to get proper response when posting form data to php server using eclipse?
我正在向服务器发送 json 数据,但在收到响应时应该是这样的
{"id":65,"check":1,"date":"08-Jan-19"}
相反,我得到这个
{"id":"65check=1","check":null,"date":"08-Jan-19"}
这是单击按钮时的代码 我向服务器发送 json 表单数据,但作为响应,id 值附加到检查值,如何获得正确的响应。
Attendance_TimeCheck = "1";
users_identify = "65";
try {
URL urlForPostRequest = new URL("http://xenzet.com/ds/getrec.php");
System.out.println("Instantiated new URL: " + urlForPostRequest);
final long id = Long.valueOf(users_identify);
HttpURLConnection conection = (HttpURLConnection) urlForPostRequest.openConnection();
conection.setDoOutput(true);
conection.setRequestMethod("POST");
conection.setRequestProperty("User-Agent", "Mozilla/5.0");
conection.getOutputStream().write(("id="+id).getBytes(StandardCharsets.UTF_8));
conection.getOutputStream().write(("check="+Attendance_TimeCheck).getBytes(StandardCharsets.UTF_8));
conection.connect();
BufferedInputStream bis = new BufferedInputStream(conection.getInputStream());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int resultBuffer = bis.read();
while (resultBuffer != -1) {
bos.write((byte) resultBuffer);
resultBuffer = bis.read();
}
String result1 = bos.toString();
System.out.println(result1);
} catch (Exception ex) {
ex.printStackTrace();
}
我认为字符串读取错误。
检查您传递给 POST 请求的负载,
conection.getOutputStream().write(("id="+id).getBytes(StandardCharsets.UTF_8));
conection.getOutputStream().write(("check="+Attendance_TimeCheck).getBytes(StandardCharsets.UTF_8));
"id="+id
后跟 "check="+Attendance_TimeCheck
将导致 "id":"65check=1"
在查询参数 check
之前附加 & 符号以获得所需的结果,
conection.getOutputStream().write(("&check="+Attendance_TimeCheck).getBytes(StandardCharsets.UTF_8));
我正在向服务器发送 json 数据,但在收到响应时应该是这样的
{"id":65,"check":1,"date":"08-Jan-19"}
相反,我得到这个
{"id":"65check=1","check":null,"date":"08-Jan-19"}
这是单击按钮时的代码 我向服务器发送 json 表单数据,但作为响应,id 值附加到检查值,如何获得正确的响应。
Attendance_TimeCheck = "1";
users_identify = "65";
try {
URL urlForPostRequest = new URL("http://xenzet.com/ds/getrec.php");
System.out.println("Instantiated new URL: " + urlForPostRequest);
final long id = Long.valueOf(users_identify);
HttpURLConnection conection = (HttpURLConnection) urlForPostRequest.openConnection();
conection.setDoOutput(true);
conection.setRequestMethod("POST");
conection.setRequestProperty("User-Agent", "Mozilla/5.0");
conection.getOutputStream().write(("id="+id).getBytes(StandardCharsets.UTF_8));
conection.getOutputStream().write(("check="+Attendance_TimeCheck).getBytes(StandardCharsets.UTF_8));
conection.connect();
BufferedInputStream bis = new BufferedInputStream(conection.getInputStream());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int resultBuffer = bis.read();
while (resultBuffer != -1) {
bos.write((byte) resultBuffer);
resultBuffer = bis.read();
}
String result1 = bos.toString();
System.out.println(result1);
} catch (Exception ex) {
ex.printStackTrace();
}
我认为字符串读取错误。
检查您传递给 POST 请求的负载,
conection.getOutputStream().write(("id="+id).getBytes(StandardCharsets.UTF_8));
conection.getOutputStream().write(("check="+Attendance_TimeCheck).getBytes(StandardCharsets.UTF_8));
"id="+id
后跟 "check="+Attendance_TimeCheck
将导致 "id":"65check=1"
在查询参数 check
之前附加 & 符号以获得所需的结果,
conection.getOutputStream().write(("&check="+Attendance_TimeCheck).getBytes(StandardCharsets.UTF_8));