Instead of using it, Observable.createyou should use either Observable.defer()or else Observable.fromCallable(which was introduced in RxJava 1.0.15) - because these methods will provide the correct observable contract and save you some errors that you can enter when creating observables manually.
, runOnUiThread, , AndroidSchedulers.mainThread(), . RxAndroid , .
:
public Observable<String> getJsoupProxy() {
return Observable.fromCallable(() -> {
try {
Document jsoup_proxy = Jsoup.connect(Constants.SITE_PROXY_LIST)
.userAgent(Constants.USER_AGENT)
.ignoreContentType(true)
.ignoreHttpErrors(true)
.timeout(Constants.USER_TIMEOUT)
.get();
return jsoup_proxy != null ? jsoup_proxy.text().trim() : "";
} catch (IOException e) {
// just rethrow as RuntimeException to be caught in subscriber onError
throw new RuntimeException(e);
}
});
}
getJsoupProxy()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) // this scheduler is exported by RxAndroid library
.subscribe(
proxy -> {
if(proxy.equals("err_internet")) {
// toast
}
reloadAdapter();
},
error -> new DebugLog(getActivity(), "News", "Sync PROXY", Log.getStackTraceString(error)));