-2

I wrote this code for checking INTERNET and it works but i have a problem that when wifi is on but internet does not exist!! in this situation my program force closed.

       private class NetCheck extends AsyncTask<String,String,Boolean>
       {

           @Override
           protected Boolean doInBackground(String... args){

               ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
               NetworkInfo netInfo = cm.getActiveNetworkInfo();
               if (netInfo != null && netInfo.isConnected()) {
                   try {
                       URL url = new URL("http://www.google.com");
                       HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
                       urlc.setConnectTimeout(1000);
                       urlc.connect();
                       if (urlc.getResponseCode() == 200) {
                           return true;
                       }
                   } catch (MalformedURLException e1) {
                       // TODO Auto-generated catch block
                       e1.printStackTrace();
                   } catch (IOException e) {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
                   }
               }
               return false;

           }

when internet connected or disconnected its work but when wifi on and internet not exist its not work an application force close!

@Override
           protected void onPostExecute(Boolean th){

               if(th == true){


                   getcountHA();


               }
               else{

                   ShowAlertDialog();
               }
           }
       }

whats problem!!

its my logcat

sebenalern
  • 2,515
  • 3
  • 26
  • 36
M.Wandadi
  • 1
  • 2

2 Answers2

1

Check with this method:

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

            if (ipAddr.equals("")) {
                return false;
            } else {
                return true;
            }

        } catch (Exception e) {
            return false;
        }

    }

Credits

Community
  • 1
  • 1
Skynet
  • 7,820
  • 5
  • 44
  • 80
-1

my codes for check INTERNET are true and work great and the force close is because of another place. when INTERNET are not available the server give me some String codes like (

M.Wandadi
  • 1
  • 2