The server receives the answer: 0, 1, 2 (with the lines tried and still without result).

if (response.getStatusLine().getStatusCode() == 200) { HttpEntity entity = response.getEntity(); responseLogin = EntityUtils.toString(entity); responseLogin = String.valueOf(responseLogin); } 

Then, depending on the response, actions are performed.

  switch (responseLogin) { case "0": //code break; case "1": //code break; case "2": //code break; default: done = false; Toast.makeText(getApplicationContext(), "Response " + responseLogin, Toast.LENGTH_LONG).show(); break; } 

The value is, but for some reason does not want to compare.

  • responseLogin must be an integer value, and caseing is also necessary for intams. Support for strings is not everywhere added yet. If you don’t want to leave the lines, then use equals - Android Android
  • spaces, line breaks? .. - Yura Ivanov
  • Tried with inatmi, int temp = Integer.valueOf(responseLogin);switch (temp) , exits java.lang.NumberFormatException: Invalid int: "0" . There are no gaps - Vladimir Saleev
  • one
    when using int quotes should be removed - iksuy
  • those. in fact, the server does not receive 0 , but "0" (zero in quotes, three characters)? Print the responseLogin to the log, but see what's inside. - zRrr

3 answers 3

Try specifying integer variants in the switch and parsing the value from the string. ( Integer.parseInt (responseLogin) )

 String responseLogin = //значение... responseLogin = EntityUtils.toString(entity); responseLogin = String.valueOf(responseLogin); // зачем? ... switch (Integer.parseInt(responseLogin)) { case 0: //code break; case 1: //code break; case 2: //code break; default: done = false; Toast.makeText(getApplicationContext(), "Response " + responseLogin, Toast.LENGTH_LONG).show(); break; } 
  • This is the FATAL EXCEPTION: main java.lang.NumberFormatException: Invalid int: "0" error FATAL EXCEPTION: main java.lang.NumberFormatException: Invalid int: "0" responseLogin = String.valueOf (responseLogin); added to show once again what is a line - Vladimir Saleev
  • Perhaps the quotes are part of the string passed to the method. Try going back to the text case version, just make options in the form "\" 0 \ "", "\" 1 \ "", "\" 2 \ "" ... This is for debugging. If it works, just clear the line of all "non-digits". - DimXenon
  • Does not work :( In the line "\uFEFF0" , you need to somehow remove \uFEF - Vladimir Saleev
  • one
    The symbol "\ uFEFF" is added to files saved in the format "UTF-8 with BOM", to avoid this, you need to use the format "UTF-8 without BOM". If a character is added not to data from a file or form, but to the data entered by hands, it may be possible to help remove all the problematic characters from the line before passing to the switch: char bom = '\ uFEFF'; String aaa = "" + bom + bom + bom; System.out.println (aaa.length ()); String replaceAll = aaa.replaceAll (bom + "", ""); System.out.println (replaceAll.length ()); - DimXenon
  • one
    Yes, you can not modify the string by referring to responseLogin.substring (1), as you did, to the second character in the string. In the case when you need to cope with a large string, I hope you will find it useful to use replaceAll. - DimXenon

Through debag saw "\uFEFF0" . It was necessary to cut the string switch(responseLogin.substring(1))

    It is not clear why the above code does not work for you

     String responseLogin = //значение строка responseLogin = EntityUtils.toString(entity);//здесь объект как строка т //т е тип Строка ... /*Здесь из тип Строка переводим в тип Целое,но с тем же значением.А потом по типу Целое,по его значению выполняем ту или иную ветку */ switch (Integer.parseInt(responseLogin)) { case 0: //code break; case 1: //code break; case 2: //code break; default: done = false; Toast.makeText(getApplicationContext(), "Response " + responseLogin, Toast.LENGTH_LONG).show(); break; } 

    It is definitely necessary to work with the type Integer. If the responseLogin contains exactly “0”, the quotation mark character, 0 character, quotation character, then it is necessary to clear the substing to 0 character.