String comparison does not work

0

I have a string that currently only receives "ERROR" and I made an if for when it gave me this value I executed some things, it happens that the comparison always fails, even if the strings being exactly the same, I always compared string that way, no I understand why it is not working:

                           try {
                                String mensagem = response.body().getAsJsonObject().get("error").getAsJsonObject().get("msg").toString();
                                String status = response.body().getAsJsonObject().get("status").toString();
                                //Log.i(TAG, "onResponse: "+mensagem);
                                Log.i(TAG, "onResponse: "+status);
                                if(status.equals("ERROR")){
                                    Toast.makeText(SplashScreen.this, mensagem, Toast.LENGTH_SHORT).show();
                                    habilitarformgerarsenha(true);
                                }else{
                                    Log.i(TAG, "onResponse: Status mensagem:" + status);
                                }
                            }catch (NullPointerException e){
                                Log.i(TAG, "onResponse: "+e);
                            }

LOG:

02-05 23:57:41.036 30711-30711/1.com.br.doup I/igr: onResponse: "ERROR"
02-05 23:57:41.036 30711-30711/1.com.br.doup I/igr: onResponse: Status mensagem:"ERROR"
    
asked by anonymous 06.02.2018 / 00:58

1 answer

1

From what appears in the Log:

  

onResponse: "ERROR"

The String that has in status already has a quotation mark inside which causes if :

if(status.equals("ERROR")){

Never give true.

To resolve you can change the if to include the quotation marks too:

if(status.equals("\"ERROR\"")){

Note that they had to be included with \" in order to escape the initial quotation marks.

Or another alternative would be to remove quotation marks that are already at the expense of substring :

status = status.substring(1, status.length()-1); //remover as aspas
if(status.equals("ERROR")){ //if normal

substring used from the second character, in position 1, to the penultimate, given by length - 1

    
06.02.2018 / 02:00