fizo07
fizo07

Reputation: 625

Android string .equals method not working

I'm learning android development. Ive been able to successfully make a login class, which my application checks my mysql database to see if the user's details exists in the DB. The php file posts either "correct" if the user's details exist in the DB or "incorrect"

private void checkResult(){
    if(getResults().equals("correct")){
            //do some stuffs

    }
    else{

        displayDialog(getResults());
    }   
}

The getResults() method, returns the String (response from the server). However, the string comparison doesn't seem to be working because when the server returns "correct" as the response, the if condition always comes out as false. I confirmed that by using the method below,the false statement is executed, but the msg in the alertDialog is "correct". Please can anyone help, cant seem to understand why the code's misbehaving.

    public void displayDialog(String msg){

    AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle("Authetication Failed!");
    alertDialog.setMessage(msg);
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // do something when the user presses OK (place focus on weight input?)
        }
    });
    alertDialog.setIcon(R.drawable.icon);
    alertDialog.show();

}

Upvotes: 2

Views: 7145

Answers (3)

Nikola Despotoski
Nikola Despotoski

Reputation: 50538

Maybe the response contains some <br blaa bla html tags. This is my first though of it. Log the output. Log("RESPONSE", getResults());

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500535

I strongly suspect that the string contains "invisible" characters, e.g. Unicode U+0000 or perhaps a newline.

I suggest you diagnose this by logging the length of the string and the Unicode value of each character:

String results = getResults();
for (int i = 0; i < results.length(); i++)
{
    // Whatever the relevant log call is
    log("Got character: " + (int) results.charAt(i));
}

It seems far more likely that this is the problem than that String.equals is really broken.

Upvotes: 5

Pikaling
Pikaling

Reputation: 8312

I've had the same problem before. Try getResults().trim().equals() instead

Upvotes: 5

Related Questions