How to check internet access on Android? InetAddress never times out

后端 未结 30 3524
猫巷女王i
猫巷女王i 2020-11-21 04:45

I got a AsyncTask that is supposed to check the network access to a host name. But the doInBackground() is never timed out. Anyone have a clue?

30条回答
  •  自闭症患者
    2020-11-21 05:00

    One important use case on mobile devices to it ensure an actual connection exists. This is a common problem when a mobile user enters a Wifi network with a "Captive Portal", in which they need to sign in. I use this blocking function in the background to ensure a connection exists.

    /*
     * Not Thread safe. Blocking thread. Returns true if it
     * can connect to URL, false and exception is logged.
     */
    public boolean checkConnectionHttps(String url){
        boolean responded = false;
        HttpGet requestTest = new HttpGet(url);
        HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, 3000);
        HttpConnectionParams.setSoTimeout(params, 5000);
        DefaultHttpClient client = new DefaultHttpClient(params);
        try {
            client.execute(requestTest);
            responded = true;
        } catch (ClientProtocolException e) {
            Log.w(MainActivity.TAG,"Unable to connect to " + url + " " + e.toString());
        } catch (IOException e) {
            Log.w(MainActivity.TAG,"Unable to connect to " + url + " " + e.toString());
            e.printStackTrace();
        }
        return responded;
    }
    

提交回复
热议问题