I'm implementing a native android app that requests with a WS. When trying to make the connection in method connect()
of HttpURLConnection
I'm getting an error that does not fall into catch
to be validated.
Doing some research, I discovered that the error is due to the port that I reported in my connection URL because my server is local. I have set the door number 80886 for example. With this I received the error java.lang.IllegalArgumentException: port=80886
. Searching a little more I understood that the maximum number of ports accepted is of 65535 and in fact if I put the 65536 port I get the same error mentioned previously, a smaller port does not of the problems.
So far this is understandable since I have seen that it has a connection with the TCP protocols, and that magic number 65535 is actually the maximum number since it is the calculation of 2 ^ 16. Because the port handles are 16-bit integers, this is the maximum value that is accepted.
I took the test using 2 different devices. One with android version 4.4.4 and another with version 8.0.0. In the device with version 4.4.4 the error occurred, already in android 8.0.0 I had no problems.
Could you explain why this happened? Why does an error occur only in the 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;
}