[Solved] How can i check my android device having a internet connection in android? [duplicate]


This Exception is thrown because you performing networking operation in your main Thread. Do it in your Background Thread. ie AsyncTask

class Task extends AsyncTask<String, void, boolean> {

private Exception exception;

protected boolean doInBackground(String... xyz) {
    if (networkConnectivity()) {
        try {
            HttpURLConnection urlc = (HttpURLConnection) (new URL(
                    "http://www.google.com").openConnection());
            urlc.setRequestProperty("User-Agent", "Test");
            urlc.setRequestProperty("Connection", "close");
            urlc.setConnectTimeout(3000);
            urlc.setReadTimeout(4000);
            urlc.connect();
            // networkcode2 = urlc.getResponseCode();
            return (urlc.getResponseCode() == 200);
        } catch (IOException e) {
            return (false);
        }
    } else
        return false;
}


  private boolean networkConnectivity() {
    ConnectivityManager cm = (ConnectivityManager) _context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = cm.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected()) {
        return true;
    }
    return false;
}


}

8

solved How can i check my android device having a internet connection in android? [duplicate]