• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 515
  • Last Modified:

Compare strings in a Android application

Hi Experts, I am trying to make a comparison between a string and a value taken online in my android application, but I can not.

My code is something like this:

HttpClient client = new DefaultHttpClient();
                              HttpGet request = new HttpGet(URL+login.getText()+"&passwd="+passwd.getText());
                              HttpResponse response = client.execute(request);
                              HttpEntity entity = response.getEntity();
                              if (entity != null) {
                                    InputStream in = entity.getContent();
                                    String resultado = convertStreamToString(in);                  
                                    if (resultado.equals("ok")) {
                                          Log.i("Teste","--------------------------------");
                                          Log.i("Teste","Login sucess");
                                          Log.i("Teste","--------------------------------");
                                    } else {
                                          Log.i("Teste","--------------------------------");
                                          Log.i("Teste","Login failed");
                                          Log.i("Teste","--------------------------------");
                                    }
                              }

My problem is in line "if (resultado.equals("ok")) {", because even when resultado value is "ok" the application show me "Login failed". How to comppare properly these strings ?

I also noticed that the compiler shows me the message:

"10-10 14:49:29.135: INFO/global(737): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required."

is this a problem in my code ?

Thanks
Alexandre


0
alpires
Asked:
alpires
1 Solution
 
CEHJCommented:
I don't know that api, but an http response is a digit before anything else. I'd be very surprised if "ok" alone is returned...
0
 
CEHJCommented:
You probably want (but don't use the magic number as i did)
if(200 = response.getStatusLine().getStatusCode()) {
	// Logged in
}
else {
	// Not logged in
}

Open in new window

0
 
CEHJCommented:
Aargh!
if(200 == response.getStatusLine().getStatusCode()) {
	// Logged in
}
else {
	// Not logged in
}

Open in new window

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
stachenovCommented:
Are you sure that the value is strictly "ok", no newlines or spaces inside? It looks like you're comparing strings correctly, I don't think in Android it's any different, although I don't really know anything about it.

You could try
resultado.trim().equals("ok")

Open in new window


>> I don't know that api, but an http response is a digit before anything else. I'd be very surprised if "ok" alone is returned...

I don't know the API either, but judging by the InputStream presence, it is the response body, not the raw HTTP response. "ok" seems like a response in a custom application protocol.
0
 
for_yanCommented:
    String resultado = convertStreamToString(in);    

Just printout resultado and see what it retrrns:
either do
System.out.println("resultrtado: " + resultado);  

or put it to the Log just after

String resultado = convertStreamToString(in);    

So you'll know what kind of result is returned.
           
0
 
for_yanCommented:

"10-10 14:49:29.135: INFO/global(737): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required." - this looks like a warning and probably is not the reason for the problem;
so just find out experimentally what it is that is returned - even "ok" may be in upper case say "OK" - so you may try
equalsIgnoreCase() or soemthing like that, but better printout - it is difficult to guess
0
 
alpiresAuthor Commented:
CEHJ, in "resultado" I get the body of my page with "ok" or "error", so I can not get only the status.

stachenov, the command trim() worked perfectly here.

Thanks Guys
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now