0

I'm sending a username and password to a website for authentication purposes, after all is said and done and I've retrieved the results from the server, I've placed the results in a variable called 'response' To this point everything is working correctly

response = sb.toString();
Toast.makeText(this,"Returned Value: "+ response,0).show();

The value seen in the above Toast is the value being returned by the php script. I've used both a valid user and an invalid user and the Toast displayed above shows the correct value (i.e. "Good Login" or "Login Failed") returned by the server. I want to test for those results so I can start the appropriate activity so I've put in some test "if" statements

if("Good Login".equals(response)){
 Toast.makeText(this, "Registered User" + mUsername, 0).show();
 }
if("Login Failed".equals(response)){
 Toast.makeText(this, "Sorry You're Not A Registered Subscriber",0).show();
 }

I'm getting nothing from either one. I've also tried

if(response.equals("Good Login")){
 Toast.makeText(this, "Registered User" + mUsername, 0).show();
 }
if(response.equals("Login Failed")){
 Toast.makeText(this, "Sorry You're Not A Registered Subscriber",0).show();
 }

With the same results. Not sure what else to test for. Is there a better way to test for success or failure?

Thanks

Hank Wilson
  • 509
  • 12
  • 31
  • 1
    Have you debugged your code to see what **response** is when returned? – bschultz Mar 06 '12 at 20:49
  • 1
    have you tried checking the actual value of response? Is it one of "Good Login" or "Login Failed"? Could it be an untrimmed string? such as "Good Login " or " Login Failed"? – Jacob Eggers Mar 06 '12 at 20:49
  • It might be an encoding issue, take a look at this question: http://stackoverflow.com/questions/4210713/equal-and-equalsignorecase-return-false-for-equal-strings – MByD Mar 06 '12 at 20:50
  • Have you tried a Sysout of your `response`? What does it show? – Guillaume Polet Mar 06 '12 at 20:50

3 Answers3

2

Debug (or print) the exact value of the response variable.

It is likely that there are whitespaces, so you may need to have response = response.trim()

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • It was the trim(), once I trimmed it, everything worked Thanks – Hank Wilson Mar 06 '12 at 20:55
  • @Hank good. Remember to mark the answer as accepted. And also remember to debug your programs if they don't work ;) – Bozho Mar 06 '12 at 21:02
  • @Hank - when you output like your `Toast.makeText(this,"Returned Value: "+ response,0).show();` it helps to surround the output string with something -- I use square brackets -- `Toast.makeText(this,"Returned Value: ["+ response+"]",0).show();` then you'll see the leading and trailing whitespace. – Stephen P Mar 07 '12 at 01:02
0
  1. I would return an integer error code rather than some string to check the error response.
  2. Make sure you are returning the correct case, otherwise use equalsIgnoreCase
Sid
  • 7,511
  • 2
  • 28
  • 41
0

The Java string equals function is fully case/spacing sensitive compare.

So if:response = "Good Login " or if it contains extra-spaces or non-printing characters then it will fail the test.

It would be a good idea to strip all whitespace, even the internal ones. Here's a SO question about doing just that. Also use String.equalsCaseInsesitive() when doing that actual comparison.

Community
  • 1
  • 1
Mark Robinson
  • 3,135
  • 1
  • 22
  • 37