简体   繁体   中英

Getting response in android from PHP

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

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM