use of io.reactivex.ObservableOnSubscribe in project WorkHelperApp by hubme.
the class RxJavaSampleFragment method onTimeInterval.
/*
将一个发射数据的Observable转换为发射那些数据发射时间间隔的Observable.
操作符拦截原始Observable发射的数据项,替换为发射表示相邻发射物时间间隔的对象。
*/
@OnClick(R.id.tv_timeInterval)
public void onTimeInterval() {
Disposable subscribe = Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(@NonNull ObservableEmitter<String> e) throws Exception {
for (int i = 0; i < 3; i++) {
e.onNext(i + "");
// Thread.sleep(1000);
SystemClock.sleep(1000);
}
e.onComplete();
}
}).subscribeOn(Schedulers.newThread()).timeInterval().subscribe(new Consumer<Timed<String>>() {
@Override
public void accept(@NonNull Timed<String> stringTimed) throws Exception {
Logger.i(String.valueOf(stringTimed.time()));
}
});
mCompositeDisposable.add(subscribe);
}
use of io.reactivex.ObservableOnSubscribe in project SeeWeather by xcc3641.
the class MultiCityFragment method multiLoad.
private void multiLoad() {
mWeathers.clear();
Observable.create((ObservableOnSubscribe<CityORM>) emitter -> {
try {
for (CityORM cityORM : OrmLite.getInstance().query(CityORM.class)) {
emitter.onNext(cityORM);
}
emitter.onComplete();
} catch (Exception e) {
emitter.onError(e);
}
}).doOnSubscribe(subscription -> mRefreshLayout.setRefreshing(true)).map(city -> Util.replaceCity(city.getName())).distinct().flatMap(cityName -> RetrofitSingleton.getInstance().fetchWeather(cityName)).filter(weather -> !C.UNKNOWN_CITY.equals(weather.status)).take(3).compose(RxUtil.fragmentLifecycle(this)).doOnNext(weather -> mWeathers.add(weather)).doOnComplete(() -> {
mRefreshLayout.setRefreshing(false);
mAdapter.notifyDataSetChanged();
if (mAdapter.isEmpty()) {
mLayout.setVisibility(View.VISIBLE);
} else {
mLayout.setVisibility(View.GONE);
}
}).doOnError(error -> {
if (mAdapter.isEmpty() && mLayout != null) {
mLayout.setVisibility(View.VISIBLE);
}
}).subscribe();
}
use of io.reactivex.ObservableOnSubscribe in project SmartCampus by Vegen.
the class HomePresenter method getNewsList.
@Override
public void getNewsList(String url) {
Disposable disposable = Observable.create((ObservableOnSubscribe<Document>) e -> {
Document document = Jsoup.connect(url).get();
e.onNext(document);
e.onComplete();
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(document -> {
Element body = document.body();
Element photoList = body.getElementById("ListNews").getElementById("leftbox").getElementById("photolist");
// 新闻列表
List<NewsList> newsListList = new ArrayList<>();
// NEWS_TYPE_FOCUS 要闻
newsListList.add(new NewsList(NEWS_TYPE_FOCUS, getTagNews(photoList, "xxyw", NEWS_TYPE_FOCUS)));
// NEWS_TYPE_COMPREHENSIVE 综合
newsListList.add(new NewsList(NEWS_TYPE_COMPREHENSIVE, getTagNews(photoList, "zhxw", NEWS_TYPE_COMPREHENSIVE)));
// NEWS_TYPE_FLASH 快讯
newsListList.add(new NewsList(NEWS_TYPE_FLASH, getTagNews(photoList, "xykx", NEWS_TYPE_FLASH)));
// NEWS_TYPE_OTHER 其他
newsListList.add(new NewsList(NEWS_TYPE_OTHER, getTagNews(photoList, "otherPic", NEWS_TYPE_OTHER)));
List<String> strings = new ArrayList<>();
Elements elements = photoList.select("div.more");
for (Element element : elements) {
String id = element.getElementsByTag("a").attr("href");
strings.add(id);
}
if (mView != null) {
mView.showNewsList(newsListList);
mView.moreNewsClassId(strings);
mView.hideLoading(false);
}
}, throwable -> {
if (mView != null) {
mView.showMessage(HttpError.getErrorMessage(throwable));
mView.hideLoading(true);
}
});
mHttpLinkers.add(new DisposableHolder(disposable));
}
use of io.reactivex.ObservableOnSubscribe in project SmartCampus by Vegen.
the class MoreNewsPresenter method getNewsList.
@Override
public void getNewsList(String url, int newsType) {
Disposable disposable = Observable.create((ObservableOnSubscribe<Document>) e -> {
Document document = Jsoup.connect(url).get();
e.onNext(document);
e.onComplete();
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(document -> {
Element body = document.body();
// 下一页的参数
String __VIEWSTATE = body.select("#__VIEWSTATE").first().attr("value").trim();
String __VIEWSTATEGENERATOR = body.select("#__VIEWSTATEGENERATOR").first().attr("value").trim();
String __EVENTVALIDATION = body.select("#__EVENTVALIDATION").first().attr("value").trim();
Map<String, String> newsForm = new HashMap<>();
newsForm.put("__VIEWSTATE", __VIEWSTATE);
newsForm.put("__VIEWSTATEGENERATOR", __VIEWSTATEGENERATOR);
newsForm.put("__EVENTVALIDATION", __EVENTVALIDATION);
// 本页的数据
Element photoList = body.getElementById("ListNews").getElementById("leftbox").getElementById("photolist");
List<News> newsList = getTagNews(photoList, newsType);
if (mView != null) {
mView.nextNewsListForm(newsForm);
mView.showNewsList(newsList);
mView.hideLoading(false);
}
}, throwable -> {
if (mView != null) {
mView.showMessage(HttpError.getErrorMessage(throwable));
mView.hideLoading(true);
}
});
mHttpLinkers.add(new DisposableHolder(disposable));
}
use of io.reactivex.ObservableOnSubscribe in project MyJapanese by 54wall.
the class FavWordsFragmentModelImpl method getData.
@Override
public void getData(Consumer<List<Word>> consumer, Consumer<Throwable> throwable, final String lessonId) {
Disposable disposable = Observable.create(new ObservableOnSubscribe<List<Word>>() {
@Override
public void subscribe(ObservableEmitter<List<Word>> emitter) throws Exception {
List<Word> list;
list = DBManager.getInstance().getFav(lessonId);
if (list == null) {
Log.e(TAG, "list == null");
// emitter.onError(new Exception());//经常报这个错误io.reactivex.exceptions.OnErrorNotImplementedException
} else {
emitter.onNext(list);
}
emitter.onComplete();
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(consumer, throwable);
mCompositeDisposable.add(disposable);
}
Aggregations