Whats wrong with my code for checking if a site is online or not?

78 Views Asked by At

I had been working on a app for my college project. It that application i just want to check if a website is available(online) or not. If it is available then open it in webview and if it isn't open a pre specified website.

After some research I landed up till the following code but it does not seem to work. App always opens bing.com (i.e value of flag does not get updated after running pingHost)

public class MainActivity extends Activity {

    WebView web1;
    String Address;
    int flag=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Timer repeatTask = new Timer();
        repeatTask.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        pingHost("http://www.google.com", 80, 5000);
                        if (flag==1) {
                            web1 = (WebView) findViewById(R.id.webView1);
                            Address = "https://learn2lead.home.blog";
                            WebSettings webSetting = web1.getSettings();
                            webSetting.setBuiltInZoomControls(true);
                            webSetting.setJavaScriptEnabled(true);
                            web1.setWebViewClient(new WebViewClient());
                            web1.loadUrl(Address);
                        } else if (flag==0){
                            web1 = (WebView) findViewById(R.id.webView1);
                            Address = "http://bing.com";
                            WebSettings webSetting = web1.getSettings();
                            webSetting.setBuiltInZoomControls(true);
                            webSetting.setJavaScriptEnabled(true);
                            web1.setWebViewClient(new WebViewClient());
                            web1.loadUrl(Address);
                        }
                    }
                });
            }
        }, 0, 10000);

    public void pingHost(final String host, final int port, final int timeout) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try (Socket socket = new Socket()) {
                    socket.connect(new InetSocketAddress(host, port), timeout);
                    flag = 1;
                } catch (IOException e) {
                    flag = 0;
                }
            }


        }).start();
    }
}
1

There are 1 best solutions below

3
UdayaLakmal On

Try

public class MainActivity extends Activity {

WebView web1;
String Address;
int flag = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Timer repeatTask = new Timer();
    repeatTask.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            if (isInternetAvailable()){
                flag = 1;
            }else{
                flag = 0;
            }
            System.out.println("pingHost flag: " + flag );
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (flag == 1) {
                        web1 = (WebView) findViewById(R.id.webView1);
                        Address = "https://learn2lead.home.blog";
                        WebSettings webSetting = web1.getSettings();
                        webSetting.setBuiltInZoomControls(true);
                        webSetting.setJavaScriptEnabled(true);
                        web1.setWebViewClient(new WebViewClient());
                        web1.loadUrl(Address);
                    } else if (flag == 0) {
                        web1 = (WebView) findViewById(R.id.webView1);
                        Address = "http://bing.com";
                        WebSettings webSetting = web1.getSettings();
                        webSetting.setBuiltInZoomControls(true);
                        webSetting.setJavaScriptEnabled(true);
                        web1.setWebViewClient(new WebViewClient());
                        web1.loadUrl(Address);
                    }
                }
            });
        }
    }, 0, 10000);
}

public boolean isInternetAvailable() {
    try {
        InetAddress ipAddr = InetAddress.getByName("google.com");
        //You can replace it with your name
        return !ipAddr.equals("");

    } catch (Exception e) {
        return false;
    }
}
}

Modified your posted question to working.

But implementing you should consider about that timer task close and think it really nessary 10sec period.


to check whether internet available this is much reliable and fast

public boolean isInternetAvailable() {
        try {
            int timeoutMs = 1500;
            Socket sock = new Socket();
            SocketAddress sockaddr = new InetSocketAddress("8.8.8.8", 53);

            sock.connect(sockaddr, timeoutMs);
            sock.close();

            return true;
        } catch (IOException e) { return false; }
    }

check : https://stackoverflow.com/a/27312494/1025070