Whats wrong with my code for checking if a site is online or not in android app? : 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();
    }
}

PLEASE HELP ME OUT ASAP... I AM SO CLOSE TO DEADLINE FOR SUBMISSION OF PROJECT!!!!