I get a registration response from the server. If answer 1 then login is free, if 0 then no. In the application I check this answer. But what comes to me does not want to be compared.

` String resp = null; try { db.execute("registration.php", newacc); resp = db.get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } //test.setText(resp); if (!resp.equals("0")) { startActivity(new Intent(Registration.this, Login.class)); } else toast4.show();` 

I have already tried everything I could but nothing works. If 0 arrives, the application still enters the if block and switches to another activation. Here is another PHP code

 <?php require "conn.php"; mysqli_set_charset($conn,"utf8"); if (isset ($_POST['login'],$_POST['password'],$_POST['salt'],$_POST['reg_date'],$_POST['name'],$_POST['number'])){ $login= $_POST['login']; $pass= $_POST['password']; $salt= $_POST['salt']; $reg_date= $_POST['reg_date']; $name= $_POST['name']; $number= $_POST['number']; $querry0 = mysqli_query($conn,"SELECT login FROM account WHERE login = '$login'"); if(mysqli_num_rows($querry0) == 0){ $querry1 = mysqli_query($conn,"INSERT INTO account (login, password,salt, registration_date) VALUES ('$login', '$pass','$salt', '$reg_date')") or die(mysqli_error($conn)); $querry2 = mysqli_query($conn,"INSERT INTO client (client_name, phone_number, email) VALUES ('$name', '$number','$login' )") or die(mysqli_error($conn)); echo "1"; }else echo "0"; }else echo "err"; ?> 

enter image description here

Translated strings to HEX: the string from the server is efbbbf30, and with which I compare - 30. With all the encoding everywhere in UTF-8. How to deal with this?

  • After ?> Perhaps there is a newline character, so the line may turn out not "0" , but "0\n" - andreymal
  • There are spaces before the <?php , so the string is not even "0\n" , but " 0\n" - andreymal
  • After?> There is no transfer, and before <? accidentally put spaces on the site - Nikita Ragutkin
  • Perhaps your text editor simply does not show the transfer, but it actually is. Erase ?> general, it is not necessary - andreymal
  • one
    Who prevents character-by-character (and better in hex) to output each line and compare them with your eyes? - PinkTux

2 answers 2

php adds BOM (Byte Order Mark) to Unicode. Java, and therefore Android, when decoding, does not delete it (I don’t know why), and you have to do it yourself.
In your case, you need one character (as I understood it) and you can only compare it:

  if (resp.charAt(resp.length() - 1) != '0')) { startActivity(new Intent(Registration.this, Login.class)); } else toast4.show(); 

    In general, I now simply compare strings in HEX. A crutch, but it works.