*/
public void onRetrofitClick(View view) {
startActivity(new Intent(this, RetrofitActivity.class));
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
viewModel = new RxjavaViewModel(this);
getLifecycle().addObserver(viewModel);
mListView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, strings));
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0://Get方法 @Query
RetrofitClient.getInstance()
.create(ServiceApi.class)// 创建服务
.getSearchContent("", 5)//调用接口
.subscribeOn(Schedulers.io())// 指定被观察者的操作在io线程中完成
.doOnSubscribe(disposable -> viewModel.showProgressDialog(“正在请求中”))
.observeOn(AndroidSchedulers.mainThread())//指定观察者接收到数据,然后在Main线程中完成
.retryWhen(new RetryWithDelay(1, 1))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
.doAfterTerminate(() -> viewModel.hintProgressDialog())
.compose(rxLifecycle.bindUntilEvent(Lifecycle.Event.ON_DESTROY))//生命周期
.subscribe(new ConsumerCallback() {
@Override
public void onSucceed(Top250Bean result) {
// 成功获取数据
Toast.makeText(AppGlobals.getApplication().getApplicationContext(),
“成功了” + result, Toast.LENGTH_LONG).show();
}
@Override
public void onError(int code, String msg) {
// 获取数据失败
Toast.makeText(AppGlobals.getApplication().getApplicationContext(),
msg, Toast.LENGTH_LONG).show();
}
}, new ThrowableCallback() {
@Override
public void onError(HttpThrowable httpThrowable) {
// 获取数据失败
Log.i(TAG, httpThrowable.message);
}
});
break;
case 1://POST方法 @Body
ArrayMap<String, Object> map = new ArrayMap<>();
map.put(“jsCode”, “033sHaaK03pRwa2Bic9K0EmcaK0sHaaq”);
RetrofitClient.getInstance()
.create(ServiceApi.class)
.postCode2Session(map)
.subscribeOn(Schedulers.io())
.doOnSubscribe(disposable -> viewModel.showProgressDialog(“正在请求中”))
.observeOn(AndroidSchedulers.mainThread())
.compose(rxLifecycle.bindUntilEvent(Lifecycle.Event.ON_DESTROY))
.retryWhen(new RetryWithDelay(1, 1))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
.doAfterTerminate(() -> viewModel.hintProgressDialog())
.subscribe(new ObserverCallback() {
@Override
public void onSucceed(LoginBean result) {
// 成功获取数据
Toast.makeText(AppGlobals.getApplication().getApplicationContext(),
“成功了” + result, Toast.LENGTH_LONG).show();
}
@Override
public void onFail(int code, String msg) {
// 获取数据失败
Toast.makeText(AppGlobals.getApplication().getApplicationContext(),
msg, Toast.LENGTH_LONG).show();
}
@Override
public void onComplete() {
super.onComplete();
}
});
break;
case 2://使用Rxjava切换线程
Observable.empty().observeOn(Schedulers.io())
.doOnComplete(new Action() {
@Override
public void run() throws Throwable {
Log.i(TAG, “我是子线程:” + AppUtils.isMainThread());
Observable.timer(3, TimeUnit.SECONDS)
.observeOn(AndroidSchedulers.mainThread())
.doOnComplete(() -> msgManagement(10))
.subscribe();
Observable.empty().observeOn(AndroidSchedulers.mainThread())
.doOnComplete(() -> msgManagement(20)
).subscribe();
}
}).subscribe();
break;
case 3:
//Rxjava(一)emitter 发射器
/**
-
借鉴:给初学者的RxJava2.0教程(一)
-
https://www.jianshu.com/p/464fa025229e
-
规则:
-
1、上游可以发送无限个onNext, 下游也可以接收无限个onNext.
-
2、当上游发送了一个onComplete后, 上游onComplete之后的事件将会继续发送, 而下游收到onComplete事件之后将不再继续接收事件.
-
3、当上游发送了一个onError后, 上游onError之后的事件将继续发送, 而下游收到onError事件之后将不再继续接收事件.
-
4、上游可以不发送onComplete或onError.
-
5、最为关键的是onComplete和onError必须唯一并且互斥, 即不能发多个onComplete, 也不能发多个onError,
-
也不能先发一个onComplete, 然后再发一个onError, 反之亦然
-
注: 关于onComplete和onError唯一并且互斥这一点, 是需要自行在代码中进行控制, 如果你的代码逻辑中违背了这个规则,
-
**并不一定会导致程序崩溃. ** 比如发送多个onComplete是可以正常运行的, 依然是收到第一个onComplete就不再接收了,
-
但若是发送多个onError, 则收到第二个onError事件会导致程序会崩溃.
*/
Observable.create(new ObservableOnSubscribe() {
@Override
public void subscribe(@NonNull ObservableEmitter emitter) throws Throwable {
Log.e(TAG, “是否是主线程=>” + AppUtils.isMainThread());
emitter.onNext(“huangxiaoguo1”);
emitter.onNext(“huangxiaoguo2”);
emitter.onNext(“huangxiaoguo3”);
emitter.onComplete();
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer() {
private Disposable mDisposable;
/**
-
不带任何参数的subscribe() 表示下游不关心任何事件,你上游尽管发你的数据去吧, 老子可不管你发什么.
-
带有一个Consumer参数的方法表示下游只关心onNext事件, 其他的事件我假装没看见,
-
@param d
*/
@Override
public void onSubscribe(@NonNull Disposable d) {
Log.d(TAG, “subscribe”);
mDisposable = d;
}
@Override
public void onNext(@NonNull String s) {