Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANDROID Check condition for server down

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.

like image 856
A_rmas Avatar asked Dec 26 '22 05:12

A_rmas


2 Answers

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;
        }
    }
like image 114
Seshu Vinay Avatar answered Dec 28 '22 09:12

Seshu Vinay


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.

like image 21
SerCna Avatar answered Dec 28 '22 11:12

SerCna