Java 当没有强大的网络时,Android会导致网络崩溃
我使用的是来自的Java 当没有强大的网络时,Android会导致网络崩溃,java,android,Java,Android,我使用的是来自的'com.github.pwitchen:reactivenetwork-rx2:3.0.3' 这是我的密码 @SuppressLint("CheckResult") public void checkNetworkAvailable() { ReactiveNetwork .observeInternetConnectivity() .subscribeOn(Schedulers.io())
'com.github.pwitchen:reactivenetwork-rx2:3.0.3'
这是我的密码
@SuppressLint("CheckResult")
public void checkNetworkAvailable() {
ReactiveNetwork
.observeInternetConnectivity()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(isConnectedToInternet -> {
changeOnlineStatus(isConnectedToInternet ? ConnectionQuality.EXCELLENT : ConnectionQuality.POOR);
});
}
private void changeOnlineStatus(ConnectionQuality connectionQuality) {
if (connectionQuality == ConnectionQuality.EXCELLENT) {
// do something
if (isTrue) {
Snackbar.make(getView(), "Internet Connected", Snackbar.LENGTH_LONG).show();
}
} else if (connectionQuality == ConnectionQuality.POOR) {
// do something
isTrue = true;
final Snackbar snackBar = Snackbar.make(getView(), "No Internet connection", Snackbar.LENGTH_INDEFINITE);
snackBar.setAction("DISMISS", v -> {
// Call your action method here
snackBar.dismiss();
});
snackBar.show();
} else if (connectionQuality == ConnectionQuality.UNKNOWN) {
isTrue = true;
final Snackbar snackBar = Snackbar.make(getView(), "Unknown network", Snackbar.LENGTH_INDEFINITE);
snackBar.setAction("DISMISS", v -> {
// Call your action method here
snackBar.dismiss();
});
snackBar.show();
// do something
}
}
我已经在应用程序清单中添加了android:usesCleartextTraffic=“true”
应用程序仍在崩溃。是否有人使用图书馆并面对相同的网络
我选择图书馆是因为它测试手机是否真的可以上网
任何帮助都将不胜感激
这里是错误
E: Could not establish connection with WalledGardenStrategy
java.net.SocketTimeoutException: timeout
at com.android.okhttp.okio.Okio$3.newTimeoutException(Okio.java:212)
at com.android.okhttp.okio.AsyncTimeout.exit(AsyncTimeout.java:261)
at com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:215)
at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:306)
at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:300)
at com.android.okhttp.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196)
at com.android.okhttp.internal.http.Http1xStream.readResponse(Http1xStream.java:186)
at com.android.okhttp.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:127)
at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:737)
at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:609)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:471)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:538)
即使连接超时,是否仍要处理此问题?
我还注意到,只有当我的网络不稳定时,代码才会使应用程序崩溃,从而导致超时。多亏了ReactiveNetwork library的所有者pWitchen的快速响应。我在
rxjava
public void checkNetworkAvailable() {
ReactiveNetwork
.observeInternetConnectivity()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Boolean>() {
@Override
public void onSubscribe(final Disposable d) {
// this will be invoked before operation is started
}
@Override
public void onNext(final Boolean isConnectedToInternet) {
// do your action, when you're connected to the internet
changeOnlineStatus(isConnectedToInternet ? ConnectionQuality.EXCELLENT : ConnectionQuality.POOR);
}
@Override
public void onError(final Throwable e) {
// handle an error here <-----------------
Snackbar.make(getView(), "Network timeout!", Snackbar.LENGTH_LONG).show();
}
@Override
public void onComplete() {
// this will be invoked when operation is completed
}
});
}
public void checkNetworkAvailable(){
反应网络
.observeInternetConnectivity()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.订阅(新观察员){
@凌驾
公开认购无效(最终可处置d){
//这将在操作开始之前调用
}
@凌驾
public void onNext(最终布尔值为connectedpointernet){
//当您连接到internet时,执行您的操作
更改联机状态(已连接到Internet?连接质量。优秀:连接质量。差);
}
@凌驾
公共作废申报人(最终可丢弃e){
//在此处处理错误检查以下内容: