[英]Android RactiveNetwork crashing app when no strong network
我正在使用來自https://github.com/pwittchen/ReactiveNetwork的'com.github.pwittchen: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庫的所有者pwittchen的快速申購。 我在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.