0
This problem occurs only on Android 7!
In other versions the operation is perfect. And performing the tests on an S8 with the intelligent network exchange, by the reports of a friend, is also working as it should.
Mine is a G5 Plus and has no way to turn off the smart network switch.
At certain times it connects but mostly not and the error returned in Ioexception is Network is unreachable.
The piece of code the error is running on:
Socket clientSocket = new Socket();
try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
clientSocket.connect(new InetSocketAddress(serverAddr, SERVERPORT), 20000); //<- aqui gera o erro
main.socket = clientSocket;
} catch (UnknownHostException e1) {
e1.printStackTrace();
Log.d("Artur",e1.getMessage());
} catch (IOException e1) {
e1.printStackTrace();
Log.d("Artur","Erro:"+e1.getMessage());
}
Have you seen if there is a watch to detect when a network change occurs? Try to put the socket as global and in the network exchange you close and open the connection again.
– Hiago Souza
I’ll try. But I think I already solved. There is no problem in the code. It is Android that disconnects alone and falls on the cellular network. It checks that my device does not have internet and looks for the connection elsewhere. On other phones just turn off the smart network switch option. But on Moto G5 Plus there is no such option. So it will never work but it’s not because of the program.
– Artur Gaspar
I was able to use this solution: https://stackoverflow.com/questions/42329775/using-a-wifi-without-internet-connection/46165010#46165010
– Artur Gaspar