gpt4 book ai didi

java - 如果 JSON 响应结果为 0/11,如何将用户发送到 servlet 内的 .html 页面

转载 作者:太空宇宙 更新时间:2023-11-04 09:55:51 26 4
gpt4 key购买 nike

假设用户使用正确的凭据登录我的网站,结果是

{"ticketStatus":"Valid","requestId":"73","result":0,"resultText":"Valid"}

{"ticketStatus":"Invalid","requestId":"8","result":11,"resultText":"Invalid"}

如何才能检索“有效/无效”或“0/11”,以便将用户发送到 valid.html/invalid.html(其中包含您已成功/未成功登录)。如果它是“无效”或“11”(两者之一,取决于我可以分配的内容),情况也是如此。我只是不知道应该如何选择或选择什么才能将其放入 if 语句中。这是我的代码。

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//index.html form user input
String fname = request.getParameter("firstName");
String lastName = request.getParameter("lname");
String ticketNummer = request.getParameter("ticketnr");

JsonParser parser = new JsonParser();

URL object=new URL(url);

String ticketCheck = "{\"function\":\"Check\",\"teamId\":\"IC106-2\",\"teamKey\":\"1b3741ccf6d9ec5245055370125d901e\",\"requestId\":\""+REQ_ID+"\",\"firstName\":\""+fname+"\",\"lastName\":\""+lastName+"\",\"ticketNumber\":\""+ticketNummer+"\"}";

System.out.println(ticketCheck);

HttpURLConnection con = (HttpURLConnection) object.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
con.setRequestMethod("POST");

OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
writer.write(ticketCheck);
writer.flush();

StringBuilder sb = new StringBuilder();
String jsonResponseString = sb.toString();
JsonElement jsonTree = parser.parse(jsonResponseString);

int HttpResult = con.getResponseCode();
if (HttpResult == HttpURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"));
String line;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
System.out.println("" + sb.toString());
} else {
System.out.println(con.getResponseMessage());
}







}

最佳答案

通过解析响应解决了问题。

int HttpResult = con.getResponseCode();
if (HttpResult == HttpURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"));
String line;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
System.out.println("" + sb.toString()); //the responsemessage
} else {
System.out.println(con.getResponseMessage());
}


String jsonStr = sb.toString();
JSONObject jsonObj = new JSONObject(jsonStr);
String ticketstat = jsonObj.getString("ticketStatus"); //get ticketStatus value
System.out.println(ticketstat);

if (ticketstat .equals("Invalid")){ //code here for w/e you want to do with invalid


}

}

关于java - 如果 JSON 响应结果为 0/11,如何将用户发送到 servlet 内的 .html 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54172313/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com