Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I cannot use if or switch on jSONObject?

I simply have an AsyncTask that use a listener to communicate with the main activity. Where the response is arrived , and the first Toast will been shown with correct values. The problem is that in try catch I can recover the values. So if or switch don't work anymore.

@Override
public void OnAsyncTaskComplete(String response) {

    Toast.makeText(getContext(),((ActivityLogin) getActivity()).choiceButton+" result: "+response, Toast.LENGTH_LONG).show();

    try {

        JSONObject obj = new JSONObject(response);

        String objservice = obj.getString("service");
        String objstatuCode = obj.getString("status-code");
        String objdescription = obj.getString("status-description");

        JSONObject result = obj.getJSONObject("result");

        if(!objstatuCode.equals("200") || objstatuCode.equals(null)){
            Toast.makeText(getContext(), objstatuCode+"", Toast.LENGTH_SHORT).show();
            return;
        } else {
            Toast.makeText(getContext(), objstatuCode+"", Toast.LENGTH_SHORT).show();
            return;
        }

    } catch (JSONException e){
        e.printStackTrace();
    }
}

enter image description here

like image 350
Giorgio Cafiso Avatar asked Dec 18 '25 22:12

Giorgio Cafiso


1 Answers

I assume, You getting JSONException.

Why?

Thrown to indicate a problem with the JSON API

Problem coming from

 String objstatuCode = obj.getString("status-code");
 JSONObject result = obj.getJSONObject("result");

Your JSON VALUE (KEY=status-code) is int. You should rectify that.

It must be

int objstatuCode = obj.getInt("status-code");
String result    = obj.getString("result");

Then

if(objstatuCode!=200){
                Toast.makeText(getContext(), objstatuCode+"", Toast.LENGTH_SHORT).show();
                return;
            } else {
                Toast.makeText(getContext(), objstatuCode+"", Toast.LENGTH_SHORT).show();
                return;
            }
like image 173
IntelliJ Amiya Avatar answered Dec 21 '25 12:12

IntelliJ Amiya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!