0

I am getting response from php to android, see below .php

 <?php
 $username = $_POST[username];
 $password = $_POST[password];
 if($username == "himm")
 {
if($password == "rangde"){
        echo "success" ;
}
else{
    echo "passwor is wrong.";
}
  }
 else
 {

echo "fail";
  }
  ?>

i am getting success in logcat window of android. But here in android i have made comparison like below,

  BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
  StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
           sb.append(line + "\n");
    }
    is.close();
    result = sb.toString();
    Log.v("","Result : "+result);
    Log.v("","this is Result: "+result.equalsIgnoreCase("success"));
    if(result.equals("success")){
        Log.v("","Login successfully......");
}
else{
        Log.v("","Fail to login......");
    }

but in logcat window i see "fail to login" message. Php send response as "success" but which type ? Here condition of if(result.equals("success")) should be true. Please any body give me idea or suggestions to achieve thies..........thank you in advance

6
  • You should also see 'result' value in logcat... Which is... ? Commented Aug 30, 2012 at 14:01
  • do you want to authenticate the user of android from php script? Commented Aug 30, 2012 at 14:03
  • yes i want to make authentication here... Commented Aug 30, 2012 at 14:05
  • and when i want print result in logcat it print success Commented Aug 30, 2012 at 14:06
  • It actually prints success\n. See my answer for a more detailed explanation. Commented Aug 30, 2012 at 14:07

3 Answers 3

1

sb.append(line + "\n"); modifies the 'success' to 'success\n' so the if(result.equals("success")){ fails because 'success\n' does not match 'success'.

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

5 Comments

sb.append(line + "\n"); result = sb.toString(); i update this statement and after i have pring Log.v("",""+result.equals("success")) but it still get false
Yes, \n is still part of the string after the sb.toString() function. toString() does not remove the \n from the string. Try using result = sb.toString().trim().
Log.v("","Result : "+result.trim().equals("success")); it also getting false
Modify: result = sb.toString(); to result = sb.toString().trim();. If you only edit the part you pasted in your comment, you won't make any changed to the result variable so it would not be saved if you use the result variable in the if-statement.
ya...........thank you very very much i got it.......thanks again result = sb.toString().trim(); this statement worked.
1

In your android code, you add a trailing LineFeed to the result you receive from php :

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

So you actually compare 'success' to 'success\n' which is false.

3 Comments

to complete the answer, either don't add trailing line feed or String.trim() the result before comparing it.
Log.v("","Result : "+result.trim().equals("success")) also not worked getting false here
it possible by this statement result = sb.toString().trim(); thank you to suggest me......
0

In addition to previously posted answer (which are correct), I would like to point out that HTTP contains mechanisms to do what you are trying to do.

Http authentication allows you to use standard Http return codes (200 in case of success, 401 in case of authentication failure) and to use existing systems to handle that part (most frameworks will provide such).

It also allows you to separate authentication from the rest of the message you send back from the server, and to compare the authentication status at soon as you receive the headers from the server.

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.