Detecting limited network connectivity in Android?

前端 未结 2 373
独厮守ぢ
独厮守ぢ 2020-12-16 06:45

There are a number of questions regarding how to detect network connectivity in Android. https://stackoverflow.com/a/4239019/90236 provides a very detailed answer and https:

2条回答
  •  时光取名叫无心
    2020-12-16 06:51

    For completeness, let me post some code that I've added. I think it is fairly generic HttpClient code to make an Http request and return true when the request is succesful. I used HttpHead because I don't care about the entity in the response and this should make it faster. This method is in a class called ConnectivityHelper and DefaultSettings is a class that has a bunch of useful constants defined.

    public static boolean isNetworkAvailable() {
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo;
    
        try {
            netInfo = manager.getActiveNetworkInfo();
        } catch (Exception e) {
            netInfo = null;
        }
    
        return netInfo != null && netInfo.isConnected() && testConnection(DefaultSettings.TEST_CONNECTION_URL);
    }
    
    
    private static boolean testConnection(String url) {
        HttpClient httpClient = null;
        HttpHead request = null;
        HttpResponse response = null;
        int statusCode = 0;
    
        try {
            HttpParams myParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(myParams, DefaultSettings.CONNECTION_TIMEOUT);
            HttpConnectionParams.setSoTimeout(myParams, DefaultSettings.SO_TIMEOUT);
    
            httpClient = new DefaultHttpClient(myParams);
    
            request = new HttpHead(url);
            response = httpClient.execute(request);
            statusCode = response.getStatusLine().getStatusCode();
    
            if (statusCode != 200)
            {
                Log.e(ConnectivityHelper.class.getName(), String.format("testConnection not successful. For %s - Status code = %d", url, statusCode));
                return false;
            }
            return true;
    
        } catch(ClientProtocolException e) {
            Log.e(ConnectivityHelper.class.getName(), String.format("testConnection failure. For %s - Exception: %s.", url, e.toString()));
    
            if (request != null && !request.isAborted()) {
                request.abort();
            }
    
            return false;
        } catch(IOException e) {
            if (statusCode == 401) {
                Log.e(ConnectivityHelper.class.getName(), String.format("testConnection access denied. For %s - Status code = %d", url, statusCode));               
            } else {
                Log.e(ConnectivityHelper.class.getName(), String.format("testConnection failure. For %s - Exception: %s.", url, e.toString()));
            }
    
            if (request != null && !request.isAborted()) {
                request.abort();
            }
    
            return false;
        } finally {
            if (httpClient != null) {
                httpClient.getConnectionManager().shutdown();
            }
        }           
    }
    

提交回复
热议问题