To find out if you have internet, you need to do something more than check if you have a network connection. Because if you are connected to a slow network, portable portal or VPN, you have a connection to the network, but there is no real Internet or the Internet used.
That is why you still need to check if you have internet / used internet before you make an HTTP request by adding a simple ping to the server or Google (because Google is 99.99% of the time). You can also do this periodically or when you catch the first exception you need.
public Boolean isInternet() { try { Process process = java.lang.Runtime.getRuntime().exec("ping -c 1 www.google.com"); int returnVal = process.waitFor(); boolean reachable = (returnVal==0); return reachable } catch (Exception e) {
You can also use a different method, but the general idea is the same as you know, almost always online, and check if you can achieve it.
However, continue to handle exceptions, because there is no way to always catch it, and having a backup way of handling things cannot damage
source share