切换不起作用-java
我正在尝试切换key1的值,但我总是得到相同的值。我做错了什么切换不起作用-java,java,toggle,Java,Toggle,我正在尝试切换key1的值,但我总是得到相同的值。我做错了什么 boolean jsonText = true; JSONObject jsonObj = new JSONObject(); public void setjsonObject(boolean val){ this.jsonText = val; } public Boolean getjsonObject(){ return this.jsonText; } @POST("/hello") @Pe
boolean jsonText = true;
JSONObject jsonObj = new JSONObject();
public void setjsonObject(boolean val){
this.jsonText = val;
}
public Boolean getjsonObject(){
return this.jsonText;
}
@POST("/hello")
@PermitAll
public String hello(String text) {
try {
if (jsonObj.has("key1")) {
boolean val = !jsonObj.getBoolean("key1");
jsonObj.remove("key1");
jsonObj.put("key1", val);
} else {
jsonObj.put("key1", true);
}
} catch (JSONException e) {
e.printStackTrace();
}
return "some result";
}
我是否在某处重置了key1布尔值?您每次都在创建新的JSON对象:
JSONObject jsonObj = new JSONObject();
因此,它对key1没有任何值,并且在您的hello代码之后总是设置为true