From 88380d2ee7a6857c3d9dcae65eacc73f1da3e55d Mon Sep 17 00:00:00 2001 From: liqi Date: Thu, 16 Nov 2017 11:22:13 +0800 Subject: [PATCH] Support rxjava2 Support rxjava2 --- .../OnSubscribeTimerPeriodically.java | 158 -------------- .../build.gradle | 8 +- .../src/main/AndroidManifest.xml | 0 .../com/liqi/nohttputils/RxNoHttpUtils.java | 0 .../download/DownloadUrlEntity.java | 0 .../nohttputils/download/NohttpDownload.java | 0 .../download/NohttpDownloadConfig.java | 0 .../download/NohttpDownloadUtils.java | 0 .../download/NohttpDownloadWhatAlgorithm.java | 0 .../service/NohttpDownloadService.java | 0 .../interfa/DialogGetListener.java | 0 .../interfa/OnIsRequestListener.java | 0 .../interfa/OnRequestRxNoHttpListener.java | 0 .../liqi/nohttputils/nohttp/BinaryAllObj.java | 0 .../nohttputils/nohttp/BinaryFactory.java | 0 .../liqi/nohttputils/nohttp/NoHttpInit.java | 0 .../nohttputils/nohttp/RequestBeanObj.java | 0 .../com/liqi/nohttputils/nohttp/RxNoHttp.java | 63 ++++-- .../nohttp/RxPollNoHttpConfig.java | 16 +- .../nohttputils/nohttp/RxRequestConfig.java | 0 .../nohttp/RxRequestEntityBase.java | 0 .../nohttp/RxRequestInputStreamEntity.java | 0 .../nohttp/RxRequestJsonListEntity.java | 0 .../nohttp/RxRequestJsonObjectEntity.java | 0 .../nohttputils/nohttp/RxRequestOperate.java | 0 .../nohttp/RxRequestStringEntity.java | 0 .../nohttp/RxThreadInterchange.java | 60 +++-- .../nohttputils/nohttp/RxUtilsConfig.java | 0 .../nohttputils/nohttp/SSLContextUtil.java | 0 .../nohttp/gsonutils/JsonUtil.java | 0 .../OnToInputStreamEntityMethodListener.java | 0 .../OnToJsonListEntityMethodListener.java | 0 .../OnToJsonObjectEntityMethodListener.java | 0 .../OnToStringEntityMethodListener.java | 0 .../nohttp/rx_poll/RxPollUtils.java | 13 +- .../rx_poll/extend/ObservableExpand.java | 40 +--- .../OnRxInformationCancelListener.java | 0 .../interfa/OnRxInformationListListener.java | 0 .../OnRxPollConfigBuilderListener.java | 4 +- .../rx_poll/model/RxInformationModel.java | 0 .../rx_poll/model/RxInformationPoolModel.java | 31 ++- .../operators/OnObserverEventListener.java | 0 .../OnSubscribeTimerPeriodically.java | 206 ++++++++++++++++++ .../rx_poll/pond/RxInformationPool.java | 0 .../nohttp/rx_threadpool/RxMessageSource.java | 0 .../interfa/OnRxMessageGetListener.java | 0 .../interfa/OnRxMessageSetListener.java | 0 .../model/BaseRxRequestModel.java | 14 +- .../rx_threadpool/model/RxRequestModel.java | 0 .../thread/RxThreadDispatch.java | 0 .../utils/RxThreadPoolUtisl.java | 0 .../src/main/res/values/strings.xml | 0 sample/build.gradle | 2 +- .../java/com/liqi/nohttprxutils/PollDemo.java | 38 ++-- settings.gradle | 2 +- 55 files changed, 363 insertions(+), 292 deletions(-) delete mode 100644 nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/operators/OnSubscribeTimerPeriodically.java rename {nohttputils-r1 => nohttputils-r2}/build.gradle (82%) rename {nohttputils-r1 => nohttputils-r2}/src/main/AndroidManifest.xml (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/RxNoHttpUtils.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/download/DownloadUrlEntity.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/download/NohttpDownload.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/download/NohttpDownloadConfig.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/download/NohttpDownloadUtils.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/download/NohttpDownloadWhatAlgorithm.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/download/service/NohttpDownloadService.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/interfa/DialogGetListener.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/interfa/OnIsRequestListener.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/interfa/OnRequestRxNoHttpListener.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/BinaryAllObj.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/BinaryFactory.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/NoHttpInit.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/RequestBeanObj.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/RxNoHttp.java (84%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/RxPollNoHttpConfig.java (88%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/RxRequestConfig.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/RxRequestEntityBase.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/RxRequestInputStreamEntity.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/RxRequestJsonListEntity.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/RxRequestJsonObjectEntity.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/RxRequestOperate.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/RxRequestStringEntity.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/RxThreadInterchange.java (93%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/RxUtilsConfig.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/SSLContextUtil.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/gsonutils/JsonUtil.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToInputStreamEntityMethodListener.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToJsonListEntityMethodListener.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToJsonObjectEntityMethodListener.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToStringEntityMethodListener.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/RxPollUtils.java (89%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/extend/ObservableExpand.java (63%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/interfa/OnRxInformationCancelListener.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/interfa/OnRxInformationListListener.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/interfa/OnRxPollConfigBuilderListener.java (77%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/model/RxInformationModel.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/model/RxInformationPoolModel.java (91%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/operators/OnObserverEventListener.java (100%) create mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/operators/OnSubscribeTimerPeriodically.java rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/pond/RxInformationPool.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/RxMessageSource.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/interfa/OnRxMessageGetListener.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/interfa/OnRxMessageSetListener.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/model/BaseRxRequestModel.java (80%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/model/RxRequestModel.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/thread/RxThreadDispatch.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/utils/RxThreadPoolUtisl.java (100%) rename {nohttputils-r1 => nohttputils-r2}/src/main/res/values/strings.xml (100%) diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/operators/OnSubscribeTimerPeriodically.java b/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/operators/OnSubscribeTimerPeriodically.java deleted file mode 100644 index db123aa..0000000 --- a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/operators/OnSubscribeTimerPeriodically.java +++ /dev/null @@ -1,158 +0,0 @@ -package com.liqi.nohttputils.nohttp.rx_poll.operators; - -import java.util.concurrent.TimeUnit; - -import rx.Observable; -import rx.Scheduler; -import rx.Subscriber; -import rx.exceptions.Exceptions; -import rx.functions.Action0; -import rx.schedulers.Schedulers; - -/** - * 订阅间隔计时器对象 - * Created by LiQi on 2017/9/6. - */ - -public final class OnSubscribeTimerPeriodically implements Observable.OnSubscribe { - /** - * 初始化加载延迟 - */ - final long initialDelay; - /** - * 轮询间隔时间 - */ - final long period; - /** - * 时间单位 - */ - final TimeUnit unit; - /** - * 订阅者线程线路 - */ - final Scheduler scheduler; - /** - * 可观察者事件监听器 - */ - private OnObserverEventListener eventListener; - /** - * 可观察者线程线路-事件处理默认在子线程 - */ - private Scheduler eventScheduler = Schedulers.io(); - /** - * 传输给被观察者接受的对象 - */ - private V transferValue; - - public OnSubscribeTimerPeriodically(long initialDelay, long period, TimeUnit unit, Scheduler scheduler) { - this.initialDelay = initialDelay; - this.period = period; - this.unit = unit; - this.scheduler = scheduler; - } - - /** - * 赋值传输给被观察者接受的对象 - * - * @param transferValue 被观察者接受的对象 - */ - public void setTransferValue(V transferValue) { - this.transferValue = transferValue; - } - - /** - * 设置事件执行线程线路 - * - * @param eventScheduler 线程线路 - */ - public void setEventScheduler(Scheduler eventScheduler) { - if (null != eventScheduler) { - this.eventScheduler = eventScheduler; - } - } - - /** - * 设置可观察者事件监听器 - * - * @param eventListener 可观察者事件监听器 - */ - public void setOnObserverEventListener(OnObserverEventListener eventListener) { - this.eventListener = eventListener; - } - - @Override - public void call(final Subscriber subscriber) { - //子线程调度器执行 - final Scheduler.Worker worker = Schedulers.io().createWorker(); - subscriber.add(worker); - //开启子线程调度器定时器执行 - worker.schedulePeriodically(new Action0() { - @Override - public void call() { - try { - synchronized (OnSubscribeTimerPeriodically.this) { - //被观察事件处理的调度器 - final Scheduler.Worker workerEvent = eventScheduler.createWorker(); - workerEvent.schedule(new Action0() { - @Override - public void call() { - if (null != eventListener) { - try { - final T observerEvent = eventListener.onObserverEvent(transferValue); - - //观察者事件处理的调度器 - final Scheduler.Worker workerDispose = scheduler.createWorker(); - workerDispose.schedule(new Action0() { - @Override - public void call() { - try { - subscriber.onNext(observerEvent); - awake(); - } catch (Throwable e) { - try { - worker.unsubscribe(); - workerEvent.unsubscribe(); - workerDispose.unsubscribe(); - awake(); - } finally { - Exceptions.throwOrReport(e, subscriber); - } - } - } - }); - } catch (Throwable e) { - try { - worker.unsubscribe(); - workerEvent.unsubscribe(); - awake(); - } finally { - Exceptions.throwOrReport(e, subscriber); - } - } - } - } - }); - //当前线程休眠,等待"被观察者"事件逻辑处理完毕 - OnSubscribeTimerPeriodically.this.wait(); - } - } catch (Throwable e) { - try { - worker.unsubscribe(); - } finally { - Exceptions.throwOrReport(e, subscriber); - } - } - } - - }, initialDelay, period, unit); - } - - /** - * 观察者已经根据被观察者的动作做出相应处理后唤醒调度器定时器继续往下走 - */ - private void awake() { - synchronized (OnSubscribeTimerPeriodically.this) { - OnSubscribeTimerPeriodically.this.notify(); - } - } -} diff --git a/nohttputils-r1/build.gradle b/nohttputils-r2/build.gradle similarity index 82% rename from nohttputils-r1/build.gradle rename to nohttputils-r2/build.gradle index 2438b4b..f18ed10 100644 --- a/nohttputils-r1/build.gradle +++ b/nohttputils-r2/build.gradle @@ -8,8 +8,8 @@ android { defaultConfig { minSdkVersion 11 targetSdkVersion 23 - versionCode 1 - versionName "1" + versionCode 2 + versionName "2" } buildTypes { release { @@ -24,7 +24,7 @@ dependencies { testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.4.0' compile 'com.yanzhenjie.nohttp:okhttp:1.1.4' - compile 'io.reactivex:rxandroid:1.2.1' - compile 'io.reactivex:rxjava:1.1.9' + compile 'io.reactivex.rxjava2:rxandroid:2.0.1' + compile 'io.reactivex.rxjava2:rxjava:2.1.5' compile 'com.google.code.gson:gson:2.8.0' } diff --git a/nohttputils-r1/src/main/AndroidManifest.xml b/nohttputils-r2/src/main/AndroidManifest.xml similarity index 100% rename from nohttputils-r1/src/main/AndroidManifest.xml rename to nohttputils-r2/src/main/AndroidManifest.xml diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/RxNoHttpUtils.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/RxNoHttpUtils.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/RxNoHttpUtils.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/RxNoHttpUtils.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/download/DownloadUrlEntity.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/download/DownloadUrlEntity.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/download/DownloadUrlEntity.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/download/DownloadUrlEntity.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/download/NohttpDownload.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/download/NohttpDownload.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/download/NohttpDownload.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/download/NohttpDownload.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/download/NohttpDownloadConfig.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/download/NohttpDownloadConfig.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/download/NohttpDownloadConfig.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/download/NohttpDownloadConfig.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/download/NohttpDownloadUtils.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/download/NohttpDownloadUtils.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/download/NohttpDownloadUtils.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/download/NohttpDownloadUtils.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/download/NohttpDownloadWhatAlgorithm.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/download/NohttpDownloadWhatAlgorithm.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/download/NohttpDownloadWhatAlgorithm.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/download/NohttpDownloadWhatAlgorithm.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/download/service/NohttpDownloadService.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/download/service/NohttpDownloadService.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/download/service/NohttpDownloadService.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/download/service/NohttpDownloadService.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/interfa/DialogGetListener.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/interfa/DialogGetListener.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/interfa/DialogGetListener.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/interfa/DialogGetListener.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/interfa/OnIsRequestListener.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/interfa/OnIsRequestListener.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/interfa/OnIsRequestListener.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/interfa/OnIsRequestListener.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/interfa/OnRequestRxNoHttpListener.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/interfa/OnRequestRxNoHttpListener.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/interfa/OnRequestRxNoHttpListener.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/interfa/OnRequestRxNoHttpListener.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/BinaryAllObj.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/BinaryAllObj.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/BinaryAllObj.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/BinaryAllObj.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/BinaryFactory.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/BinaryFactory.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/BinaryFactory.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/BinaryFactory.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/NoHttpInit.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/NoHttpInit.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/NoHttpInit.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/NoHttpInit.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/RequestBeanObj.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RequestBeanObj.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/RequestBeanObj.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RequestBeanObj.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/RxNoHttp.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxNoHttp.java similarity index 84% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/RxNoHttp.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxNoHttp.java index ec2b4b3..cef01e1 100644 --- a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/RxNoHttp.java +++ b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxNoHttp.java @@ -21,10 +21,14 @@ import java.net.ConnectException; import java.net.ProtocolException; -import rx.Observable; -import rx.Subscriber; -import rx.android.schedulers.AndroidSchedulers; -import rx.schedulers.Schedulers; +import io.reactivex.Observable; +import io.reactivex.ObservableEmitter; +import io.reactivex.ObservableOnSubscribe; +import io.reactivex.Observer; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.annotations.NonNull; +import io.reactivex.disposables.Disposable; +import io.reactivex.schedulers.Schedulers; /** @@ -58,9 +62,9 @@ void request(final ProtocolRequest request, DialogGetListener mDialogG } } - Observable.create(new Observable.OnSubscribe>() { + Observable.create(new ObservableOnSubscribe>() { @Override - public void call(Subscriber> subscriberOut) { + public void subscribe(@NonNull ObservableEmitter> subscriberOut) throws Exception { // 最关键的就是用NoHttp的同步请求请求到response了,其它的都是rxjava做的,跟nohttp无关的。 Response response = NoHttp.startRequestSync(request); if (response.isSucceed() || response.isFromCache()) { @@ -68,18 +72,39 @@ public void call(Subscriber> subscriberOut) { } else { subscriberOut.onError(response.getException()); } - } }).subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) - .subscribe(new Subscriber>() { + .subscribe(new Observer>() { + private Disposable mDisposable; + + @Override + public void onSubscribe(@NonNull Disposable d) { + mDisposable = d; + } + @Override - public void onCompleted() { + public void onNext(@NonNull Response tResponse) { + mDisposable.dispose(); + // 关闭dialog. + if (null != dialog && dialog.isShowing()) { + try { + dialog.dismiss(); + } catch (Exception e1) { + Logger.e("Dialog-关闭异常:由于Dialog已经关闭或者依赖的Context不存在"); + } + } + + if (null != responseInterfa) { + responseInterfa.onNext(tResponse.get()); + } } @Override - public void onError(Throwable e) { + public void onError(@NonNull Throwable e) { + mDisposable.dispose(); + // 关闭dialog. if (null != dialog && dialog.isShowing()) { try { @@ -115,8 +140,8 @@ public void onError(Throwable e) { } if (TextUtils.isEmpty(anUnknownErrorHint)) { show(dialog, R.string.error_unknow); - }else{ - show(dialog,anUnknownErrorHint); + } else { + show(dialog, anUnknownErrorHint); } } @@ -127,19 +152,8 @@ public void onError(Throwable e) { } @Override - public void onNext(Response tResponse) { - // 关闭dialog. - if (null != dialog && dialog.isShowing()) { - try { - dialog.dismiss(); - } catch (Exception e1) { - Logger.e("Dialog-关闭异常:由于Dialog已经关闭或者依赖的Context不存在"); - } - } + public void onComplete() { - if (null != responseInterfa) { - responseInterfa.onNext(tResponse.get()); - } } }); } @@ -155,6 +169,7 @@ private void show(Dialog dialog, int stringId) { Toast.makeText(context, context.getResources().getString(stringId), Toast.LENGTH_SHORT).show(); } } + /** * 土司提示 * diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/RxPollNoHttpConfig.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxPollNoHttpConfig.java similarity index 88% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/RxPollNoHttpConfig.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxPollNoHttpConfig.java index 6c25565..9cf92fe 100644 --- a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/RxPollNoHttpConfig.java +++ b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxPollNoHttpConfig.java @@ -9,8 +9,8 @@ import com.liqi.nohttputils.nohttp.rx_poll.operators.OnObserverEventListener; import com.yanzhenjie.nohttp.rest.RestRequest; -import rx.functions.Action1; -import rx.functions.Func1; +import io.reactivex.functions.Consumer; +import io.reactivex.functions.Predicate; /** * NoHttp轮询配置类 @@ -29,7 +29,7 @@ public class RxPollNoHttpConfig { /** * 设置数据拦截监听对象 */ - private Func1, Boolean> mBooleanFunc1; + private Predicate> mBooleanFunc1; /** * 被观察者产生的行为事件监听器 */ @@ -37,7 +37,7 @@ public class RxPollNoHttpConfig { /** * 观察者根据被观察产生的行为做出相应处理监听器 */ - private Action1> mRxInformationModelAction1; + private Consumer> mRxInformationModelAction1; /** * 网络请求参数对象 */ @@ -54,7 +54,7 @@ public long getPeriod() { return mPeriod; } - public Func1, Boolean> getBooleanFunc1() { + public Predicate> getBooleanFunc1() { return mBooleanFunc1; } @@ -62,7 +62,7 @@ public RxRequestOperate getRxRequestOperate() { return mRxRequestOperate; } - public Action1> getRxInformationModelAction1() { + public Consumer> getRxInformationModelAction1() { return mRxInformationModelAction1; } @@ -123,7 +123,7 @@ public ConfigBuilder setPeriod(long period) { * @param booleanFunc1 设置数据拦截监听对象 * @return 构建轮询配置类 */ - public ConfigBuilder setBooleanFunc1(Func1, Boolean> booleanFunc1) { + public ConfigBuilder setBooleanFunc1(Predicate> booleanFunc1) { mRxPollNoHttpConfig.mBooleanFunc1 = booleanFunc1; return this; } @@ -134,7 +134,7 @@ public ConfigBuilder setBooleanFunc1(Func1, Boolean> bo * @param rxInformationModelAction1 观察者根据被观察产生的行为做出相应处理监听器 * @return 构建轮询配置类 */ - public ConfigBuilder setRxInformationModelAction1(Action1> rxInformationModelAction1) { + public ConfigBuilder setRxInformationModelAction1(Consumer> rxInformationModelAction1) { mRxPollNoHttpConfig.mRxInformationModelAction1 = rxInformationModelAction1; return this; } diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/RxRequestConfig.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestConfig.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/RxRequestConfig.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestConfig.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/RxRequestEntityBase.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestEntityBase.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/RxRequestEntityBase.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestEntityBase.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/RxRequestInputStreamEntity.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestInputStreamEntity.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/RxRequestInputStreamEntity.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestInputStreamEntity.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/RxRequestJsonListEntity.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestJsonListEntity.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/RxRequestJsonListEntity.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestJsonListEntity.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/RxRequestJsonObjectEntity.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestJsonObjectEntity.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/RxRequestJsonObjectEntity.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestJsonObjectEntity.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/RxRequestOperate.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestOperate.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/RxRequestOperate.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestOperate.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/RxRequestStringEntity.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestStringEntity.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/RxRequestStringEntity.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestStringEntity.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/RxThreadInterchange.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxThreadInterchange.java similarity index 93% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/RxThreadInterchange.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxThreadInterchange.java index 68b2d30..ffe5f17 100644 --- a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/RxThreadInterchange.java +++ b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxThreadInterchange.java @@ -26,10 +26,12 @@ import java.net.ConnectException; import java.net.ProtocolException; -import rx.Observable; -import rx.Subscriber; -import rx.android.schedulers.AndroidSchedulers; -import rx.schedulers.Schedulers; +import io.reactivex.Observable; +import io.reactivex.Observer; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.Disposable; +import io.reactivex.schedulers.Schedulers; + /** * 把数据源中的数据转换成rxJava线程的中转站对象 @@ -142,13 +144,40 @@ private void runRequest(final RxRequestModel baseRxRequestModel) { Observable.create(baseRxRequestModel) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) - .subscribe(new Subscriber() { + .subscribe(new Observer() { + private Disposable mDisposable; + + @Override + public void onSubscribe(@NonNull Disposable d) { + mDisposable = d; + } + @Override - public void onCompleted() { + public void onNext(@NonNull T t) { + mDisposable.dispose(); + + Dialog dialog = getDialog(baseRxRequestModel); + if (null != dialog && dialog.isShowing()) { + try { + dialog.dismiss(); + } catch (Exception e) { + Logger.e("Dialog-关闭异常:由于Dialog已经关闭或者依赖的Context不存在"); + } + } + + OnIsRequestListener onIsRequestListener = baseRxRequestModel.getOnIsRequestListener(); + if (null != onIsRequestListener) { + onIsRequestListener.onNext(t); + } + + baseRxRequestModel.clearAll(); + messageListDalete(); } @Override - public void onError(Throwable e) { + public void onError(@NonNull Throwable e) { + mDisposable.dispose(); + Dialog dialog = getDialog(baseRxRequestModel); if (null != dialog && dialog.isShowing()) { try { @@ -204,23 +233,8 @@ public void onError(Throwable e) { } @Override - public void onNext(T t) { - Dialog dialog = getDialog(baseRxRequestModel); - if (null != dialog && dialog.isShowing()) { - try { - dialog.dismiss(); - } catch (Exception e) { - Logger.e("Dialog-关闭异常:由于Dialog已经关闭或者依赖的Context不存在"); - } - } + public void onComplete() { - OnIsRequestListener onIsRequestListener = baseRxRequestModel.getOnIsRequestListener(); - if (null != onIsRequestListener) { - onIsRequestListener.onNext(t); - } - - baseRxRequestModel.clearAll(); - messageListDalete(); } }); } diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/RxUtilsConfig.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxUtilsConfig.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/RxUtilsConfig.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxUtilsConfig.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/SSLContextUtil.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/SSLContextUtil.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/SSLContextUtil.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/SSLContextUtil.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/gsonutils/JsonUtil.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/gsonutils/JsonUtil.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/gsonutils/JsonUtil.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/gsonutils/JsonUtil.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToInputStreamEntityMethodListener.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToInputStreamEntityMethodListener.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToInputStreamEntityMethodListener.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToInputStreamEntityMethodListener.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToJsonListEntityMethodListener.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToJsonListEntityMethodListener.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToJsonListEntityMethodListener.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToJsonListEntityMethodListener.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToJsonObjectEntityMethodListener.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToJsonObjectEntityMethodListener.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToJsonObjectEntityMethodListener.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToJsonObjectEntityMethodListener.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToStringEntityMethodListener.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToStringEntityMethodListener.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToStringEntityMethodListener.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToStringEntityMethodListener.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/RxPollUtils.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/RxPollUtils.java similarity index 89% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/RxPollUtils.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/RxPollUtils.java index 0650df8..8f7585b 100644 --- a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/RxPollUtils.java +++ b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/RxPollUtils.java @@ -15,10 +15,11 @@ import java.util.concurrent.TimeUnit; -import rx.android.schedulers.AndroidSchedulers; -import rx.functions.Action1; -import rx.functions.Func1; -import rx.schedulers.Schedulers; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.functions.Consumer; +import io.reactivex.functions.Predicate; +import io.reactivex.schedulers.Schedulers; + /** * RxJava轮询控制类 @@ -63,13 +64,13 @@ public void requestRxNoHttp() { informationPoolModel.setSign(sign); } //赋值拦截器 - Func1, Boolean> booleanFunc1 = mRxPollNoHttpConfig.getBooleanFunc1(); + Predicate> booleanFunc1 = mRxPollNoHttpConfig.getBooleanFunc1(); if (null != booleanFunc1) { informationPoolModel.setBooleanFunc1(booleanFunc1); } //设置观察者相应处理事件 - Action1> rxInformationModelAction1 = mRxPollNoHttpConfig.getRxInformationModelAction1(); + Consumer> rxInformationModelAction1 = mRxPollNoHttpConfig.getRxInformationModelAction1(); if (null != rxInformationModelAction1) { informationPoolModel.setRxInformationModelAction1(rxInformationModelAction1); } diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/extend/ObservableExpand.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/extend/ObservableExpand.java similarity index 63% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/extend/ObservableExpand.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/extend/ObservableExpand.java index 9dc89b7..ad38223 100644 --- a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/extend/ObservableExpand.java +++ b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/extend/ObservableExpand.java @@ -1,39 +1,23 @@ package com.liqi.nohttputils.nohttp.rx_poll.extend; -import android.support.annotation.NonNull; - import com.liqi.nohttputils.nohttp.rx_poll.operators.OnObserverEventListener; import com.liqi.nohttputils.nohttp.rx_poll.operators.OnSubscribeTimerPeriodically; import java.util.concurrent.TimeUnit; -import rx.Observable; -import rx.Scheduler; -import rx.plugins.RxJavaHooks; -import rx.schedulers.Schedulers; +import io.reactivex.Observable; +import io.reactivex.Scheduler; +import io.reactivex.plugins.RxJavaPlugins; +import io.reactivex.schedulers.Schedulers; + /** * 继承rxjava-Observable扩展类 * Created by LiQi on 2017/9/6. */ -public class ObservableExpand extends Observable { - - final OnSubscribeTimerPeriodically onSubscribeTimerPeriodically; - - /** - * Creates an Observable with a Function to execute when it is subscribed to. - *

- * Note: Use {@link #create(OnSubscribe)} to create an Observable, instead of this constructor, - * unless you specifically have a need for inheritance. - * - * @param f {@link OnSubscribe} to be executed when {@link #subscribe(Subscriber)} is called - */ - protected ObservableExpand(OnSubscribe f, OnSubscribeTimerPeriodically onSubscribe1) { - super(f); - onSubscribeTimerPeriodically = onSubscribe1; - } +public class ObservableExpand { /** @@ -53,7 +37,7 @@ public static Builder intervalPolling(long initialDelay, long perio /** * 订阅间隔计时Builder - * @param + * * @param */ public static class Builder { @@ -85,16 +69,16 @@ public Builder(long initialDelay, long period, TimeUnit unit, OnObserverEventLis * 设置可观察者监听器线程线路 * * @param eventScheduler 线程线路 - * @param transferValue 传输给被观察者对象 + * @param transferValue 待处理或者待传输的对象 * @return */ - public ObservableExpand subscribeOn(Scheduler eventScheduler, @NonNull V transferValue) { + public Observable subscribeOn(Scheduler eventScheduler, V transferValue) { - OnSubscribeTimerPeriodically timerPeriodically = new OnSubscribeTimerPeriodically<>(initialDelay, period, unit, Schedulers.computation()); + OnSubscribeTimerPeriodically timerPeriodically = new OnSubscribeTimerPeriodically<>(Math.max(0L, initialDelay), Math.max(0L, period), unit, Schedulers.computation()); timerPeriodically.setOnObserverEventListener(observerEventListener); timerPeriodically.setTransferValue(transferValue); timerPeriodically.setEventScheduler(eventScheduler); - return new ObservableExpand<>(RxJavaHooks.onCreate(timerPeriodically), timerPeriodically); + return RxJavaPlugins.onAssembly(timerPeriodically); } } -} +} \ No newline at end of file diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/interfa/OnRxInformationCancelListener.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/interfa/OnRxInformationCancelListener.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/interfa/OnRxInformationCancelListener.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/interfa/OnRxInformationCancelListener.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/interfa/OnRxInformationListListener.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/interfa/OnRxInformationListListener.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/interfa/OnRxInformationListListener.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/interfa/OnRxInformationListListener.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/interfa/OnRxPollConfigBuilderListener.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/interfa/OnRxPollConfigBuilderListener.java similarity index 77% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/interfa/OnRxPollConfigBuilderListener.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/interfa/OnRxPollConfigBuilderListener.java index a8e89f6..1a45af5 100644 --- a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/interfa/OnRxPollConfigBuilderListener.java +++ b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/interfa/OnRxPollConfigBuilderListener.java @@ -3,7 +3,7 @@ import com.liqi.nohttputils.nohttp.RxPollNoHttpConfig; import com.liqi.nohttputils.nohttp.rx_poll.model.RxInformationModel; -import rx.functions.Func1; +import io.reactivex.functions.Predicate; /** * RX轮询构建轮询配置类业务操作方法接口 @@ -17,5 +17,5 @@ public interface OnRxPollConfigBuilderListener { * @param booleanFunc1 设置数据拦截监听对象 * @return 构建轮询配置类 */ - RxPollNoHttpConfig.ConfigBuilder setBooleanFunc1(Func1, Boolean> booleanFunc1); + RxPollNoHttpConfig.ConfigBuilder setBooleanFunc1(Predicate> booleanFunc1); } diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/model/RxInformationModel.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/model/RxInformationModel.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/model/RxInformationModel.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/model/RxInformationModel.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/model/RxInformationPoolModel.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/model/RxInformationPoolModel.java similarity index 91% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/model/RxInformationPoolModel.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/model/RxInformationPoolModel.java index a695ee3..bd83816 100644 --- a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/model/RxInformationPoolModel.java +++ b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/model/RxInformationPoolModel.java @@ -24,10 +24,9 @@ import java.net.ConnectException; import java.net.ProtocolException; -import rx.android.schedulers.AndroidSchedulers; -import rx.functions.Action0; -import rx.functions.Action1; -import rx.functions.Func1; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.functions.Consumer; +import io.reactivex.functions.Predicate; /** * rxJava轮询操作数据源对象 @@ -42,9 +41,9 @@ public class RxInformationPoolModel { private RxInformationModel mRxInformationModel; - private Func1, Boolean> mBooleanFunc1; + private Predicate> mBooleanFunc1; private OnObserverEventListener, RxInformationModel> mOnObserverEventListener; - private Action1> mRxInformationModelAction1; + private Consumer> mRxInformationModelAction1; public RxInformationPoolModel(@NonNull OnIsRequestListener onIsRequestListener, DialogGetListener dialogGetListener, String anUnknownErrorHint) { mOnIsRequestListener = onIsRequestListener; @@ -62,9 +61,9 @@ public RxInformationPoolModel(@NonNull OnIsRequestListener onIsRequestListene */ private void initRxInformationModelAction1(final String anUnknownErrorHint) { - mRxInformationModelAction1 = new Action1>() { + mRxInformationModelAction1 = new Consumer>() { @Override - public void call(RxInformationModel tRxInformationModel) { + public void accept(RxInformationModel tRxInformationModel) throws Exception { Logger.e(mRestRequest.url() + ":轮询运行完毕"); Dialog dialog = null == mDialogGetListener ? null : mDialogGetListener.getDialog(); @@ -130,9 +129,9 @@ public void call(RxInformationModel tRxInformationModel) { * 内部实现是否取消轮询-拦截器 */ private void initBooleanFunc1() { - mBooleanFunc1 = new Func1, Boolean>() { + mBooleanFunc1 = new Predicate>() { @Override - public Boolean call(RxInformationModel tRxInformationModel) { + public boolean test(RxInformationModel tRxInformationModel) throws Exception { //拦截传输过来的对象为null之时,创建一个对象关闭临时关闭轮询对象 if (null == tRxInformationModel) { tRxInformationModel = new RxInformationModel<>(); @@ -155,9 +154,9 @@ public RxInformationModel onObserverEvent(RestRequest restRequest) { initTransitionModel(); if (null != mDialogGetListener) { //对话框放到主线程去运行 - AndroidSchedulers.mainThread().createWorker().schedule(new Action0() { + AndroidSchedulers.mainThread().createWorker().schedule(new Runnable() { @Override - public void call() { + public void run() { Dialog dialog = mDialogGetListener.getDialog(); if (null != dialog) { dialog.show(); @@ -192,7 +191,7 @@ public void call() { * * @return 数据拦截监听对象 */ - public Func1, Boolean> getBooleanFunc1() { + public Predicate> getBooleanFunc1() { return mBooleanFunc1; } @@ -201,7 +200,7 @@ public Func1, Boolean> getBooleanFunc1() { * * @param booleanFunc1 数据拦截监听对象 */ - public void setBooleanFunc1(Func1, Boolean> booleanFunc1) { + public void setBooleanFunc1(Predicate> booleanFunc1) { if (null != booleanFunc1) { mBooleanFunc1 = booleanFunc1; } @@ -230,7 +229,7 @@ public void setOnObserverEventListener(OnObserverEventListener, R * * @return 可观察者事件产生对应行动监听器 */ - public Action1> getRxInformationModelAction1() { + public Consumer> getRxInformationModelAction1() { return mRxInformationModelAction1; } @@ -239,7 +238,7 @@ public Action1> getRxInformationModelAction1() { * * @param rxInformationModelAction1 可观察者事件产生对应行动监听器 */ - public void setRxInformationModelAction1(Action1> rxInformationModelAction1) { + public void setRxInformationModelAction1(Consumer> rxInformationModelAction1) { mRxInformationModelAction1 = rxInformationModelAction1; } diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/operators/OnObserverEventListener.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/operators/OnObserverEventListener.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/operators/OnObserverEventListener.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/operators/OnObserverEventListener.java diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/operators/OnSubscribeTimerPeriodically.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/operators/OnSubscribeTimerPeriodically.java new file mode 100644 index 0000000..b0228ff --- /dev/null +++ b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/operators/OnSubscribeTimerPeriodically.java @@ -0,0 +1,206 @@ +package com.liqi.nohttputils.nohttp.rx_poll.operators; + +import com.yanzhenjie.nohttp.Logger; + +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; + +import io.reactivex.Observable; +import io.reactivex.Observer; +import io.reactivex.Scheduler; +import io.reactivex.disposables.Disposable; +import io.reactivex.internal.disposables.DisposableHelper; +import io.reactivex.schedulers.Schedulers; + + +/** + * 订阅者间隔计时器(轮询)对象 + * Created by LiQi on 2017/9/6. + */ + +public final class OnSubscribeTimerPeriodically extends Observable { + /** + * 初始化加载延迟 + */ + final long initialDelay; + /** + * 轮询间隔时间 + */ + final long period; + /** + * 时间单位 + */ + final TimeUnit unit; + /** + * 订阅者线程线路 + */ + final Scheduler scheduler; + /** + * 可观察者事件监听器 + */ + private OnObserverEventListener eventListener; + /** + * 可观察者线程线路-事件处理默认在子线程 + */ + private Scheduler eventScheduler = Schedulers.io(); + /** + * 传输给被观察者接受的对象 + */ + private V transferValue; + private Disposable disposable; + + public OnSubscribeTimerPeriodically(long initialDelay, long period, TimeUnit unit, Scheduler scheduler) { + this.initialDelay = initialDelay; + this.period = period; + this.unit = unit; + this.scheduler = scheduler; + } + + private static void toThrowableString(Throwable e) { + Logger.e("Expand-轮询异常捕获:" + e.toString()); + StackTraceElement[] stackTrace = e.getStackTrace(); + if (null != stackTrace) { + for (StackTraceElement traceElement : stackTrace) { + Logger.e("Expand-轮询异常捕获:" + traceElement.toString()); + } + } + } + + /** + * 赋值传输给被观察者接受的对象 + * + * @param transferValue 被观察者接受的对象 + */ + public void setTransferValue(V transferValue) { + this.transferValue = transferValue; + } + + /** + * 设置事件执行线程线路 + * + * @param eventScheduler 线程线路 + */ + public void setEventScheduler(Scheduler eventScheduler) { + if (null != eventScheduler) { + this.eventScheduler = eventScheduler; + } + } + + /** + * 设置可观察者事件监听器 + * + * @param eventListener 可观察者事件监听器 + */ + public void setOnObserverEventListener(OnObserverEventListener eventListener) { + this.eventListener = eventListener; + } + + /** + * 观察者已经根据被观察者的动作做出相应处理后唤醒调度器定时器继续往下走 + */ + private void awake() { + synchronized (OnSubscribeTimerPeriodically.this) { + OnSubscribeTimerPeriodically.this.notify(); + } + } + + @Override + protected void subscribeActual(Observer observer) { + + final Observer observerNew = observer; + + //子线程调度器执行 + final Scheduler.Worker worker = Schedulers.io().createWorker(); + + IntervalObserver is = new IntervalObserver() { + + @Override + void runStart() throws Exception { + synchronized (OnSubscribeTimerPeriodically.this) { + //被观察事件处理的调度器 + final Scheduler.Worker workerEvent = eventScheduler.createWorker(); + workerEvent.schedule(new Runnable() { + @Override + public void run() { + if (null != eventListener) { + try { + final T observerEvent = eventListener.onObserverEvent(transferValue); + + //观察者事件处理的调度器 + final Scheduler.Worker workerDispose = scheduler.createWorker(); + workerDispose.schedule(new Runnable() { + @Override + public void run() { + try { + observerNew.onNext(observerEvent); + end(); + } catch (Throwable e) { + disposable.dispose(); + disposable = null; + eventScheduler = null; + eventListener = null; + end(); + toThrowableString(e); + } + } + }); + } catch (Throwable e) { + disposable.dispose(); + disposable = null; + eventScheduler = null; + eventListener = null; + end(); + toThrowableString(e); + } + } + } + }); + //当前线程休眠,等待"被观察者"事件逻辑处理完毕 + OnSubscribeTimerPeriodically.this.wait(); + } + } + }; + observerNew.onSubscribe(is); + disposable = worker.schedulePeriodically(is, initialDelay, period, unit); + } + + private void end() { + awake(); + System.gc(); + } + + private static abstract class IntervalObserver + extends AtomicReference + implements Disposable, Runnable { + + + private static final long serialVersionUID = 346773832286157679L; + + private IntervalObserver() { + } + + @Override + public void dispose() { + DisposableHelper.dispose(this); + } + + @Override + public boolean isDisposed() { + return get() == DisposableHelper.DISPOSED; + } + + @Override + public void run() { + if (get() != DisposableHelper.DISPOSED) { + try { + runStart(); + } catch (Exception e) { + toThrowableString(e); + } + } + } + + + abstract void runStart() throws Exception; + } +} diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/pond/RxInformationPool.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/pond/RxInformationPool.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/pond/RxInformationPool.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/pond/RxInformationPool.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/RxMessageSource.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/RxMessageSource.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/RxMessageSource.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/RxMessageSource.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/interfa/OnRxMessageGetListener.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/interfa/OnRxMessageGetListener.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/interfa/OnRxMessageGetListener.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/interfa/OnRxMessageGetListener.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/interfa/OnRxMessageSetListener.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/interfa/OnRxMessageSetListener.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/interfa/OnRxMessageSetListener.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/interfa/OnRxMessageSetListener.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/model/BaseRxRequestModel.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/model/BaseRxRequestModel.java similarity index 80% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/model/BaseRxRequestModel.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/model/BaseRxRequestModel.java index 0be1cd3..d696c4d 100644 --- a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/model/BaseRxRequestModel.java +++ b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/model/BaseRxRequestModel.java @@ -1,14 +1,16 @@ package com.liqi.nohttputils.nohttp.rx_threadpool.model; -import rx.Observable; -import rx.Subscriber; +import io.reactivex.ObservableEmitter; +import io.reactivex.ObservableOnSubscribe; +import io.reactivex.annotations.NonNull; -/**所有要处理的数据基类 +/** + * 所有要处理的数据基类 * Created by LiQi on 2017/3/20. */ -public abstract class BaseRxRequestModel implements Observable.OnSubscribe { +public abstract class BaseRxRequestModel implements ObservableOnSubscribe { //是否需要打断线程标识 private boolean isRunOff; //是否已经有线程处理了 @@ -18,6 +20,7 @@ public abstract class BaseRxRequestModel implements Observable.OnSubscribe /** * 设置错误信息 + * * @param throwable 错误对象 */ protected void setThrowable(Throwable throwable) { @@ -42,8 +45,7 @@ public void setRunOff(boolean runOff) { } @Override - public void call(Subscriber subscriber) { - + public void subscribe(@NonNull ObservableEmitter subscriber) throws Exception { T t = run(); if (!isRunOff()) subscriber.onNext(t); diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/model/RxRequestModel.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/model/RxRequestModel.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/model/RxRequestModel.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/model/RxRequestModel.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/thread/RxThreadDispatch.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/thread/RxThreadDispatch.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/thread/RxThreadDispatch.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/thread/RxThreadDispatch.java diff --git a/nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/utils/RxThreadPoolUtisl.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/utils/RxThreadPoolUtisl.java similarity index 100% rename from nohttputils-r1/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/utils/RxThreadPoolUtisl.java rename to nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/utils/RxThreadPoolUtisl.java diff --git a/nohttputils-r1/src/main/res/values/strings.xml b/nohttputils-r2/src/main/res/values/strings.xml similarity index 100% rename from nohttputils-r1/src/main/res/values/strings.xml rename to nohttputils-r2/src/main/res/values/strings.xml diff --git a/sample/build.gradle b/sample/build.gradle index 39e7bd3..223afea 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -23,5 +23,5 @@ dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.4.0' - compile project(':nohttputils-r1') + compile project(':nohttputils-r2') } diff --git a/sample/src/main/java/com/liqi/nohttprxutils/PollDemo.java b/sample/src/main/java/com/liqi/nohttprxutils/PollDemo.java index 698ce6a..cda3852 100644 --- a/sample/src/main/java/com/liqi/nohttprxutils/PollDemo.java +++ b/sample/src/main/java/com/liqi/nohttprxutils/PollDemo.java @@ -12,8 +12,8 @@ import com.liqi.nohttputils.nohttp.rx_poll.operators.OnObserverEventListener; import com.yanzhenjie.nohttp.rest.RestRequest; -import rx.functions.Action1; -import rx.functions.Func1; +import io.reactivex.functions.Consumer; +import io.reactivex.functions.Predicate; /** * 轮询请求演示界面 @@ -212,23 +212,36 @@ public RxInformationModel onObserverEvent(RestRequest transferVa } }) - .setBooleanFunc1(new Func1,Boolean>() { + .setBooleanFunc1(new Predicate>() { +// @Override +// public Boolean call(RxInformationModel stringRxInformationModel) { +// Log.e("外部实现轮询拦截","外部实现轮询拦截>>>拦截状态:"+stringRxInformationModel.isStop()); +// return stringRxInformationModel.isStop(); +// } + @Override - public Boolean call(RxInformationModel stringRxInformationModel) { + public boolean test(RxInformationModel stringRxInformationModel) throws Exception { Log.e("外部实现轮询拦截","外部实现轮询拦截>>>拦截状态:"+stringRxInformationModel.isStop()); return stringRxInformationModel.isStop(); } + }) + .setRxInformationModelAction1(new Consumer>() { // @Override -// public boolean test(@NonNull RxInformationModel stringRxInformationModel) throws Exception { -// Log.e("外部实现轮询拦截","外部实现轮询拦截>>>拦截状态:"+stringRxInformationModel.isStop()); -// return stringRxInformationModel.isStop(); +// public void call(RxInformationModel stringRxInformationModel) { +// String data = stringRxInformationModel.getData(); +// Log.e("外部实现轮询完毕","外部实现轮询运行完毕>>>运行结果:"+data); +// if (!isDiy) { +// isDiy = true; +// content.setText("轮询名称:自定义轮询\n外部实现调用轮询结果:" + data); +// } else { +// String trim = content.getText().toString().trim(); +// content.setText(trim + "\n\n轮询名称:自定义轮询\n外部实现调用轮询结果:" + data); +// } // } - }) - .setRxInformationModelAction1(new Action1>() { @Override - public void call(RxInformationModel stringRxInformationModel) { + public void accept(RxInformationModel stringRxInformationModel) throws Exception { String data = stringRxInformationModel.getData(); Log.e("外部实现轮询完毕","外部实现轮询运行完毕>>>运行结果:"+data); if (!isDiy) { @@ -239,11 +252,6 @@ public void call(RxInformationModel stringRxInformationModel) { content.setText(trim + "\n\n轮询名称:自定义轮询\n外部实现调用轮询结果:" + data); } } - -// @Override -// public void accept(RxInformationModel stringRxInformationModel) throws Exception { -// -// } }) .switchPoll() .requestRxNoHttp(); diff --git a/settings.gradle b/settings.gradle index fdae6a5..ab2e898 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1 @@ -include ':sample', ':nohttputils-r1' +include ':sample', ':nohttputils-r2'