简体   繁体   中英

Toggle doesn't work - java

I am trying this to toggle the value of key1 in but I get the same value all the time. What am I doing wrong here?

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";

    }

Am I resetting the " key1 " boolean value somewhere?

you are creating new JSON object every time:

JSONObject jsonObj = new JSONObject();

so it never has any value for "key1" and it is always set to true after your hello code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM