[英]Android RactiveNetwork crashing app when no strong network
I'm using the 'com.github.pwittchen:reactivenetwork-rx2:3.0.3'
from https://github.com/pwittchen/ReactiveNetwork 我正在使用来自https://github.com/pwittchen/ReactiveNetwork的'com.github.pwittchen:reactivenetwork-rx2:3.0.3'
And here is my code 这是我的代码
@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
}
}
I have added android:usesCleartextTraffic="true"
into application manifest. 我已经将android:usesCleartextTraffic="true"
到应用程序清单中。
Still app keeps crashing. 应用仍然崩溃。 Is there anyone using the library and facing same network? 有没有人在使用图书馆并面对相同的网络?
I chose the library because it test if phone actually have access to internet. 我选择该库是因为它可以测试电话是否真的可以访问互联网。
Any help will be appreciated. 任何帮助将不胜感激。
Here is the error 这是错误
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)
Even if connection timeout, is there anyway to handle this? 即使连接超时,仍然可以处理吗? I also noticed is only when i'm having unstable network that the code crash the app, which caused the timeout. 我还注意到,只有当我的网络不稳定时,代码才会使应用崩溃,从而导致超时。
Thanks to pwittchen, the owner of ReactiveNetwork library for his quick reposne. 感谢ReactiveNetwork库的所有者pwittchen的快速申购。 I resolve the problem using onError method in rxjava
我在rxjava
使用onError方法解决了问题
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
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.