我正在尝试通过调用 getSessionKey 方法创建一个以json格式从服务器返回响应的应用程序。响应应该是这三个参数。消息,状态和 sessionKey 格式
{
"message": "Successful",
"status": 0,
"sessionKey": "f85d1246c98f11e"
}
这是java代码。
private static String sessionKeyResponse(){
try(CloseableHttpClient httpClient =HttpClientBuilder.create().build()) {
String url="http://myurl.com:8080/getSessionKey";
String body="";
HttpPost request = new HttpPost(url);
StringEntity params = new StringEntity(body);
request.addHeader("content-type", "application/json");
request.setEntity(params);
HttpResponse result = httpClient.execute(request);
String json = EntityUtils.toString(result.getEntity(),"UTF 8");
com.google.gson.Gson gson = new com.google.gson.Gson();
Response respuesta = gson.fromJson(json, Response.class);
System.out.println(respuesta.getMessage());
System.out.println(respuesta.getStatus());
System.out.println(respuesta.getSessionKey());
} catch (IOException ex) {
}
return "";
}
@JsonPropertyOrder({ "message","status","sessionKey" })
public class Response{
private String message;
private String status;
private String sessionKey;
public String getMessage() {
return message;
}
@JsonProperty("message")
public void setMessage(String message) {
this.message = message;
}
public String getStatus() {
return status;
}
@JsonProperty("status")
public void setStatus(String status) {
this.status = status;
}
public String getSessionKey() {
return sessionKey;
}
}
当我在位置1运行应用意外令牌END OF FILE时,这是我从服务器获得的响应。
任何回应都会受到影响。