本文整理汇总了Java中io.reactivex.ObservableOnSubscribe类的典型用法代码示例。如果您正苦于以下问题:Java ObservableOnSubscribe类的具体用法?Java ObservableOnSubscribe怎么用?Java ObservableOnSubscribe使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ObservableOnSubscribe类属于io.reactivex包,在下文中一共展示了ObservableOnSubscribe类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onHandleParseHTML
import io.reactivex.ObservableOnSubscribe; //导入依赖的package包/类
@Override
public void onHandleParseHTML(final String url) {
mView.showLoading(true);
Observable.create(new ObservableOnSubscribe<ArrayList<ArticleItem>>() {
@Override
public void subscribe(ObservableEmitter<ArrayList<ArticleItem>> e) throws Exception {
ArrayList<ArticleItem> list = new ArrayList<>();
Document doc = Jsoup.connect(url).get();
Elements ul = doc.getElementsByClass("list_line");
for (Element u : ul) {
Elements li = u.getElementsByTag("li");
for (Element l : li) {
String text = l.getElementsByTag("a").text();
String href = l.getElementsByTag("a").attr("href");
String time = l.getElementsByTag("span").text();
list.add(new ArticleItem(text, href, time));
}
}
e.onNext(list);
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<ArrayList<ArticleItem>>() {
@Override
public void accept(@NonNull ArrayList<ArticleItem> articleItems) throws Exception {
mView.showList(articleItems);
mView.showLoading(false);
}
});
}
示例2: beaconsInRegion
import io.reactivex.ObservableOnSubscribe; //导入依赖的package包/类
public Observable<RxBeaconRange> beaconsInRegion() {
return startup()
.flatMap(new Function<Boolean, ObservableSource<RxBeaconRange>>() {
@Override
public ObservableSource<RxBeaconRange> apply(@NonNull Boolean aBoolean) throws Exception {
return Observable.create(new ObservableOnSubscribe<RxBeaconRange>() {
@Override
public void subscribe(@NonNull final ObservableEmitter<RxBeaconRange> objectObservableEmitter) throws Exception {
beaconManager.addRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> collection, Region region) {
objectObservableEmitter.onNext(new RxBeaconRange(collection, region));
}
});
beaconManager.startRangingBeaconsInRegion(getRegion());
}
});
}
});
}
示例3: getObservable
import io.reactivex.ObservableOnSubscribe; //导入依赖的package包/类
private Observable<Integer> getObservable() {
return Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
// send events with simulated time wait
Thread.sleep(0);
emitter.onNext(1); // skip
emitter.onNext(2); // deliver
Thread.sleep(505);
emitter.onNext(3); // skip
Thread.sleep(99);
emitter.onNext(4); // skip
Thread.sleep(100);
emitter.onNext(5); // skip
emitter.onNext(6); // deliver
Thread.sleep(305);
emitter.onNext(7); // deliver
Thread.sleep(510);
emitter.onComplete();
}
});
}
示例4: insertRecipes
import io.reactivex.ObservableOnSubscribe; //导入依赖的package包/类
public void insertRecipes(@NonNull final ArrayList<Recipe> recipes, Observer<Integer> observer){
Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> e) throws Exception {
ContentValues[] contentValues = new ContentValues[recipes.size()];
for (int i = 0; i < recipes.size(); ++i) {
contentValues[i] = buildContentValuesFromRecipe(recipes.get(i));
}
int recipesAdded = mContext.getContentResolver().bulkInsert(RecipesContract.RecipeEntry.CONTENT_URI, contentValues);
if (recipesAdded != 0){
e.onNext(recipesAdded);
} else {
e.onError(new NullPointerException("Failed to insert"));
}
e.onComplete();
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(observer);
}
示例5: createResults
import io.reactivex.ObservableOnSubscribe; //导入依赖的package包/类
private Observable<List<Task>> createResults(QuerySelector<DbTask> querySelector) {
return Observable.create((ObservableOnSubscribe<List<Task>>) emitter -> {
Realm realm = Realm.getDefaultInstance();
final RealmResults<DbTask> dbTasks = querySelector.createQuery(realm);
final RealmChangeListener<RealmResults<DbTask>> realmChangeListener = element -> {
if(element.isLoaded() && !emitter.isDisposed()) {
List<Task> tasks = mapFrom(element);
if(!emitter.isDisposed()) {
emitter.onNext(tasks);
}
}
};
emitter.setDisposable(Disposables.fromAction(() -> {
if(dbTasks.isValid()) {
dbTasks.removeChangeListener(realmChangeListener);
}
realm.close();
}));
dbTasks.addChangeListener(realmChangeListener);
}).subscribeOn(looperScheduler.getScheduler()).unsubscribeOn(looperScheduler.getScheduler());
}
示例6: getObservable
import io.reactivex.ObservableOnSubscribe; //导入依赖的package包/类
private Observable<Integer> getObservable() {
return Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
// send events with simulated time wait
//1(drop)--(400s<500s)---2(pass)---(600s>500s)---3(drop)---(100s<500s)---4(pass)---(605s>500s)---5(pass)---510s
emitter.onNext(1); // skip
Thread.sleep(400);
emitter.onNext(2); // deliver
Thread.sleep(600);
emitter.onNext(3); // skip
Thread.sleep(100);
emitter.onNext(4); // deliver
Thread.sleep(605);
emitter.onNext(5); // deliver
Thread.sleep(510);
emitter.onComplete();
}
});
}
示例7: getAllIcons
import io.reactivex.ObservableOnSubscribe; //导入依赖的package包/类
public Disposable getAllIcons() {
return Observable.create(new ObservableOnSubscribe<IconBean>() {
@Override
public void subscribe(@NonNull ObservableEmitter<IconBean> e) throws Exception {
XmlResourceParser xml = mView.getResources().getXml(R.xml.drawable);
while (xml.getEventType() != XmlResourceParser.END_DOCUMENT) {
if (xml.getEventType() == XmlPullParser.START_TAG) {
if (xml.getName().startsWith("item")) {
IconBean bean = new IconBean();
String iconName = xml.getAttributeValue(null, "drawable");
bean.id = mView.getResources().getIdentifier(
iconName, "drawable", BuildConfig.APPLICATION_ID);
bean.name = iconName;
e.onNext(bean);
}
}
xml.next();
}
e.onComplete();
}
}).toList().subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<List<IconBean>>() {
@Override
public void accept(List<IconBean> list) throws Exception {
mView.onLoadData(list);
}
});
}
示例8: map
import io.reactivex.ObservableOnSubscribe; //导入依赖的package包/类
private void map() {
Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> e) throws Exception {
e.onNext(1);
e.onNext(2);
e.onNext(3);
}
}).map(new Function<Integer, String>() {
@Override
public String apply(Integer integer) throws Exception {
return "This is result " + integer;
}
}).subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
Log.e(MainActivity.TAG, "accept: " + Thread.currentThread().getName());
info += s + "\n";
tv.setText(info);
}
});
}
示例9: test
import io.reactivex.ObservableOnSubscribe; //导入依赖的package包/类
public void test()
{
Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> e) throws Exception
{
e.onNext(1);
}
}).subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception
{
System.out.println(integer);
}
});
}
示例10: doRxJavaWork
import io.reactivex.ObservableOnSubscribe; //导入依赖的package包/类
private void doRxJavaWork() {
Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
for (;;) { // 无限循环发送事件
emitter.onNext(Integer.MAX_VALUE);
}
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
Log.d(TAG, "" + integer);
}
});
}
示例11: getObservable
import io.reactivex.ObservableOnSubscribe; //导入依赖的package包/类
public Observable<String> getObservable() {
return Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(ObservableEmitter<String> e) throws Exception {
e.onNext("Today's news update");
e.onNext("Today's topic is Study");
e.onComplete();
}
});
/* 下面两个方法作用类似,just 的内部调用的就是 fromArray */
// return Observable.just("Topic 1", "Heat 1", "News");
// return Observable.fromArray("Topic 1", "Heat 1", "News");
/* 只能发送一个数据 */
/*return Observable.fromCallable(new Callable<String>() {
@Override
public String call() throws Exception {
return "Topic is Study";
}
});*/
}
示例12: requestLoadingList
import io.reactivex.ObservableOnSubscribe; //导入依赖的package包/类
public void requestLoadingList() {
Observable.create(new ObservableOnSubscribe<List<ConfigBean>>() {
@Override
public void subscribe(ObservableEmitter<List<ConfigBean>> e) throws Exception {
mModel = ConfigModel.getInstance(configView.getContext());
e.onNext(mModel.getConfigList());
mModel.setConfigCallback(ConfigPresenter.this);
}
})
.observeOn(Schedulers.io())
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<List<ConfigBean>>() {
@Override
public void accept(List<ConfigBean> list) throws Exception {
configView.displayConfigList(list);
}
});
}
示例13: with
import io.reactivex.ObservableOnSubscribe; //导入依赖的package包/类
public static Observable<View> with(final View view) {
return Observable.create(new ObservableOnSubscribe<View>() {
@Override
public void subscribe(@NonNull final ObservableEmitter<View> e) throws Exception {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View value) {
e.onNext(value);
}
});
}
});
}
});
}
示例14: getAlbumByCate
import io.reactivex.ObservableOnSubscribe; //导入依赖的package包/类
/**
* 按一级分类查找专辑
**/
private Observable<List<Album>> getAlbumByCate(String cateId, int calc_dimension) {
final Map<String, String> params = new HashMap<>();
params.put(DTransferConstants.CATEGORY_ID, cateId);
params.put(DTransferConstants.CALC_DIMENSION, String.valueOf(calc_dimension));
return Observable.create(new ObservableOnSubscribe<List<Album>>() {
@Override
public void subscribe(final ObservableEmitter<List<Album>> e) throws Exception {
CommonRequest.getAlbumList(params, new IDataCallBack<AlbumList>() {
@Override
public void onSuccess(AlbumList albumList) {
//onNext的参数不允许为null
e.onNext(albumList.getAlbums());
e.onComplete();
}
@Override
public void onError(int i, String s) {
e.onError(new Throwable(i + " " + s));
}
});
}
}).subscribeOn(Schedulers.io());
}
示例15: getAlbumByKeyWord
import io.reactivex.ObservableOnSubscribe; //导入依赖的package包/类
/**
* 按关键词查找专辑
**/
private Observable<List<Album>> getAlbumByKeyWord(String keyword, String cateId, int calc_dimension) {
final Map<String, String> params = new HashMap<>();
params.put(DTransferConstants.SEARCH_KEY, keyword);
params.put(DTransferConstants.CATEGORY_ID, cateId);
params.put(DTransferConstants.CALC_DIMENSION, String.valueOf(calc_dimension));
return Observable.create(new ObservableOnSubscribe<List<Album>>() {
@Override
public void subscribe(final ObservableEmitter<List<Album>> e) throws Exception {
CommonRequest.getSearchedAlbums(params, new IDataCallBack<SearchAlbumList>() {
@Override
public void onSuccess(SearchAlbumList searchAlbumList) {
e.onNext(searchAlbumList.getAlbums());
e.onComplete();
}
@Override
public void onError(int i, String s) {
e.onError(new Throwable(i + " " + s));
}
});
}
})
.subscribeOn(Schedulers.io());
}