0

我使用volleyasyntasks从 VPS 下载数据并将其显示在片段和活动中。当我连接到公共网络并且网络信号(非常)弱时,我经常遇到此错误。当我没有连接到任何网络时,我在应用程序中使用了一个简单的网络检查功能来显示祝酒词。在这些情况下,活动不会开始,并且会显示 Toast。但是,当我在线并且网络信号很弱时,活动开始,Volley运行并抛出错误

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).

在应用程序的这种状态下,当我单击导航抽屉项目时,它会打开活动并引发相同的错误。这是我检查连接的方式:

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;
            }

        }
    }

您对解决此错误或升级我的网络检查功能以检查网络信号较弱有任何想法吗?

4

2 回答 2

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  
    }

请检查此代码。我希望它会有所帮助。

于 2015-03-10T16:57:07.493 回答
0

我在另一个线程上找到了这个:

在 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);
    }
}

所以你可以检查信号强度。

于 2017-02-09T11:56:12.457 回答