Good day, when developing android applications faced with a number of problems related to POST requests. Most of them I decided myself and with the help of this site. And when everything seemed to be done, another problem appeared, namely after I received a response from the server after a POST request. The answer looks like a digit from 0 to 23, then I can’t do anything except display it. I get a response from the server like this:

String bTime = EntityUtils.toString(response.getEntity(), "UTF-8"); 

or this method also works:

 bTime = convertStreamToString(response.getEntity().getContent()); //...... public static String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line);// + "\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } 

I tried to compare the received String with another String by the function .equals (str), but nothing happened, the comparison is always false, I tried to turn the String into an int by the function int BeginTime = Integer.parseInt (bTime.toString ()), Integer.parseInt (bTime) , int Time = Integer.valueOf (bTime.toString ()), Integer.valueOf (bTime), but when trying to convert a string to a number, the application threw out with an error.

Thus, I have a response from the server, but I can do nothing with it, except for the output to the screen. I ask you to tell what to do and how, in the end, I wanted to use this answer so that I could apply the comparison function to equality with another digit on it. Thank you in advance.

  • And what kind of error when parse to number? Can any hidden characters spoil everything (space, line break, etc.)? - Chad
  • I send the answer just a number like this echo ($ daybegin), where $ daybegin = 3 when parsing this error java.lang.RuntimeException: Unable to create service ru.bazanski.tracker.beta.ServiceGPS: java.lang.NumberFormatException: unable to parse '? 3' as integer I don’t understand where this question mark comes from, since it’s not displayed on the screen, there is only a number 3. It’s also not when eclips shows the error itself, appeared after saving the error log as a file, if I can throw off all log file - bazanski
  • the character is unprintable, so you don’t see it. log response.getEntity().getContentLength() . set breakpoint in eclipse before converting, see what you have in bTime in the zero character. From php climbs garbage, apparently. - Yura Ivanov
  • As a patch, kill the regexp from the server response all "not numbers" :-) But in general you need to get where it comes from. Maybe in php that accidentally came across ... what a line feed, etc. ... - Chad 2:51 pm
  • @ Chad or rather tell me, but better show how to use this regex, I'm new to java big. - bazanski

1 answer 1

You can try something like this:

 Integer.parseInt(bTime.replaceAll("([^0-9])",""); 

About regekspa not sure one hundred percent.

  • gut and if the logs climb and we cut them so? as a result, the user will receive "your order for 2007-20123 rubles sent." cool - Yura Ivanov
  • Well, logs, in principle, are not so necessary ... It is necessary or to handle server errors humanly or to beat a tambourine :) - Chad
  • one
    First of all, you need to figure out the server so that it works as expected. The function of receiving the text of the answer is correct, it means php php cure the effects - dead poultices. - Yura Ivanov
  • So I say: 'As a patch, kill the regexp' - Chad
  • one
    Thanks, the patch helped) For the time being let it be, but I will start watching PCP and look for a mistake. - bazanski