I'm using the 'com.github.pwittchen:reactivenetwork-rx2:3.0.3'
from https://github.com/pwittchen/ReactiveNetwork
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.
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. I resolve the problem using onError method in 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
}
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.