0

I use volley and asyntasks to download data from a VPS and display it in fragments and activities. I encounter this error a lot when I am connected to a public network and the network signal is (very) weak. I use a simple network checking function in the app to display a toast when I am not connected to any network. In these cases the activities don't start and the Toast is displayed. However, when I'm online and the network signal is very weak, the activity starts, Volley runs and throws an error

java.net.ConnectException: failed to connect to www.website.com/XX.XX.XX.XXX (port 80) after 2500ms: isConnected failed: EHOSTUNREACH (where Xs are the server address).

In this state of the app when I click on a navdrawer item it opens the activity and throws the same error. This is how I check for connection:

public static boolean isInternetAvailable(Context context)
    {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();

        if (info == null) {
             Log.d(TAG,"no internet connection");
             return false;
        }
        else
        {
            if(info.isConnected()) {
                Log.d(TAG," internet connection available...");
                return true;
            }
            else {
                Log.d(TAG," noninternet connection");
                return false;
            }

        }
    }

Do you have any ideas on solving this error or upgrading my network checking function to check for weak network signal?

erdomester
  • 11,789
  • 32
  • 132
  • 234
  • Gating operation at the front door is poor design, because even if the network is strong when you check it, the situation might be quite different even a split second later when you are trying to use it. Don't require and assume, rather *deal with whatever actually happens*. – Chris Stratton Mar 10 '15 at 19:16

2 Answers2

1
ConnectivityManager connectivityManager = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = connectivityManager.getActiveNetworkInfo();
    DetailedState detailedState = info.getDetailedState();
    if(detailedState == DetailedState.VERIFYING_POOR_LINK){
          // Your checks goes here  
    }

Please check this code. I hope it will help.

Rohan Pawar
  • 1,875
  • 22
  • 40
Satty
  • 1,352
  • 1
  • 12
  • 16
  • Thanks, but it requires API level 16. – erdomester Mar 10 '15 at 17:05
  • yeah, what is your API requirement ? then I think info.isConnectedOrConnecting() can probably help you in knowing the current status of network. Also if exception comes after opening activity , you can still dialog to user and close the activity by catching the exception. – Satty Mar 10 '15 at 17:40
  • I know but I want to prevent it. My minimum is API 14. `isConnectedOrConnecting() ` doesn't help. – erdomester Mar 10 '15 at 17:45
0

I found this on another thread:

Get Signal Strength in Android

public class myPhoneStateListener extends PhoneStateListener {
    public int signalStrengthValue;

    public void onSignalStrengthsChanged(SignalStrength signalStrength) {
        super.onSignalStrengthsChanged(signalStrength);
        if (signalStrength.isGsm()) {
            if (signalStrength.getGsmSignalStrength() != 99)
                signalStrengthValue = signalStrength.getGsmSignalStrength() * 2- 113;
            else
                signalStrengthValue = signalStrength.getGsmSignalStrength();
        } else {
            signalStrengthValue = signalStrength.getCdmaDbm();
        }
        txtSignalStr.setText("Signal Strength : " + signalStrengthValue);
    }
}

So you can check the signal strength.

Community
  • 1
  • 1
MichaB
  • 495
  • 7
  • 20