Error making android HTTP request

Asked

Viewed 179 times

0

I’m implementing a native android app that requests with a WS. When trying to make the connection in the method connect() of HttpURLConnection I’m getting an error that doesn’t fall into the catch to be validated.

Doing some research I found that the error occurs by the port I reported in my connection URL because my server is local. I set the door number 80886 for example. With this I received the error java.lang.IllegalArgumentException: port=80886. Researching a little more understood that the maximum number of accepted ports is 65535 and in fact if I put the door 65536 I get the same error mentioned earlier, a smaller port not problems.

So far it is even understandable because I saw that it has connection with the TCP protocols and this magic number 65535 is actually the maximum number since it is the calculation of 2 16. Since port identifiers are 16-bit integers, this is the maximum accepted value.

I tested using 2 different devices. One with version 4.4.4 of android and another with version 8.0.0. On device with version 4.4.4 error occurred, already on android 8.0.0 had no problems.

Could you explain to me why this happened? Why error only occurs in old version?

Method Code:

public static boolean testarConexaoServidor(String url) {

    boolean retorno = false;
    int milissegundos = 20000;

    try {
        URL apiEnd = new URL(url);
        int codigoResposta;
        HttpURLConnection conexao;

        conexao = (HttpURLConnection) apiEnd.openConnection();
        conexao.setRequestMethod("GET");
        conexao.setReadTimeout(milissegundos);
        conexao.setConnectTimeout(milissegundos);
        conexao.connect();

        codigoResposta = conexao.getResponseCode();
        if (codigoResposta < HttpURLConnection.HTTP_BAD_REQUEST) {
            retorno = true;         
        }

        conexao.disconnect();


    } catch (MalformedURLException e) {
        e.printStackTrace();

    } catch (IOException e) {
        e.printStackTrace();
    }

    return retorno;
}
  • Code... if you can, of course

  • Inserted in the question for better understanding and reading. Thank you Woton. And to ratify, the error occurs as soon as the line "connected.connect();" is processed. It does not pass it and the fatal error in the app in versions 4.4 of android. I have not tested in previous. In 6.0 and 8.0 it worked correctly.

  • What exactly is the question? There is no TCP port or UDP with number above 65535, so the reason for the error is already clear.

  • I’m sorry if the question is unclear. What I wanted to know is why "java.lang.Illegalargumentexception: port=80886" error occurs, causing fatal application error only on android version 4.4 and not on versions 6.0 or 8.0?

No answers

Browser other questions tagged

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