1

I use json to get a string data from php and i want compare this data whit a string ,even when they are equal but returns false for example

php file:

<?php
   echo "ok";
?>

java use 3 log

BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();

String line = null;
while ((line = reader.readLine()) != null)
{
    sb.append(line + "\n");
}
is.close();
page_output = sb.toString();

Log.i("page_output",  page_output);

Log.i("page_output",  String.valueOf(page_output=="ok"));

Log.i("page_output",  String.valueOf(page_output.equals("ok")));

log out

07-12 03:42:45.616: I/page_output(2007): ok
07-12 03:42:45.736: I/page_output(2007): false
07-12 03:42:45.736: I/page_output(2007): false

you can see that page_output is ok but returns false

2
  • Q: Shouldn't you be using String.equals()???? Commented Jul 12, 2015 at 7:36
  • lik this page_output.equals("ok") but not change returns false | Log.i("page_output", String.valueOf(page_output.equals("ok"))); -> 07-12 03:23:35.936: I/page_output(1820): false Commented Jul 12, 2015 at 7:41

2 Answers 2

3

Try this:

Log.i("page_output",  String.valueOf(page_output.trim().equals("ok")));

You are appending "\n" at the end of the every line read from your php response. So you're actually comparing "ok\n" with "ok" and that's why the comparison returns false.

The trim() function removes white spaces, including new lines \n.

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

Comments

1

in this line:

sb.append(line + "\n");

You add the "\n" new line character to your StringBuilder. When you call toString this character will also be put in your String.

Therefore, page_output.equals("ok\n") will be true.

Or you could just delete the '\n' character from the StringBuilder since I do not see any practical usage in your code

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.