1

在设备中,当我启用互联网数据时,它在网络中显示 E。但我的 sim 卡中没有数据包,并且互联网无法在任何应用程序中运行。我已经使用了这段代码,它返回了互联网存在的真实情况。在这种情况下,如何确定互联网是否正常工作:

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

2 回答 2

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;
}
于 2016-10-18T14:58:01.253 回答
0

来自@YLS 答案的附加错误检查。这就是我检查 HttpError 的方法。您可以将 onError 代码修改为所需的代码。

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();
        }
    }
于 2016-10-18T15:16:15.940 回答