1

In device when i enable the internet data it shows E in network. but i dont have data pack in my sim and internet is not working in any apps. I have used this code it returns true that internet is there. In this case how to find whether internet is working or not:

 public static boolean isConnectingToInternetLatest(Context mContext) {
    ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Network[] networks = connectivityManager.getAllNetworks();
        NetworkInfo networkInfo;
        for (Network mNetwork : networks) {
            networkInfo = connectivityManager.getNetworkInfo(mNetwork);
            if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) {
                return true;
            }
        }
    } else {
        if (connectivityManager != null) {
            //noinspection deprecation
            NetworkInfo[] info = connectivityManager.getAllNetworkInfo();
            if (info != null) {
                for (NetworkInfo anInfo : info) {
                    if (anInfo.getState() == NetworkInfo.State.CONNECTED) {
                        //Log.d("Network", "NETWORK NAME: " + anInfo.getTypeName());
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
YLS
  • 1,368
  • 2
  • 12
  • 32
  • Look for examples that makes a request to a url, say google.com and listens for an OK response. If so, then internet is available. – Zhi Kai Oct 18 '16 at 14:53
  • try this http://stackoverflow.com/questions/6493517/detect-if-android-device-has-internet-connection/30733807#30733807 – Bilal Shahid Oct 18 '16 at 14:55

2 Answers2

1
public boolean isInternetWorking() {
boolean success = false;
try {
    URL url = new URL("https://google.com");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setConnectTimeout(10000);
    connection.connect();
    success = connection.getResponseCode() == 200;
} catch (IOException e) {
    e.printStackTrace();
}
return success;
}
YLS
  • 1,368
  • 2
  • 12
  • 32
0

Additional error checking from @YLS answer. This is what i do to check HttpError.. You can modified onError code to your desired code.

try {
    URL url = new URL("https://google.com");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setConnectTimeout(10000);
    connection.connect();
    success = connection.getResponseCode() == 200;
} catch (IOException e) {
    onError(this,e);
}

public void onError(Activity activity,Throwable e){
        try {
            e.printStackTrace();
            FirebaseCrash.report(e);

            if (e instanceof HttpException) {
                Response body = ((HttpException) e).response();
                if (body.code() == 401) {
                    Toast.makeText(activity, "Not Authorized Access", Toast.LENGTH_SHORT).show();
                } else if (body.code() == 404) {
                    Toast.makeText(activity, "Request not found", Toast.LENGTH_SHORT).show();
                } else if (body.code() == 400) {
                    Toast.makeText(activity, "Bad Request", Toast.LENGTH_SHORT).show();
                } else if (body.code() == 500) {
                    Toast.makeText(activity, "Internal Server Error", Toast.LENGTH_SHORT).show();
                } else {
                    Log.e(Constants.TAG, "Error : Code HTTP = " + body.code());
                }
            }else if(e instanceof ConnectException || e instanceof SocketException){
                Toast.makeText(activity, "Check Your Internet Connection", Toast.LENGTH_SHORT).show();
            } else if (e instanceof UnknownHostException) {
                Toast.makeText(activity, "Make Sure Your Internet Connection is Properly Connected", Toast.LENGTH_SHORT).show();
            } else if (e instanceof SSLHandshakeException || e instanceof SSLPeerUnverifiedException) {
                Toast.makeText(activity, "Server Connection Problem..Please Try Again", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(activity, "Data Load Error", Toast.LENGTH_SHORT).show();
            }
        }
        catch (Exception ex){
            FirebaseCrash.report(e);
            e.printStackTrace();
        }
    }
ZeroOne
  • 7,936
  • 4
  • 23
  • 37