Getresponse - Httpurlconnection

Asked

Viewed 82 times

1

inserir a descrição da imagem aquiI’m trying to make a comeback getResponse. I can see the return with the System.out.println, but I can’t get the same feedback within the application.

HttpURLConnection conexao = (HttpURLConnection)url.openConnection();
            conexao.setDoOutput(true);

            BufferedReader in = new BufferedReader(new InputStreamReader(conexao.getInputStream()));

             String inputLine = "";
             while ((inputLine = in.readLine()) != null)
                 System.out.println(inputLine);
             lblStatus.setText(inputLine);


             String respostaenvio = SomenteNumeros(inputLine);

             double resposta = Double.parseDouble(respostaenvio);

             if(resposta > 100000){
                 System.out.println("Enviado");
             }else{
                 System.out.println("Não Enviado");
             }
             in.close();
             conexao.disconnect();
            }

Can you help me understand what’s wrong?

I need the information reply sent to determine whether an SMS has been sent.

On the line System.out.println(inputLine); prints exactly the code I need, but when I try to print this same code into a JOptionPane, for example, will not.

  • You want to set the value in inputLine and then display on the label?

  • What is that method SomenteNumeros ago?

  • the Somentenumeros method changes the received value to only numbers, since in return, there are other characters.

  • Laerte, exactly that. set the value and display on a label or Joption

1 answer

1


Look at your while:

String inputLine = "";
while ((inputLine = in.readLine()) != null)
    System.out.println(inputLine);
lblStatus.setText(inputLine);

String respostaenvio = SomenteNumeros(lastLine);

What is the necessary condition to exit the while? It’s when the line reads, that’s on inputLine for null. This means that after the while be finalised, the inputLine will be at all times null, and that null will be placed on the label and passed to the method SomenteNumeros.

I think what you wanted is this:

String inputLine = "";
String lastLine = "";
while ((inputLine = in.readLine()) != null) {
    lastLine = inputLine;
    System.out.println(lastLine);
}
lblStatus.setText(lastLine);

String respostaenvio = SomenteNumeros(lastLine);

I mean, I have a new variable lastLine which represents the last line read and valid, and not just simply the last line read (which will always be null in the end).

  • Victor, Thanks for the reply. I tried the way I reported, but still did not get a return on the status label. I attached an image to the question. See that below the test line, there is a number. This is the number I need on a label or Joptionpane

  • @Wagnerfilho But in your screenshot you don’t have the line lblStatus.setText(lastLine); right after the end of while, different from what is here in the answer.

  • Victor My mess.. Perfect friend, it worked.. Ow. Thanks so much for the help..

  • @Victormancada Can I ask one last question? Regarding the above code. How can I put a third if condition to return if the user’s IP is blocked to use the application. So far I have two conditions, where if the answer is greater than 10000, sent, if not, not sent. Only the application has another return, in case ACCESS DENIED. So how do I get this answer in a Joptionpane.show..?

  • I got @Victormancada. if (lblStatus.gettext(). contains("ACCESS DENIED")) { System.out.println("IP is not released, please contact the system administrator"); }Else{ String replenishment = Somentenumeros(lastLine);...

  • @Wagnerson Instead of wearing if (lblStatus.getText().contains("ACESSO NEGADO")) I would wear if (lastLine.contains("ACESSO NEGADO")). Thus it becomes easier and the logic of the download operation is less coupled with the logic of the screen elements.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.