如何查找互联网是否正常工作
How to find internet is working or not
在设备中,当我启用互联网数据时,它在网络中显示 E。但我的 SIM 卡中没有数据包,互联网在任何应用程序中都无法使用。我已经使用了这段代码 returns 是的,互联网就在那里。在这种情况下如何确定互联网是否正常工作:
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;
}
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 答案的额外错误检查。这就是我检查 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();
}
}
在设备中,当我启用互联网数据时,它在网络中显示 E。但我的 SIM 卡中没有数据包,互联网在任何应用程序中都无法使用。我已经使用了这段代码 returns 是的,互联网就在那里。在这种情况下如何确定互联网是否正常工作:
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;
}
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 答案的额外错误检查。这就是我检查 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();
}
}