0

I have an Android application. I'm trying to do a request to my webserver and retreive the response from the server. It seems working. Now I'm checking if the response is equal to success, The equal() function worked well for a while and seems stopped working.

How do I properly check if the output is equal to success?

This is what I have tried:

if(LOGIN_AUTH.Login(usr, pwd, token).equals("success")) {
    // Server returned 'success'
    toDashboard();

}else {
    // Server returned something else then 'success'
    Alert("Error", LOGIN_AUTH.Login(usr, pwd, token));
    alert.dismiss();

    EditText password = (EditText) findViewById(R.id.login_ip_password);

    assert password != null;
    password.setText("");
}

I also added .trim() as I found this question: Android string .equals method not working. I tried that solution, but it's not working for me.

In the Login() function also trimmed the output like this:

return postResponse.trim().toString();

For some reason the application skipped the check and triggers the Alert() function. This alert showed me that the server returned success and there were no whitespaces, special characters or line-breaks found in the output.

enter image description here

8
  • then LOGIN_AUTH.Login(usr, pwd, token) is not returning what you think it is Commented Jun 23, 2016 at 9:42
  • @Blobonat "plaintext" received from the server. In my screenshot it returned "success". Also in the Android Monitor i've made an logger. Commented Jun 23, 2016 at 9:46
  • 3
    You are calling Login() twice. It may happen that the server output changed the second time. Try storing the return value in a string and debug the value. Commented Jun 23, 2016 at 9:48
  • Could you try String txt = LOGIN_AUTH.Login(usr, pwd, token); and than use txt.equals("success")? Commented Jun 23, 2016 at 9:49
  • 1
    Try using Log.d() to check what it returns. I personally don't think there's any problem with your .equals(). Like @Blobonat said, the problem probably occurs at the request part. Either there's a problem connecting to server, or the request is still running on a different thread and you check the result before the you getting it. Commented Jun 23, 2016 at 10:10

2 Answers 2

1

Try to log what

LOGIN_AUTH.Login(usr, pwd, token)

is returning. It might not return what you think it returns.

Then, the good practice is to either flip your equals condition to:

"success".equals(LOGIN_AUTH.Login(usr, pwd, token))

(to avoid getting NullPointerException when the response is null) or user Android platform's class called TextUtils:

TextUtils.equals("success", LOGIN_AUTH.Login(usr, pwd, token))

If it's still not working, log the server response (as suggested above) and post the log somewhere. I will update my answer accordingly.

Sign up to request clarification or add additional context in comments.

5 Comments

It still give me the AlertDialog with the error and message success
post the output of Log.d function where you log the arguments of LOGIN_AUTH.Login(usr, pwd, token) function and the value this function returns.
I did a test: First I entered a wrong password, Log.d() showed me the output, after I entered the right password, the Log.d() shows nothing in other words, it was not fired at all
It if shows nothing it could be that it actually shows an empty string "". You need to post the actual output from the Log.d, not just your interpretation, so we can help you. Also the code snippet would be nice. Note that I requested to log the raw server response and also the parsed one for some reason. I am sorry but we cannot help you more if you do not post the stuff I suggested.
I found out what is is.. okhttp3 stored some cache, and I'm unable to clear this cache. Because sometimes it returned success and on a moment it returns something else..
1

Maybe .contentEquals() is what you are looking for.
equals(Object o) return true/false on any type of data, depends if the content is equal or not !

contentEquals(CharacterSequence cs) returns true if and only if this String represents the same sequence of characters as the specified StringBuffer. You can read more about the difference at this link.
Also,the function is well described here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.