use of io.reactivex.ObservableOnSubscribe in project MVP by yuchengren.
the class RxAndroidActivity method test2.
private void test2() {
Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(ObservableEmitter<String> e) throws Exception {
// IO线程中做网络访问等耗时操作
// String responseJsonString = OkHttpUtil.get("http://192.168.0.1:8080/TradeType=getPeoples");
// String responseJsonString = "[{\"gendar\":29,\"name\":\"ren\",\"sex\":0},{\"gendar\":30,\"name\":\"ling\",\"sex\":1}]";
List<People> peopleList = new ArrayList<>();
peopleList.add(new People("ren", 0, 29));
peopleList.add(new People("ling", 1, 30));
LogHelper.e(TAG, GsonUtil.formatObjectToJson(peopleList));
e.onNext(GsonUtil.formatObjectToJson(peopleList));
}
}).map(new Function<String, List<People>>() {
@Override
public List<People> apply(String s) throws Exception {
LogHelper.d(TAG, "map,CurrentThreadName=" + Thread.currentThread().getName());
List<People> peopleList = GsonUtil.parseJsonToList(s, People.class);
// json解析成实体类
return peopleList;
}
}).doOnNext(new Consumer<List<People>>() {
@Override
public void accept(List<People> people) throws Exception {
LogHelper.d(TAG, "doOnNext,CurrentThreadName=" + Thread.currentThread().getName());
// 存入数据库
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<List<People>>() {
@Override
public void accept(List<People> people) throws Exception {
// 主线程中刷新页面
}
});
}
use of io.reactivex.ObservableOnSubscribe in project My-MVP by REBOOTERS.
the class RxJavaBaseActivity method basicRxjava2.
private void basicRxjava2() {
Observable mObservable = Observable.create(new ObservableOnSubscribe() {
@Override
public void subscribe(ObservableEmitter e) throws Exception {
e.onNext("1");
e.onNext("2");
e.onNext("3");
e.onNext("4");
e.onComplete();
}
});
Observer mObserver = new Observer() {
@Override
public void onSubscribe(Disposable d) {
Log.e(TAG, "onSubscribe: d=" + d);
sb.append("\nonSubcribe: d=" + d);
}
@Override
public void onNext(Object s) {
Log.e(TAG, "onNext: " + s);
sb.append("\nonNext: " + s);
}
@Override
public void onError(Throwable e) {
Log.e(TAG, "onError: " + e);
sb.append("\nonError: " + e.toString());
logContent.setText(sb.toString());
}
@Override
public void onComplete() {
Log.e(TAG, "onComplete");
sb.append("\nonComplete: ");
logContent.setText(sb.toString());
}
};
mObservable.subscribe(mObserver);
}
use of io.reactivex.ObservableOnSubscribe in project My-MVP by REBOOTERS.
the class RxJavaBaseActivity method withRetrofit2.
private void withRetrofit2() {
OkHttpClient.Builder mBuilder = new OkHttpClient.Builder();
mBuilder.readTimeout(10, TimeUnit.SECONDS);
mBuilder.connectTimeout(30, TimeUnit.SECONDS);
HttpLoggingInterceptor mLoggingInterceptor = new HttpLoggingInterceptor();
mLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
mBuilder.addInterceptor(mLoggingInterceptor);
OkHttpClient mClient = mBuilder.build();
Retrofit.Builder builder = new Retrofit.Builder();
builder.baseUrl(BASE_URL);
builder.client(mClient);
Retrofit mRetrofit = builder.build();
final GankApi mGankApi = mRetrofit.create(GankApi.class);
final Call<ResponseBody> mCall = mGankApi.getJson("10/1");
Observable.create((ObservableOnSubscribe<ResponseBody>) e -> e.onNext(mCall.execute().body())).map(ResponseBody::string).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
Log.e(TAG, "onSubscribe: d=" + d);
}
@Override
public void onNext(String s) {
logContent.setText(s);
}
@Override
public void onError(Throwable e) {
logContent.setText(e.toString());
}
@Override
public void onComplete() {
}
});
}
use of io.reactivex.ObservableOnSubscribe in project kripton by xcesco.
the class BindApp0DataSource method execute.
public <T> Observable<T> execute(final ObservableTransaction<T> transaction) {
ObservableOnSubscribe<T> emitter = new ObservableOnSubscribe<T>() {
@Override
public void subscribe(ObservableEmitter<T> emitter) {
// open database in thread safe mode
Pair<Boolean, SupportSQLiteDatabase> _status = BindApp0DataSource.this.openDatabaseThreadSafeMode(true);
boolean success = false;
SupportSQLiteDatabase connection = _status.value1;
DataSourceSingleThread currentDaoFactory = _daoFactorySingleThread.bindToThread();
currentDaoFactory.onSessionOpened();
try {
connection.beginTransaction();
if (transaction != null && TransactionResult.COMMIT == transaction.onExecute(currentDaoFactory, emitter)) {
connection.setTransactionSuccessful();
success = true;
}
emitter.onComplete();
} catch (Throwable e) {
Logger.error(e.getMessage());
e.printStackTrace();
emitter.onError(e);
currentDaoFactory.onSessionClear();
} finally {
try {
connection.endTransaction();
} catch (Throwable e) {
}
// close database in thread safe mode
closeThreadSafeMode(_status);
if (success) {
currentDaoFactory.onSessionClosed();
} else {
currentDaoFactory.onSessionClear();
}
}
return;
}
};
Observable<T> result = Observable.create(emitter);
if (globalSubscribeOn != null)
result.subscribeOn(globalSubscribeOn);
if (globalObserveOn != null)
result.observeOn(globalObserveOn);
return result;
}
use of io.reactivex.ObservableOnSubscribe in project kripton by xcesco.
the class BindAppDataSource method execute.
public <T> Observable<T> execute(final ObservableTransaction<T> transaction) {
ObservableOnSubscribe<T> emitter = new ObservableOnSubscribe<T>() {
@Override
public void subscribe(ObservableEmitter<T> emitter) {
// open database in thread safe mode
Pair<Boolean, SupportSQLiteDatabase> _status = BindAppDataSource.this.openDatabaseThreadSafeMode(true);
boolean success = false;
SupportSQLiteDatabase connection = _status.value1;
DataSourceSingleThread currentDaoFactory = _daoFactorySingleThread.bindToThread();
currentDaoFactory.onSessionOpened();
try {
connection.beginTransaction();
if (transaction != null && TransactionResult.COMMIT == transaction.onExecute(currentDaoFactory, emitter)) {
connection.setTransactionSuccessful();
success = true;
}
emitter.onComplete();
} catch (Throwable e) {
Logger.error(e.getMessage());
e.printStackTrace();
emitter.onError(e);
currentDaoFactory.onSessionClear();
} finally {
try {
connection.endTransaction();
} catch (Throwable e) {
}
// close database in thread safe mode
closeThreadSafeMode(_status);
if (success) {
currentDaoFactory.onSessionClosed();
} else {
currentDaoFactory.onSessionClear();
}
}
return;
}
};
Observable<T> result = Observable.create(emitter);
if (globalSubscribeOn != null)
result.subscribeOn(globalSubscribeOn);
if (globalObserveOn != null)
result.observeOn(globalObserveOn);
return result;
}
Aggregations