1
if (resEntity != null) { res=EntityUtils.toString(resEntity); Log.i("RESPONSE",res); } xc=res.equals("true"); Log.i("xc",String.valueOf(xc)); if(xc) { Log.i("Yes","1"); } else { Log.i("no","0"); } 

above is my code in which i am sending a post request to our company server for checking a file it returns true if the file exists.I am providing my logcat.

09-24 11:20:19.570: I/url for checking update(3050): http://www.digitalmarketingbox.com/webtool/playerupdate.php 09-24 11:20:33.789: I/RESPONSE(3050): true 09-24 11:20:33.789: I/xc(3050): false 

as you can see,the response returned from server is true and i am also comparing it to true,so why equals() function is returning false ,please help me,or am i doing something wrong here?

4
  • 3
    Is there possibly any whitespace following the response? Commented Sep 24, 2013 at 5:57
  • Please fix your indentation and blank lines when posting code - this code is far harder to read than it should be. Commented Sep 24, 2013 at 5:58
  • What do you get if you log res.length()? Commented Sep 24, 2013 at 6:05
  • What does your EntityUtils.toString(resEntity) return? Commented Sep 24, 2013 at 6:07

4 Answers 4

3

I think the most likely problem is whitespace in res. Try this:

res=EntityUtils.toString(resEntity).trim(); 
Sign up to request clarification or add additional context in comments.

3 Comments

ok this worked i will accept this answer thank you so much can you explain me in short what trim does
Check the API of String.trim()
@newkid - trim() removes all leading and trailing whitespace in a string and returns the result as a new string.
0

It seems you are getting the boolean response and you trying to compare it using the String equals() method, which is not the correct way of doing it.

EDIT: As res is actually a string value so you need to make sure that it actually contains "true" without any extra spaces around.

3 Comments

Considering res is gotten from a toString call, it's hard to see how res is a boolean here.
yes and it is returning true,in my logcat you can check the value of RESPONSE tag for res,it is returning true
@newkid have u tried to use trim on res before checking like if "true".equals(res.trim()); ?
0

Ya mey be you have space that why result is not in your favour So use trim to remove the space and then use like tha

 xc=res.trim().equals("true"); if(xc) { Log.i("Yes","1"); } else { Log.i("no","0"); } 

3 Comments

There's no comparison in that statement; it's an assignment.
xc is boolean res and "true" are Strings. Everybody is giving the same answer but I think this is incorrect.
@newkid no where in the question u have mentioned that xc is boolean.So u better go and read ur question carefully first
-1

You can check like this.

 if(res.equals("true") { Log.i("Yes","1"); } else { Log.i("No","0"); } 

4 Comments

How is that going to produce anything different?
it is not any different and i tried this before also and the control goes to else but the if block should be executed
Result is getting in that "res"..With that itself, can check the condition instead of converting into Boolean as he did in his code.
If you try this code, can u pls print what is the value you are getting in res

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.