use of rx.Subscription in project okhttp-OkGo by jeasonlzy.
the class RxCommonActivity method retrofitRequest.
@OnClick(R.id.retrofitRequest)
public void retrofitRequest(View view) {
Subscription subscription = //
ServerApi.getServerModel("aaa", "bbb").doOnSubscribe(new Action0() {
@Override
public void call() {
//开始请求前显示对话框
showLoading();
}
}).map(new Func1<LzyResponse<ServerModel>, ServerModel>() {
@Override
public ServerModel call(LzyResponse<ServerModel> response) {
return response.data;
}
}).observeOn(//切换到主线程
AndroidSchedulers.mainThread()).subscribe(new Action1<ServerModel>() {
@Override
public void call(ServerModel serverModel) {
//请求成功
dismissLoading();
handleResponse(serverModel, null, null);
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
//请求失败
throwable.printStackTrace();
showToast("请求失败");
dismissLoading();
handleError(null, null);
}
});
addSubscribe(subscription);
}
use of rx.Subscription in project okhttp-OkGo by jeasonlzy.
the class RxCommonActivity method commonRequest.
@OnClick(R.id.commonRequest)
public void commonRequest(View view) {
Subscription subscription = //
OkGo.post(Urls.URL_METHOD).headers("aaa", //
"111").params("bbb", //
"222").getCall(StringConvert.create(), //以上为产生请求事件,请求默认发生在IO线程
RxAdapter.<String>create()).doOnSubscribe(new Action0() {
@Override
public void call() {
//开始请求前显示对话框
showLoading();
}
}).observeOn(//切换到主线程
AndroidSchedulers.mainThread()).subscribe(new Action1<String>() {
@Override
public void call(String s) {
//请求成功,关闭对话框
dismissLoading();
handleResponse(s, null, null);
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
throwable.printStackTrace();
//请求失败
dismissLoading();
showToast("请求失败");
handleError(null, null);
}
});
addSubscribe(subscription);
}
use of rx.Subscription in project nucleus by konmik.
the class DeliverReplay method call.
@Override
public Observable<Delivery<View, T>> call(Observable<T> observable) {
final ReplaySubject<Notification<T>> subject = ReplaySubject.create();
final Subscription subscription = observable.materialize().filter(new Func1<Notification<T>, Boolean>() {
@Override
public Boolean call(Notification<T> notification) {
return !notification.isOnCompleted();
}
}).subscribe(subject);
return view.switchMap(new Func1<View, Observable<Delivery<View, T>>>() {
@Override
public Observable<Delivery<View, T>> call(final View view) {
return view == null ? Observable.<Delivery<View, T>>never() : subject.map(new Func1<Notification<T>, Delivery<View, T>>() {
@Override
public Delivery<View, T> call(Notification<T> notification) {
return new Delivery<>(view, notification);
}
});
}
}).doOnUnsubscribe(new Action0() {
@Override
public void call() {
subscription.unsubscribe();
}
});
}
use of rx.Subscription in project nucleus by konmik.
the class RxPresenterTest method testStartedRestartableIsNotUnsubscribed.
@Test
public void testStartedRestartableIsNotUnsubscribed() throws Exception {
RxPresenter presenter = new RxPresenter();
presenter.create(null);
Func0<Subscription> restartable = mock(Func0.class);
Subscription subscription = mock(Subscription.class);
when(restartable.call()).thenReturn(subscription);
when(subscription.isUnsubscribed()).thenReturn(false);
presenter.restartable(1, restartable);
assertTrue(presenter.isUnsubscribed(1));
presenter.start(1);
assertFalse(presenter.isUnsubscribed(1));
}
use of rx.Subscription in project nucleus by konmik.
the class RxPresenterTest method testAddRemove.
@Test
public void testAddRemove() throws Exception {
RxPresenter presenter = new RxPresenter();
Subscription mock = Mockito.mock(Subscription.class);
when(mock.isUnsubscribed()).thenReturn(false);
presenter.add(mock);
presenter.remove(mock);
verify(mock, atLeastOnce()).isUnsubscribed();
verify(mock, times(1)).unsubscribe();
presenter.onDestroy();
verifyNoMoreInteractions(mock);
}
Aggregations