My app needs to check for internet access which I successfully implemented. But I have a condition that internet is available but website it is trying to open is currently down. In this case I need to show different message as an output. How can I do so? Please give some idea.
public boolean isServerReachable()
// To check if server is reachable
{
try {
InetAddress.getByName("google.com").isReachable(3000); //Replace with your name
return true;
} catch (Exception e) {
return false;
}
}
You should check the status of the website's response Like this:
HttpResponse response = httpClient.execute(request);
int status = response.getStatusLine().getStatusCode();
and check here to find your status code.
then you can do your job by checking status code like this:
if (status == 200) // sucess
{
also I recommend you to use AsyncTask
for your connection to do communication with server in background.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With