From 09dcb93002480650c9f164c30585f4b11edda7e6 Mon Sep 17 00:00:00 2001 From: liqi Date: Thu, 16 Nov 2017 10:59:35 +0800 Subject: [PATCH] Custom unknown error message Custom unknown error message --- nohttputils-r2/build.gradle | 30 - nohttputils-r2/src/main/AndroidManifest.xml | 11 - .../com/liqi/nohttputils/RxNoHttpUtils.java | 110 --- .../download/DownloadUrlEntity.java | 50 -- .../nohttputils/download/NohttpDownload.java | 184 ----- .../download/NohttpDownloadConfig.java | 211 ------ .../download/NohttpDownloadUtils.java | 86 --- .../download/NohttpDownloadWhatAlgorithm.java | 68 -- .../service/NohttpDownloadService.java | 342 ---------- .../interfa/DialogGetListener.java | 16 - .../interfa/OnIsRequestListener.java | 18 - .../interfa/OnRequestRxNoHttpListener.java | 9 - .../liqi/nohttputils/nohttp/BinaryAllObj.java | 150 ----- .../nohttputils/nohttp/BinaryFactory.java | 43 -- .../liqi/nohttputils/nohttp/NoHttpInit.java | 124 ---- .../nohttputils/nohttp/RequestBeanObj.java | 251 ------- .../com/liqi/nohttputils/nohttp/RxNoHttp.java | 184 ----- .../nohttp/RxPollNoHttpConfig.java | 162 ----- .../nohttputils/nohttp/RxRequestConfig.java | 637 ------------------ .../nohttp/RxRequestEntityBase.java | 63 -- .../nohttp/RxRequestInputStreamEntity.java | 28 - .../nohttp/RxRequestJsonListEntity.java | 42 -- .../nohttp/RxRequestJsonObjectEntity.java | 24 - .../nohttputils/nohttp/RxRequestOperate.java | 275 -------- .../nohttp/RxRequestStringEntity.java | 27 - .../nohttp/RxThreadInterchange.java | 310 --------- .../nohttputils/nohttp/RxUtilsConfig.java | 414 ------------ .../nohttputils/nohttp/SSLContextUtil.java | 109 --- .../nohttp/gsonutils/JsonUtil.java | 201 ------ .../OnToInputStreamEntityMethodListener.java | 21 - .../OnToJsonListEntityMethodListener.java | 48 -- .../OnToJsonObjectEntityMethodListener.java | 21 - .../OnToStringEntityMethodListener.java | 20 - .../nohttp/rx_poll/RxPollUtils.java | 98 --- .../rx_poll/extend/ObservableExpand.java | 84 --- .../OnRxInformationCancelListener.java | 27 - .../interfa/OnRxInformationListListener.java | 41 -- .../OnRxPollConfigBuilderListener.java | 21 - .../rx_poll/model/RxInformationModel.java | 64 -- .../rx_poll/model/RxInformationPoolModel.java | 318 --------- .../operators/OnObserverEventListener.java | 9 - .../OnSubscribeTimerPeriodically.java | 206 ------ .../rx_poll/pond/RxInformationPool.java | 90 --- .../nohttp/rx_threadpool/RxMessageSource.java | 112 --- .../interfa/OnRxMessageGetListener.java | 42 -- .../interfa/OnRxMessageSetListener.java | 52 -- .../model/BaseRxRequestModel.java | 63 -- .../rx_threadpool/model/RxRequestModel.java | 118 ---- .../thread/RxThreadDispatch.java | 147 ---- .../utils/RxThreadPoolUtisl.java | 28 - .../src/main/res/values/strings.xml | 14 - settings.gradle | 2 +- 52 files changed, 1 insertion(+), 5824 deletions(-) delete mode 100644 nohttputils-r2/build.gradle delete mode 100644 nohttputils-r2/src/main/AndroidManifest.xml delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/RxNoHttpUtils.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/download/DownloadUrlEntity.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/download/NohttpDownload.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/download/NohttpDownloadConfig.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/download/NohttpDownloadUtils.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/download/NohttpDownloadWhatAlgorithm.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/download/service/NohttpDownloadService.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/interfa/DialogGetListener.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/interfa/OnIsRequestListener.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/interfa/OnRequestRxNoHttpListener.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/BinaryAllObj.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/BinaryFactory.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/NoHttpInit.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RequestBeanObj.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxNoHttp.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxPollNoHttpConfig.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestConfig.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestEntityBase.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestInputStreamEntity.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestJsonListEntity.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestJsonObjectEntity.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestOperate.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestStringEntity.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxThreadInterchange.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxUtilsConfig.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/SSLContextUtil.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/gsonutils/JsonUtil.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToInputStreamEntityMethodListener.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToJsonListEntityMethodListener.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToJsonObjectEntityMethodListener.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToStringEntityMethodListener.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/RxPollUtils.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/extend/ObservableExpand.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/interfa/OnRxInformationCancelListener.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/interfa/OnRxInformationListListener.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/interfa/OnRxPollConfigBuilderListener.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/model/RxInformationModel.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/model/RxInformationPoolModel.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/operators/OnObserverEventListener.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/operators/OnSubscribeTimerPeriodically.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/pond/RxInformationPool.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/RxMessageSource.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/interfa/OnRxMessageGetListener.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/interfa/OnRxMessageSetListener.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/model/BaseRxRequestModel.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/model/RxRequestModel.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/thread/RxThreadDispatch.java delete mode 100644 nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/utils/RxThreadPoolUtisl.java delete mode 100644 nohttputils-r2/src/main/res/values/strings.xml diff --git a/nohttputils-r2/build.gradle b/nohttputils-r2/build.gradle deleted file mode 100644 index f18ed10..0000000 --- a/nohttputils-r2/build.gradle +++ /dev/null @@ -1,30 +0,0 @@ -apply plugin: 'com.android.library' -apply plugin: 'com.github.dcendents.android-maven' - -android { - compileSdkVersion 23 - buildToolsVersion "24.0.1" - - defaultConfig { - minSdkVersion 11 - targetSdkVersion 23 - versionCode 2 - versionName "2" - } - buildTypes { - release { - minifyEnabled false - proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' - } - } -} - -dependencies { - compile fileTree(include: ['*.jar'], dir: 'libs') - 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.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-r2/src/main/AndroidManifest.xml b/nohttputils-r2/src/main/AndroidManifest.xml deleted file mode 100644 index 0778222..0000000 --- a/nohttputils-r2/src/main/AndroidManifest.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/RxNoHttpUtils.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/RxNoHttpUtils.java deleted file mode 100644 index 1e17dbb..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/RxNoHttpUtils.java +++ /dev/null @@ -1,110 +0,0 @@ -package com.liqi.nohttputils; - -import android.content.Context; - -import com.liqi.nohttputils.nohttp.RxRequestConfig; -import com.liqi.nohttputils.nohttp.RxUtilsConfig; -import com.liqi.nohttputils.nohttp.rx_poll.pond.RxInformationPool; -import com.liqi.nohttputils.nohttp.rx_threadpool.RxMessageSource; -import com.liqi.nohttputils.nohttp.rx_threadpool.interfa.OnRxMessageSetListener; -import com.yanzhenjie.nohttp.NoHttp; - -/** - * NoHttpRxUtils网络请求静态调用对象 - *

- * 个人QQ:543945827 - * Nohttp作者群号:46523908 - * Created by LiQi on 2017/3/7. - */ - -public class RxNoHttpUtils { - /** - * 获取nohttp初始化对象 - * - * @param context - * @return - */ - public static RxUtilsConfig.ConfigBuilder rxNoHttpInit(Context context) { - return RxUtilsConfig.ConfigBuilder.getConfigBuilder(context); - } - - /** - * 获取nohttprx网络请求配置对象 - * - * @return - */ - public static RxRequestConfig.ConfigBuilder rxNohttpRequest() { - return new RxRequestConfig.ConfigBuilder(); - } - - /** - * 取消Sign对应的网络请求 - * - * @param sign - */ - public static void cancel(Object sign) { - getOnRxMessageSetListener().cancel(sign); - } - - /** - * 批量取消Sign对应的网络请求 - * - * @param sign - */ - public static void cancel(Object[] sign) { - getOnRxMessageSetListener().cancel(sign); - } - - /** - * 取消全部网络请求 - */ - public static void cancelAll() { - getOnRxMessageSetListener().cancelAll(); - } - - - /** - * 取消Sign对应的网络请求轮询 - * - * @param sign - */ - public static void cancelPoll(Object sign) { - RxInformationPool.getRxInformationPoolCancel().cancel(sign); - } - - /** - * 批量取消Sign对应的网络请求轮询 - * - * @param sign - */ - public static void cancelPoll(Object[] sign) { - RxInformationPool.getRxInformationPoolCancel().cancel(sign); - } - - /** - * 取消全部网络请求轮询 - */ - public static void cancelPollAll() { - RxInformationPool.getRxInformationPoolCancel().cancelAll(); - } - - /** - * 清除对应的key的缓存数据 - * - * @param cacheKey 缓存KEY - */ - public static void removeKeyCacheData(String cacheKey) { - NoHttp.getInitializeConfig().getCacheStore().remove(cacheKey); - } - - /** - * 清除所有缓存数据 - */ - public static void removeAllCacheData() { - NoHttp.getInitializeConfig().getCacheStore().clear(); - } - - private static OnRxMessageSetListener getOnRxMessageSetListener() { - return RxMessageSource.getRxMessageSource(); - } -} diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/download/DownloadUrlEntity.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/download/DownloadUrlEntity.java deleted file mode 100644 index e467384..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/download/DownloadUrlEntity.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.liqi.nohttputils.download; - - -import com.yanzhenjie.nohttp.Logger; - -/** - * 文件下载路径和存储文件名对象 - * Created by LiQi on 2017/2/23. - */ - -public class DownloadUrlEntity { - private String mDownloadUrL; - private String mFileName; - - DownloadUrlEntity() { - super(); - } - - public String getDownloadUrL() { - return mDownloadUrL; - } - - public void setDownloadUrL(String downloadUrL) { - mDownloadUrL = downloadUrL; - } - - public String getFileName() { - return null == mFileName || "".equals(mFileName) ? urlGetName() : mFileName; - } - - public void setFileName(String fileName) { - mFileName = fileName; - } - - /** - * 切割URl最尾部的内容,按“/”切割 - * - * @return - */ - private String urlGetName() { - String[] split = mDownloadUrL.split("/"); - if (null != split && split.length > 0) { - String name = split[split.length - 1]; - return name + ".nohttpDownload"; - } else { - Logger.e("下载地址按“/”切割错误,无法获取存储文件名"); - return "ErrorDownloading"; - } - } -} diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/download/NohttpDownload.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/download/NohttpDownload.java deleted file mode 100644 index 735f980..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/download/NohttpDownload.java +++ /dev/null @@ -1,184 +0,0 @@ -package com.liqi.nohttputils.download; - -import android.app.ActivityManager; -import android.content.ComponentName; -import android.content.Context; -import android.content.Intent; -import android.content.ServiceConnection; -import android.os.IBinder; - -import com.liqi.nohttputils.download.service.NohttpDownloadService; -import com.yanzhenjie.nohttp.Logger; - -/** - * 下载服务操作对象 - * Created by LiQi on 2017/2/23. - */ - -class NohttpDownload implements NohttpDownloadService.DownloadServiceFinishListener { - private static NohttpDownload mNohttpDownload; - private final String SERVICE = "com.liqi.nohttputils.download.service.NohttpDownloadService"; - private NohttpDownloadService mBindService; - private NohttpDownloadConfig mNohttpDownloadConfig; - private Context mContext; - /** - * 服务绑定回调接口 - */ - private ServiceConnection mServiceConnection = new ServiceConnection() { - // 出现意外的解绑服务回调此方法 - @Override - public void onServiceDisconnected(ComponentName name) { - Logger.e("下载服务解绑"); - } - - // 绑定成功的回调方法 - @Override - public void onServiceConnected(ComponentName name, IBinder service) { - mBindService = ((NohttpDownloadService.NohttpDownloadBinderService) service).getBindService(); - mBindService.setDownloadServiceFinishListener(NohttpDownload.this); - mBindService.start(mNohttpDownloadConfig); - } - }; - - private NohttpDownload() { - } - - static synchronized NohttpDownload getNohttpDownload() { - return mNohttpDownload = null == mNohttpDownload ? new NohttpDownload() : mNohttpDownload; - } - - NohttpDownload init(NohttpDownloadConfig nohttpDownloadConfig, Context context) { - mNohttpDownloadConfig = nohttpDownloadConfig; - mContext = context; - startService(); - return mNohttpDownload; - } - - /** - * 开启服务 - */ - private void startService() { - if (isServiceRunning()) { - if (null == mBindService) { - mContext.bindService(getService(), mServiceConnection, Context.BIND_AUTO_CREATE); - } else - mBindService.start(mNohttpDownloadConfig); - } else { - mContext.startService(getService()); - mContext.bindService(getService(), mServiceConnection, Context.BIND_AUTO_CREATE); - } - } - - /** - * 获取服务 - * - * @return - */ - private Intent getService() { - return new Intent(mContext, NohttpDownloadService.class); - } - - /** - * 判断下载服务是否已经开启 - * - * @return - */ - private boolean isServiceRunning() { - ActivityManager manager = (ActivityManager) mContext.getSystemService(mContext.ACTIVITY_SERVICE); - for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { - if (SERVICE.equals(service.service.getClassName())) { - return true; - } - } - return false; - } - - /** - * 暂停当前正在下载的任务 - */ - void cancelAll() { - if (null != mBindService) - mBindService.cancelAll(); - } - - /** - * 暂停当前指定下载的任务 - * - * @param downloadUrl 下载地址 - */ - void cancel(String downloadUrl) { - if (null != mBindService) - mBindService.cancel(downloadUrl); - } - - /** - * 开启当前所有下载任务 - */ - void startAllRequest() { - if (null != mBindService) - mBindService.startAllRequest(); - } - - /** - * 开启当前指定下载任务 - * - * @param downloadUrl 下载地址 - */ - void startRequest(String downloadUrl) { - if (null != mBindService) - mBindService.startRequest(downloadUrl); - } - - /** - * 清空当前下载请求对象,并停止服务 - */ - void clearAll() { - if (null != mBindService) { - mBindService.clearAll(); - mContext.unbindService(mServiceConnection); - mContext.stopService(getService()); - mBindService = null; - } - } - - /** - * 获取下载请求对应的what值 - * - * @param downloadUrl 下载地址 - * @return - */ - int getWhat(String downloadUrl) { - int what = -1; - if (null != mBindService) - - what = mBindService.getWhat(downloadUrl); - return what; - } - - /** - * 把指定的What从容器里面移除 - * - * @param downloadUrl 下载地址 - */ - void removeWhatData(String downloadUrl) { - if (null != mBindService) - mBindService.removeWhatData(downloadUrl); - } - - /** - * 清空What存储容器 - */ - void removeWhatAll() { - if (null != mBindService) - mBindService.removeWhatAll(); - } - - @Override - public void onFinish() { - mBindService.cancelAll(); - mContext.unbindService(mServiceConnection); - mBindService = null; - mContext = null; - mNohttpDownloadConfig = null; - } -} diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/download/NohttpDownloadConfig.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/download/NohttpDownloadConfig.java deleted file mode 100644 index f498a1d..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/download/NohttpDownloadConfig.java +++ /dev/null @@ -1,211 +0,0 @@ -package com.liqi.nohttputils.download; - -import android.app.Activity; - -import com.liqi.nohttputils.nohttp.RxUtilsConfig; -import com.yanzhenjie.nohttp.download.DownloadListener; - -import java.util.ArrayList; -import java.util.List; - -/** - * 文件下载配置对象 - * Created by LiQi on 2017/2/23. - */ - -public class NohttpDownloadConfig { - /** - * 线程池数量,并发任务的数量 - */ - private final int THREADPOOLSIZE = RxUtilsConfig.ConfigBuilder.getConfigBuilder().getRxUtilsConfig().getThreadPoolSize(); - /** - * 是否断点续传下载,默认断点续传 - */ - private boolean isRange = true; - /** - * 在指定的文件夹发现同名的文件是否删除后重新下载,true则删除重新下载,false则直接通知下载成功。 - */ - private boolean isDeleteOld = false; - /** - * 下载地址和存储文件名字集合 - */ - private List mDownloadUrlEntities; - /** - * 下载文件存储文件夹 - */ - private String mFileFolder; - /** - * 下载进度监听接口 - */ - private DownloadListener mDownloadListener; - /** - * 下载链接超时时间(默认以全局链接超时时间) - */ - private int mConnectTimeout = -1; - /** - * 读取超时时间(默认以全局读取超时时间) - */ - private int mReadTimeout = -1; - /** - * 请求失败重试计数 - */ - private int mRetryCount=-1; - - private NohttpDownloadConfig() { - mDownloadUrlEntities = null == mDownloadUrlEntities ? new ArrayList() : mDownloadUrlEntities; - } - - public int getTHREADPOOLSIZE() { - return THREADPOOLSIZE; - } - - public int getRetryCount() { - return mRetryCount; - } - - public int getConnectTimeout() { - if (mConnectTimeout > 0) { - return mConnectTimeout * 1000; - } else { - return mConnectTimeout; - } - } - - public int getReadTimeout() { - if (mReadTimeout > 0) { - return mReadTimeout * 1000; - } else { - return mReadTimeout; - } - } - - public boolean isRange() { - return isRange; - } - - public boolean isDeleteOld() { - return isDeleteOld; - } - - public List getDownloadUrlEntities() { - return mDownloadUrlEntities; - } - - public String getFileFolder() { - return mFileFolder; - } - - public DownloadListener getDownloadListener() { - return mDownloadListener; - } - - /** - * 下载文件构建器对象 - */ - public static class Build { - private NohttpDownloadConfig mNohttpDownloadConfig; - - Build() { - mNohttpDownloadConfig = new NohttpDownloadConfig(); - } - - /** - * 设置是否断点续传下载 - * - * @param isRange 是否断点续传下载 - * @return - */ - public Build setRange(boolean isRange) { - mNohttpDownloadConfig.isRange = isRange; - return this; - } - - /** - * 设置在指定的文件夹发现同名的文件是否删除后重新下载 - * - * @param isDeleteOld 是否删除后重新下载 - * @return - */ - public Build setDeleteOld(boolean isDeleteOld) { - mNohttpDownloadConfig.isDeleteOld = isDeleteOld; - return this; - } - - /** - * 添加下载文件参数 - * - * @param downloadUrl 下载地址 - * @param fileName 存储文件名字(如果为空,文件名字就从URL中截取) - * @return - */ - public Build addDownloadParameter(String downloadUrl, String fileName) { - if (null != downloadUrl && !"".equals(downloadUrl)) { - DownloadUrlEntity downloadUrlEntity = new DownloadUrlEntity(); - downloadUrlEntity.setDownloadUrL(downloadUrl); - downloadUrlEntity.setFileName(fileName); - mNohttpDownloadConfig.mDownloadUrlEntities.add(downloadUrlEntity); - } - return this; - } - - /** - * 设置下载进度监听接口 - * - * @param downloadListener 下载进度监听接口 - * @return - */ - public Build setDownloadListener(DownloadListener downloadListener) { - mNohttpDownloadConfig.mDownloadListener = downloadListener; - return this; - } - - /** - * 设置下载文件存储文件路径 - * - * @param fileFolder 下载文件存储文件路径 - * @return - */ - public Build setFileFolder(String fileFolder) { - mNohttpDownloadConfig.mFileFolder = fileFolder; - return this; - } - - /** - * 设置链接超时时间 - * - * @param connectTimeout 时间,单位秒 - * @return - */ - public Build setConnectTimeout(int connectTimeout) { - mNohttpDownloadConfig.mConnectTimeout = connectTimeout; - return this; - } - - /** - * 设置读取时间 - * - * @param readTimeout 时间,单位秒 - * @return - */ - public Build setReadTimeout(int readTimeout) { - mNohttpDownloadConfig.mReadTimeout = readTimeout; - return this; - } - - /** - * 设置请求失败重试计数。默认值是0,也就是说,失败后不会再次发起请求。 - * - * @param retryCount 重试计数 - * @return - */ - public Build setRetryCount(int retryCount) { - mNohttpDownloadConfig.mRetryCount = retryCount; - return this; - } - - public void satart(Activity activity) { - NohttpDownload.getNohttpDownload().init(mNohttpDownloadConfig, activity.getApplicationContext()); - } - } - -} diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/download/NohttpDownloadUtils.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/download/NohttpDownloadUtils.java deleted file mode 100644 index be9d3e0..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/download/NohttpDownloadUtils.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.liqi.nohttputils.download; - -/** - * 文件下载工具代理对象 - * - * 个人QQ:543945827 - * NoHttp作者群号:46523908 - * Created by LiQi on 2017/2/24. - */ - -public class NohttpDownloadUtils { - /** - * 获取下载请求构建器 - * - * @return - */ - public static NohttpDownloadConfig.Build getNohttpDownloadBuild() { - return new NohttpDownloadConfig.Build(); - } - - /** - * 暂停全部下载 - */ - public static void cancelAll() { - NohttpDownload.getNohttpDownload().cancelAll(); - } - - /** - * 暂停指定下载 - * - * @param downloadUrl 下载地址 - */ - public static void cancel(String downloadUrl) { - NohttpDownload.getNohttpDownload().cancel(downloadUrl); - } - - /** - * 恢复全部下载 - */ - public static void startAllRequest() { - NohttpDownload.getNohttpDownload().startAllRequest(); - } - - /** - * 恢复指定下载 - * - * @param downloadUrl 下载地址 - */ - public static void startRequest(String downloadUrl) { - NohttpDownload.getNohttpDownload().startRequest(downloadUrl); - } - - /** - * 清空当前下载请求对象,并停止服务 - */ - public static void clearAll() { - NohttpDownload.getNohttpDownload().clearAll(); - } - - /** - * 获取下载URL对象的What值 - * - * @param downloadUrl 下载地址 - * @return - */ - public static int getDownloadRequestsWhat(String downloadUrl) { - return NohttpDownload.getNohttpDownload().getWhat(downloadUrl); - } - - /** - * 移除下载地址对应的What - * - * @param downloadUrl 下载地址 - */ - public static void removeWhatData(String downloadUrl) { - NohttpDownload.getNohttpDownload().removeWhatData(downloadUrl); - } - - /** - * 移除全部下载What - * - */ - public static void removeWhatAll() { - NohttpDownload.getNohttpDownload().removeWhatAll(); - } -} diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/download/NohttpDownloadWhatAlgorithm.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/download/NohttpDownloadWhatAlgorithm.java deleted file mode 100644 index f19f683..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/download/NohttpDownloadWhatAlgorithm.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.liqi.nohttputils.download; - -import java.util.Hashtable; - -/** - * 生产下载地址对应唯一what算法对象 - * - * @author Liqi - */ -public class NohttpDownloadWhatAlgorithm { - private static NohttpDownloadWhatAlgorithm mBaseHandlerIdObj; - /** - * what值初始值 - */ - private int mWhatInit = 0x1; - - private NohttpDownloadWhatAlgorithm() { - } - - public synchronized static NohttpDownloadWhatAlgorithm getNohttpDownloadKeyFactory() { - return mBaseHandlerIdObj = null == mBaseHandlerIdObj ? new NohttpDownloadWhatAlgorithm() - : mBaseHandlerIdObj; - } - - public void setWhatInit(int whatInit) { - mWhatInit = whatInit; - } - - /** - * 获取下载地址对应的唯一What值 - * - * @param whats 存储What值集合 - * @param downloadUrl 下载地址 - * @return - */ - public int productionWhat(Hashtable whats, String downloadUrl) { - if (whats.containsKey(downloadUrl)) { - return whats.get(downloadUrl); - } else { - return addClassID(whats, downloadUrl); - } - } - - - /** - * 把对应的下載請求的What值存储进容器里面 - * - * @param downloadUrl - * @return - */ - private synchronized int addClassID(Hashtable whats, String downloadUrl) { - int values = getValues(whats); - synchronized (this) { - whats.put(downloadUrl, values); - } - return values; - } - - /** - * 保证容器里面的What值唯一性 - */ - private synchronized int getValues(Hashtable whats) { - if (whats.containsValue(++mWhatInit)) { - getValues(whats); - } - return mWhatInit; - } -} diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/download/service/NohttpDownloadService.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/download/service/NohttpDownloadService.java deleted file mode 100644 index f030dff..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/download/service/NohttpDownloadService.java +++ /dev/null @@ -1,342 +0,0 @@ -package com.liqi.nohttputils.download.service; - -import android.app.Service; -import android.content.Intent; -import android.os.Binder; -import android.os.IBinder; -import android.util.SparseArray; - -import com.liqi.nohttputils.download.DownloadUrlEntity; -import com.liqi.nohttputils.download.NohttpDownloadConfig; -import com.liqi.nohttputils.download.NohttpDownloadWhatAlgorithm; -import com.yanzhenjie.nohttp.Headers; -import com.yanzhenjie.nohttp.NoHttp; -import com.yanzhenjie.nohttp.download.DownloadListener; -import com.yanzhenjie.nohttp.download.DownloadQueue; -import com.yanzhenjie.nohttp.download.DownloadRequest; - -import java.util.Hashtable; -import java.util.List; -import java.util.Map; - -/** - * 下载服务 - *

- * 个人QQ:543945827 - * NoHttp作者群号:46523908 - * Created by LiQi on 2017/2/23. - */ - -public class NohttpDownloadService extends Service implements DownloadListener { - private NohttpDownloadBinderService mNohttpDownloadBinderService; - /** - * 指定线程池并发数量 - */ - private int threadPoolSize; - /** - * 下载任务列表。 - */ - private SparseArray mDownloadRequests; - /** - * 下载请求对应的what值容器 - */ - private Hashtable mWhats; - private DownloadListener mDownloadListener; - private DownloadServiceFinishListener mDownloadServiceFinishListener; - private DownloadQueue mDownloadQueue; - - public void setDownloadServiceFinishListener(DownloadServiceFinishListener downloadServiceFinishListener) { - mDownloadServiceFinishListener = downloadServiceFinishListener; - } - - /** - * 开启下载请求 - * - * @param nohttpDownloadConfig 配置文件 - */ - public void start(NohttpDownloadConfig nohttpDownloadConfig) { - mDownloadListener = nohttpDownloadConfig.getDownloadListener(); - threadPoolSize = nohttpDownloadConfig.getTHREADPOOLSIZE(); - addDownloadRequest(nohttpDownloadConfig); - } - - /** - * 添加进下载请求集合中 - * - * @param nohttpDownloadConfig 配置对象 - */ - private void addDownloadRequest(NohttpDownloadConfig nohttpDownloadConfig) { - List downloadUrlEntities = nohttpDownloadConfig.getDownloadUrlEntities(); - if (null != downloadUrlEntities && !downloadUrlEntities.isEmpty()) { - mDownloadRequests = null == mDownloadRequests ? new SparseArray() : mDownloadRequests; - mWhats = null == mWhats ? new Hashtable() : mWhats; - NohttpDownloadWhatAlgorithm.getNohttpDownloadKeyFactory().setWhatInit(getMapWhatInit()); - - for (int i = 0; i < downloadUrlEntities.size(); i++) { - DownloadUrlEntity downloadUrlEntity = downloadUrlEntities.get(i); - String downloadUrL = downloadUrlEntity.getDownloadUrL(); - //如果mWhats里面存在对应的what值就取出,如果没有就生产一个新的唯一what值 - int key = NohttpDownloadWhatAlgorithm.getNohttpDownloadKeyFactory().productionWhat(mWhats, downloadUrL); - DownloadRequest downloadRequest = mDownloadRequests.get(key); - //对应的what值有下载请求进入 - if (null != downloadRequest) { - //如果下载地址不一致,那么移除掉对应的下载请求 - if (!downloadUrL.equals(downloadRequest.url())) { - mDownloadRequests.get(key).cancel(); - mDownloadRequests.remove(key); - } - continue; - } - DownloadRequest request = NoHttp.createDownloadRequest( - downloadUrL, nohttpDownloadConfig.getFileFolder(), - downloadUrlEntity.getFileName(), nohttpDownloadConfig.isRange(), - nohttpDownloadConfig.isDeleteOld()); - - int readTimeout = nohttpDownloadConfig.getReadTimeout(); - if (readTimeout>0) { - request.setReadTimeout(readTimeout); - } - int connectTimeout = nohttpDownloadConfig.getConnectTimeout(); - if (connectTimeout>0) { - request.setConnectTimeout(connectTimeout); - } - int retryCount = nohttpDownloadConfig.getRetryCount(); - if (retryCount>0) { - request.setRetryCount(retryCount); - } - mDownloadRequests.put(key, request); - } - - startAllRequest(); - } - } - - /** - * 开启全部下载请求队列 - */ - public void startAllRequest() { - if (mDownloadRequests.size() > 0) { - for (int i = 0; i < mDownloadRequests.size(); i++) { - int what = mDownloadRequests.keyAt(i); - DownloadRequest downloadRequest = mDownloadRequests.get(what); - - if (downloadRequest.isCanceled()) { - int connectTimeout = downloadRequest.getConnectTimeout(); - int readTimeout = downloadRequest.getReadTimeout(); - int retryCount = downloadRequest.getRetryCount(); - downloadRequest = NoHttp.createDownloadRequest( - downloadRequest.url(), downloadRequest.getFileDir(), - downloadRequest.getFileName(), downloadRequest.isRange(), - downloadRequest.isDeleteOld()); - downloadRequest.setConnectTimeout(connectTimeout); - downloadRequest.setReadTimeout(readTimeout); - downloadRequest.setRetryCount(retryCount); - mDownloadRequests.put(what, downloadRequest); - } - if (!downloadRequest.isStarted()) { - getDownloadQueueInstance().add(what, downloadRequest, this); - } - } - } - } - - /** - * 开启指定下载请求队列 - */ - public void startRequest(String downloadUrl) { - if (null != mDownloadRequests && mDownloadRequests.size() > 0) { - if (mWhats.containsKey(downloadUrl)) { - int what = mWhats.get(downloadUrl); - DownloadRequest downloadRequest = mDownloadRequests.get(what); - if (null != downloadRequest) { - if (downloadRequest.isCanceled()) { - int connectTimeout = downloadRequest.getConnectTimeout(); - int readTimeout = downloadRequest.getReadTimeout(); - int retryCount = downloadRequest.getRetryCount(); - downloadRequest = NoHttp.createDownloadRequest( - downloadRequest.url(), downloadRequest.getFileDir(), - downloadRequest.getFileName(), downloadRequest.isRange(), - downloadRequest.isDeleteOld()); - downloadRequest.setConnectTimeout(connectTimeout); - downloadRequest.setReadTimeout(readTimeout); - downloadRequest.setRetryCount(retryCount); - mDownloadRequests.put(what, downloadRequest); - } - if (!downloadRequest.isStarted()) { - getDownloadQueueInstance().add(what, downloadRequest, this); - } - } - } - } - } - - /** - * 暂停当前所有正在下载的任务 - */ - public void cancelAll() { - if (null != mDownloadRequests && mDownloadRequests.size() > 0) { - for (int i = 0; i < mDownloadRequests.size(); i++) { - DownloadRequest downloadRequest = mDownloadRequests.valueAt(i); - if (null != downloadRequest && !downloadRequest.isCanceled()) - downloadRequest.cancel(); - } - } - } - - /** - * 暂停指定下载的任务 - */ - public void cancel(String downloadUrl) { - if (null != mDownloadRequests && mDownloadRequests.size() > 0) { - if (mWhats.containsKey(downloadUrl)) { - int what = mWhats.get(downloadUrl); - DownloadRequest downloadRequest = mDownloadRequests.get(what); - if (null != downloadRequest && !downloadRequest.isCanceled()) - downloadRequest.cancel(); - } - } - } - - /** - * 清空当前下载请求对象 - */ - public void clearAll() { - if (null != mDownloadRequests && mDownloadRequests.size() > 0) { - for (int i = 0; i < mDownloadRequests.size(); i++) { - DownloadRequest downloadRequest = mDownloadRequests.valueAt(i); - if (null != downloadRequest && !downloadRequest.isCanceled()) - downloadRequest.cancel(); - } - mDownloadRequests.clear(); - } - removeWhatAll(); - } - - /** - * 获取下载队列 - * - * @return - */ - private DownloadQueue getDownloadQueueInstance() { - threadPoolSize = threadPoolSize > 0 ? threadPoolSize : mDownloadRequests.size(); - if (null == mDownloadQueue) { - mDownloadQueue = NoHttp.newDownloadQueue(threadPoolSize); - } - return mDownloadQueue; - } - - /** - * 获取下载请求对应的what值 - * - * @param downloadUrl 下载地址 - * @return - */ - public int getWhat(String downloadUrl) { - if (null != mWhats && !mWhats.isEmpty()) { - if (mWhats.containsKey(downloadUrl)) - return mWhats.get(downloadUrl); - } - return -1; - } - - /** - * 把指定的What从容器里面移除 - * - * @param downloadUrl 下载地址 - */ - public void removeWhatData(String downloadUrl) { - if (null != mWhats && !mWhats.isEmpty()) { - if (mWhats.containsKey(downloadUrl)) - mWhats.remove(downloadUrl); - } - } - - /** - * 清空What存储容器 - */ - public void removeWhatAll() { - if (null != mWhats && !mWhats.isEmpty()) - mWhats.clear(); - } - - @Override - public IBinder onBind(Intent intent) { - return mNohttpDownloadBinderService = null == mNohttpDownloadBinderService ? new NohttpDownloadBinderService() : mNohttpDownloadBinderService; - } - - @Override - public void onCreate() { - super.onCreate(); - } - - @Override - public void onDestroy() { - super.onDestroy(); - } - - @Override - public void onDownloadError(int what, Exception exception) { - if (mDownloadListener != null) mDownloadListener.onDownloadError(what, exception); - } - - @Override - public void onStart(int what, boolean isResume, long rangeSize, Headers responseHeaders, long allCount) { - if (mDownloadListener != null) - mDownloadListener.onStart(what, isResume, rangeSize, responseHeaders, allCount); - } - - @Override - public void onProgress(int what, int progress, long fileCount, long speed) { - if (mDownloadListener != null) - mDownloadListener.onProgress(what, progress, fileCount, speed); - } - - @Override - public void onFinish(int what, String filePath) { - mDownloadRequests.remove(what); - if (mDownloadListener != null) mDownloadListener.onFinish(what, filePath); - if (mDownloadServiceFinishListener != null) { - if (mDownloadRequests.size() <= 0) { - mDownloadRequests = null; - mDownloadServiceFinishListener.onFinish(); - } - } - } - - /** - * 获取what初始化值 - * - * @return - */ - private int getMapWhatInit() { - int whatInit = 0x1; - if (!mWhats.isEmpty()) { - for (Map.Entry entry : mWhats.entrySet()) { - whatInit = entry.getValue(); - } - } - return whatInit; - } - - @Override - public void onCancel(int what) { - if (mDownloadListener != null) mDownloadListener.onCancel(what); - } - - /** - * 下载请求已经全部下载完毕接口 - */ - public interface DownloadServiceFinishListener { - void onFinish(); - } - - /** - * 获取NohttpDownloadService对象Binder类 - */ - public class NohttpDownloadBinderService extends Binder { - public NohttpDownloadService getBindService() { - return NohttpDownloadService.this; - } - } - -} diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/interfa/DialogGetListener.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/interfa/DialogGetListener.java deleted file mode 100644 index 1389b96..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/interfa/DialogGetListener.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.liqi.nohttputils.interfa; - -import android.app.Dialog; - -/** - * 获取dialog - * Created by LiQi on 2016/12/8. - */ -public interface DialogGetListener { - /** - * 获取dialog - * @return - */ - Dialog getDialog(); - -} diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/interfa/OnIsRequestListener.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/interfa/OnIsRequestListener.java deleted file mode 100644 index 76b0e4d..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/interfa/OnIsRequestListener.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.liqi.nohttputils.interfa; - -/**是否请求网络成功回调接口 - * Created by LiQi on 2016/12/8. - */ -public interface OnIsRequestListener { - /** - * 请求成功 - * @param response - */ - void onNext(T response); - - /** - * 请求失败 - * @param e - */ - void onError(Throwable e); -} diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/interfa/OnRequestRxNoHttpListener.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/interfa/OnRequestRxNoHttpListener.java deleted file mode 100644 index c904619..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/interfa/OnRequestRxNoHttpListener.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.liqi.nohttputils.interfa; - -/**网络请求对外暴露接口 - * Created by LiQi on 2017/9/12. - */ - -public interface OnRequestRxNoHttpListener { - void requestRxNoHttp(); -} diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/BinaryAllObj.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/BinaryAllObj.java deleted file mode 100644 index 6dc7bd6..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/BinaryAllObj.java +++ /dev/null @@ -1,150 +0,0 @@ -package com.liqi.nohttputils.nohttp; - -import android.graphics.Bitmap; -import android.util.Log; - -import com.yanzhenjie.nohttp.BasicBinary; -import com.yanzhenjie.nohttp.Logger; -import com.yanzhenjie.nohttp.tools.IOUtils; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; - -/** - * 上传文件对象 - * Created by LiQi on 2016/12/9. - */ -class BinaryAllObj extends BasicBinary { - - protected InputStream inputStream; - - /*==========inputStream形式传入==========*/ - - /** - * An input stream - * - * @param inputStream must be {@link FileInputStream}, {@link ByteArrayInputStream}. - * @param fileName file name. Had better pass this value, unless the server tube don't care about the file name. - */ - BinaryAllObj(InputStream inputStream, String fileName) { - this(inputStream, fileName, null); - } - - /** - * An input stream . - * - * @param inputStream must be {@link FileInputStream}, {@link ByteArrayInputStream}. - * @param fileName file name. Had better pass this value, unless the server tube don't care about the file name. - * @param mimeType content type. - */ - BinaryAllObj(InputStream inputStream, String fileName, String mimeType) { - super(fileName, mimeType); - this.inputStream = inputStream; - if (!(inputStream instanceof FileInputStream) && !(inputStream instanceof ByteArrayInputStream)) { - Log.e("Binary", "Binary was cancelled, because the InputStream must be FileInputStream or ByteArrayInputStream."); - super.cancel(); - } - } - - - /*==========File形式传入==========*/ - - /** - * File binary. - * - * @param file a file. - */ - BinaryAllObj(File file) { - this(file, file.getName(), null); - } - - /** - * File binary. - * - * @param file a file. - * @param fileName file name. - */ - BinaryAllObj(File file, String fileName) { - this(file, fileName, null); - } - - /** - * File binary. - * - * @param file a file. - * @param fileName file name. - * @param mimeType content type. - */ - BinaryAllObj(File file, String fileName, String mimeType) { - super(fileName, mimeType); - try { - this.inputStream = new FileInputStream(file); - } catch (FileNotFoundException e) { - Log.e("Binary", "Binary was cancelled, because the file does not exist."); - super.cancel(); - } - } - - - /* ==========bitmap形式传入==========*/ - - /** - * An input stream {@link Binary}. - * - * @param bitmap image. - * @param fileName file name. Had better pass this value, unless the server tube don't care about the file name. - */ - BinaryAllObj(Bitmap bitmap, String fileName) { - this(bitmap, fileName, null); - } - - /** - * An input stream {@link Binary}. - * - * @param bitmap image. - * @param fileName file name. Had better pass this value, unless the server tube don't care about the file name. - * @param mimeType such as: image/png. - */ - BinaryAllObj(Bitmap bitmap, String fileName, String mimeType) { - super(fileName, mimeType); - if (bitmap == null) - throw new IllegalArgumentException("Bitmap is null: " + fileName); - if (bitmap.isRecycled()) - throw new IllegalArgumentException("Bitmap is recycled: " + fileName + ", bitmap must be not recycled."); - inputStream = new ByteArrayInputStream(bitmap2ByteArray(bitmap)); - - } - - private byte[] bitmap2ByteArray(Bitmap bitmap) { - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream); - IOUtils.closeQuietly(outputStream); - return outputStream.toByteArray(); - } - - @Override - public void cancel() { - IOUtils.closeQuietly(inputStream); - super.cancel(); - } - - @Override - public long getBinaryLength() { - try { - return inputStream == null ? 0 : inputStream.available(); - } catch (IOException e) { - Logger.e(e); - } - return 0; - } - - @Override - protected InputStream getInputStream() throws IOException { - return inputStream; - } -} diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/BinaryFactory.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/BinaryFactory.java deleted file mode 100644 index 06e2680..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/BinaryFactory.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.liqi.nohttputils.nohttp; - -import android.graphics.Bitmap; - - -import com.yanzhenjie.nohttp.BasicBinary; - -import java.io.File; -import java.io.InputStream; - -/** - * 文件上传对象生存工厂 - * Created by LiQi on 2016/12/9. - */ -public class BinaryFactory { - public static BasicBinary getBinary(InputStream inputStream, String fileName) { - return new BinaryAllObj(inputStream, fileName); - } - - public static BasicBinary getBinary(InputStream inputStream, String fileName, String mimeType) { - return new BinaryAllObj(inputStream, fileName, mimeType); - } - - public static BasicBinary getBinary(File file) { - return new BinaryAllObj(file); - } - - public static BasicBinary getBinary(File file, String fileName) { - return new BinaryAllObj(file, fileName); - } - - public static BasicBinary getBinary(File file, String fileName, String mimeType) { - return new BinaryAllObj(file, fileName, mimeType); - } - - public static BasicBinary getBinary(Bitmap bitmap, String fileName) { - return new BinaryAllObj(bitmap, fileName); - } - - public BasicBinary getBinary(Bitmap bitmap, String fileName, String mimeType) { - return new BinaryAllObj(bitmap, fileName, mimeType); - } -} diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/NoHttpInit.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/NoHttpInit.java deleted file mode 100644 index 62baaa3..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/NoHttpInit.java +++ /dev/null @@ -1,124 +0,0 @@ -package com.liqi.nohttputils.nohttp; - -import android.content.Context; - -import com.liqi.nohttputils.interfa.DialogGetListener; -import com.yanzhenjie.nohttp.InitializationConfig; -import com.yanzhenjie.nohttp.Logger; -import com.yanzhenjie.nohttp.NetworkExecutor; -import com.yanzhenjie.nohttp.NoHttp; -import com.yanzhenjie.nohttp.OkHttpNetworkExecutor; -import com.yanzhenjie.nohttp.URLConnectionNetworkExecutor; -import com.yanzhenjie.nohttp.cache.DBCacheStore; -import com.yanzhenjie.nohttp.cookie.DBCookieStore; -import com.yanzhenjie.nohttp.ssl.SSLUtils; - -import java.io.InputStream; - -import javax.net.ssl.HostnameVerifier; -import javax.net.ssl.SSLSocketFactory; - -/** - * nohttp初始化辅助对象 - * Created by LiQi on 2017/3/7. - */ - -public class NoHttpInit { - //URLCONNECTION请求标识 - public static final int URLCONNECTION = -0X1; - //OKHTTP请求标识 - public static final int OKHTTP = -0X2; - - private static NoHttpInit mNoHttpInit; - /** - * 加载框获取接口 - */ - private DialogGetListener mDialogGetListener; - - private NoHttpInit() { - - } - - static NoHttpInit getNoHttpInit() { - return mNoHttpInit = null == mNoHttpInit ? new NoHttpInit() : mNoHttpInit; - } - - public DialogGetListener getDialogGetListener() { - return mDialogGetListener; - } - - /** - * 自定义初始化nohttp - * - * @return - */ - void init(RxUtilsConfig rxUtilsConfig) { - - // nohttp默认设置的初始化 - //NoHttp.initialize(context); - if (null != rxUtilsConfig) { - Context context = rxUtilsConfig.getContext(); - //Cookie管理监听。 - DBCookieStore.CookieStoreListener cookieStoreListener = rxUtilsConfig.getCookieStoreListener(); - DBCookieStore dbCookieStore = new DBCookieStore(context); - if (null != cookieStoreListener) { - dbCookieStore.setCookieStoreListener(cookieStoreListener); - } - - //网络请求方式 - NetworkExecutor networkExecutor; - switch (rxUtilsConfig.getRxRequestUtilsWhy()) { - case URLCONNECTION: - networkExecutor = new URLConnectionNetworkExecutor(); - break; - case OKHTTP: - networkExecutor = new OkHttpNetworkExecutor(); - break; - default: - networkExecutor = new OkHttpNetworkExecutor(); - break; - } - - InitializationConfig.Builder builder = rxUtilsConfig.getBuilder() - // nohttp底层注释=>设置全局连接超时时间,单位毫秒,默认10s。 - .connectionTimeout(rxUtilsConfig.getConnectTimeout()) - // nohttp底层注释=>设置全局服务器响应超时时间,单位毫秒,默认10s。 - .readTimeout(rxUtilsConfig.getReadTimeout()) - //(此处只支持缓存到数据库中) nohttp底层注释=>配置缓存,默认保存数据库DBCacheStore,保存到SD卡使用DiskCacheStore。 - .cacheStore(new DBCacheStore(context).setEnable(rxUtilsConfig.isDbEnable())) - // (此处只支持保存到数据库中)nohttp底层注释=>配置Cookie,默认保存数据库DBCookieStore,开发者可以自己实现。如果不使用缓存,设置false禁用。 - .cookieStore(dbCookieStore.setEnable(rxUtilsConfig.isCookieEnable())) - // 配置网络层,默认使用URLConnection,如果想用OkHttp:OkHttpNetworkExecutor。 - .networkExecutor(networkExecutor) - .retry(rxUtilsConfig.getRetry()); - - //是否可以使用SSL安全通讯 - boolean ssl = rxUtilsConfig.isSSL(); - if (ssl) { - InputStream inputStreamSSL = rxUtilsConfig.getInputStreamSSL(); - - SSLSocketFactory socketFactory; - //是否有证书 - if (null != inputStreamSSL) { - socketFactory = SSLContextUtil.getSSLContext(inputStreamSSL).getSocketFactory(); - } else { - socketFactory =SSLContextUtil.getDefaultSLLContext().getSocketFactory(); - } - builder.sslSocketFactory(socketFactory); - //主机名验证 - HostnameVerifier hostnameVerifier = rxUtilsConfig.getHostnameVerifier(); - if (null != hostnameVerifier) { - builder.hostnameVerifier(hostnameVerifier); - } else { - builder.hostnameVerifier(SSLContextUtil.HOSTNAME_VERIFIER); - } - } - - // 自定义配置初始化: - NoHttp.initialize(builder.build()); - Logger.setDebug(rxUtilsConfig.isDebug());// 开启NoHttp的调试模式, 配置后可看到请求过程、日志和错误信息。 - Logger.setTag(rxUtilsConfig.getDebugName());// 设置NoHttp打印Log的tag。 - mDialogGetListener = rxUtilsConfig.getDialogGetListener(); - } - } -} diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RequestBeanObj.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RequestBeanObj.java deleted file mode 100644 index b1395b3..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RequestBeanObj.java +++ /dev/null @@ -1,251 +0,0 @@ -/* - * Copyright © Yan Zhenjie. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.liqi.nohttputils.nohttp; - -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.widget.ImageView; - -import com.liqi.nohttputils.nohttp.gsonutils.JsonUtil; -import com.yanzhenjie.nohttp.Headers; -import com.yanzhenjie.nohttp.Logger; -import com.yanzhenjie.nohttp.RequestMethod; -import com.yanzhenjie.nohttp.rest.RestRequest; -import com.yanzhenjie.nohttp.rest.StringRequest; - -import org.json.JSONArray; -import org.json.JSONObject; - -import java.util.Locale; - -/** - *

自定义JavaBean请求。

- * - * @param 请求成功后的数据转换对象 - * Created by Liqi on 2016/10/15. - */ -class RequestBeanObj extends RestRequest { - - /** - * Decoding lock so that we don't decode more than one image at a time (to avoid OOM's). - */ - private static final Object DECODE_LOCK = new Object(); - private Class clazz; - //请求的bitmap最大宽度 - private int mMaxWidth = 1000; - //请求的bitmap最大高度 - private int mMaxHeight = 1000; - //bitmap配置 - private Bitmap.Config mDecodeConfig = Bitmap.Config.ARGB_8888; - //bitmap比例 - private ImageView.ScaleType mScaleType = ImageView.ScaleType.CENTER_INSIDE; - - RequestBeanObj(String url, Class clazz) { - this(url, RequestMethod.GET, clazz); - } - - RequestBeanObj(String url, RequestMethod requestMethod, Class clazz) { - super(url, requestMethod); - this.clazz = clazz; - } - - /** - * bitmap创建GET请求对象函数 - * - * @param url URL地址 - * @param maxWidth 请求的bitmap最大宽度 - * @param maxHeight 请求的bitmap最大高度 - * @param decodeConfig bitmap配置 - * @param scaleType bitmap比例 - */ - RequestBeanObj(String url, int maxWidth, int maxHeight, Bitmap.Config decodeConfig, ImageView.ScaleType scaleType, Class clazz) { - this(url, RequestMethod.GET, maxWidth, maxHeight, decodeConfig, scaleType, clazz); - } - - /** - * bitmap创建自定义请求对象函数 - * - * @param url URL地址 - * @param requestMethod 请求模式 - * @param maxWidth 请求的bitmap最大宽度 - * @param maxHeight 请求的bitmap最大高度 - * @param decodeConfig bitmap配置 - * @param scaleType bitmap比例 - */ - RequestBeanObj(String url, RequestMethod requestMethod, int maxWidth, int maxHeight, Bitmap.Config decodeConfig, ImageView.ScaleType scaleType, Class clazz) { - super(url, requestMethod); - if (maxWidth >= 0) - this.mMaxWidth = maxWidth; - if (maxHeight >= 0) - this.mMaxHeight = maxHeight; - if (null != decodeConfig) - this.mDecodeConfig = decodeConfig; - if (null != scaleType) - this.mScaleType = scaleType; - this.clazz = clazz; - setAccept("image/*"); - } - - private RequestBeanObj(String url, RequestMethod requestMethod) { - super(url, requestMethod); - } - - private static int getResizedDimension(int maxPrimary, int maxSecondary, int actualPrimary, int actualSecondary, ImageView.ScaleType scaleType) { - - // If no dominant value at all, just return the actual. - if ((maxPrimary == 0) && (maxSecondary == 0)) { - return actualPrimary; - } - - // If ScaleType.FIT_XY fill the whole rectangle, ignore ratio. - if (scaleType == ImageView.ScaleType.FIT_XY) { - if (maxPrimary == 0) { - return actualPrimary; - } - return maxPrimary; - } - - // If primary is unspecified, scale primary to match secondary's scaling ratio. - if (maxPrimary == 0) { - double ratio = (double) maxSecondary / (double) actualSecondary; - return (int) (actualPrimary * ratio); - } - - if (maxSecondary == 0) { - return maxPrimary; - } - - double ratio = (double) actualSecondary / (double) actualPrimary; - int resized = maxPrimary; - - // If ScaleType.CENTER_CROP fill the whole rectangle, preserve aspect ratio. - if (scaleType == ImageView.ScaleType.CENTER_CROP) { - if ((resized * ratio) < maxSecondary) { - resized = (int) (maxSecondary / ratio); - } - return resized; - } - - if ((resized * ratio) > maxSecondary) { - resized = (int) (maxSecondary / ratio); - } - return resized; - } - - // Visible for testing. - private int findBestSampleSize(int actualWidth, int actualHeight, int desiredWidth, int desiredHeight) { - double wr = (double) actualWidth / desiredWidth; - double hr = (double) actualHeight / desiredHeight; - double ratio = Math.min(wr, hr); - float n = 1.0f; - while ((n * 2) <= ratio) { - n *= 2; - } - return (int) n; - } - - @Override - public T parseResponse(Headers responseHeaders, byte[] responseBody) throws Exception { - String response = StringRequest.parseResponseString(responseHeaders, responseBody); - if (null != clazz) { - Logger.e("NohttpRxUtils服务器数据转换类型:" + clazz.getName()); - //不是bitmap和byte[]进入 - if (clazz != Bitmap.class && clazz != byte[].class) { - Logger.e("Http服务器响应数据:"+response); - - //不是JSONObject和JSONArray类型进入 - if (clazz != JSONObject.class && clazz != JSONArray.class) { - if (clazz == String.class) { - return (T) response; - } else { - // 这里如果数据格式错误,或者解析失败,会在失败的回调方法中返回 ParseError 异常。 - return JsonUtil.jsonToBean(response, clazz); - } - } else { - if (clazz == JSONArray.class) { - return (T) new JSONArray(response); - } else if (clazz == JSONObject.class) { - return (T) new JSONObject(response); - } - } - - } else { - //是bitmap类型就转换bitmap类型 - if (clazz == Bitmap.class) { - return getBitmap(responseBody); - } - //是byte[]类型就转换byte[]类型 - else if (clazz == byte[].class) { - return (T) (responseBody == null ? new byte[0] : responseBody); - } - } - } - return (T) response; - } - - /** - * 获取bitmap对象 - * - * @param responseBody bitmap对象数组 - * @return - */ - private T getBitmap(byte[] responseBody) { - synchronized (DECODE_LOCK) { - T bitmap = null; - if (responseBody != null) { - try { - bitmap = (T) doResponse(responseBody); - } catch (OutOfMemoryError e) { - String errorMessage = String.format(Locale.US, "Caught OOM for %d byte image, url=%s", responseBody.length, url()); - Logger.e(e, errorMessage); - } - } - return bitmap; - } - } - - /** - * The real guts of AnalyzeResponse. Broken out for readability. - */ - private Bitmap doResponse(byte[] byteArray) throws OutOfMemoryError { - BitmapFactory.Options decodeOptions = new BitmapFactory.Options(); - Bitmap bitmap; - if (mMaxWidth == 0 && mMaxHeight == 0) { - decodeOptions.inPreferredConfig = mDecodeConfig; - bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length, decodeOptions); - } else { - decodeOptions.inJustDecodeBounds = true; - BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length, decodeOptions); - int actualWidth = decodeOptions.outWidth; - int actualHeight = decodeOptions.outHeight; - - int desiredWidth = getResizedDimension(mMaxWidth, mMaxHeight, actualWidth, actualHeight, mScaleType); - int desiredHeight = getResizedDimension(mMaxHeight, mMaxWidth, actualHeight, actualWidth, mScaleType); - - decodeOptions.inJustDecodeBounds = false; - decodeOptions.inSampleSize = findBestSampleSize(actualWidth, actualHeight, desiredWidth, desiredHeight); - Bitmap tempBitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length, decodeOptions); - - if (tempBitmap != null && (tempBitmap.getWidth() > desiredWidth || tempBitmap.getHeight() > desiredHeight)) { - bitmap = Bitmap.createScaledBitmap(tempBitmap, desiredWidth, desiredHeight, true); - tempBitmap.recycle(); - } else { - bitmap = tempBitmap; - } - } - return bitmap; - } -} diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxNoHttp.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxNoHttp.java deleted file mode 100644 index cef01e1..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxNoHttp.java +++ /dev/null @@ -1,184 +0,0 @@ -package com.liqi.nohttputils.nohttp; - -import android.app.Dialog; -import android.content.Context; -import android.text.TextUtils; -import android.widget.Toast; - -import com.liqi.nohttputils.R; -import com.liqi.nohttputils.interfa.DialogGetListener; -import com.liqi.nohttputils.interfa.OnIsRequestListener; -import com.yanzhenjie.nohttp.Logger; -import com.yanzhenjie.nohttp.NoHttp; -import com.yanzhenjie.nohttp.error.NetworkError; -import com.yanzhenjie.nohttp.error.NotFoundCacheError; -import com.yanzhenjie.nohttp.error.TimeoutError; -import com.yanzhenjie.nohttp.error.URLError; -import com.yanzhenjie.nohttp.error.UnKnownHostError; -import com.yanzhenjie.nohttp.rest.ProtocolRequest; -import com.yanzhenjie.nohttp.rest.Response; - -import java.net.ConnectException; -import java.net.ProtocolException; - -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; - - -/** - * 用rxjava去请求nohttp - * Created by Liqi on 2016/12/08. - */ -class RxNoHttp { - private static RxNoHttp rxNoHttp; - - private RxNoHttp() { - - } - - synchronized static RxNoHttp getRxNoHttp() { - return rxNoHttp = (null == rxNoHttp ? new RxNoHttp() : rxNoHttp); - } - - /** - * 通过nohttp去请求 - * - * @param mDialogGetListener dialog获取接口 - * @param responseInterfa 请求成功或者失败回调对象 - */ - void request(final ProtocolRequest request, DialogGetListener mDialogGetListener, final OnIsRequestListener responseInterfa, final String anUnknownErrorHint) { - final Dialog dialog = null == mDialogGetListener ? null : mDialogGetListener.getDialog(); - if (null != dialog && !dialog.isShowing()) { - try { - dialog.show(); - } catch (Exception e) { - Logger.e("Dialog-显示异常:由于Dialog依赖的Context不是栈顶。"); - } - } - - Observable.create(new ObservableOnSubscribe>() { - @Override - public void subscribe(@NonNull ObservableEmitter> subscriberOut) throws Exception { - // 最关键的就是用NoHttp的同步请求请求到response了,其它的都是rxjava做的,跟nohttp无关的。 - Response response = NoHttp.startRequestSync(request); - if (response.isSucceed() || response.isFromCache()) { - subscriberOut.onNext(response); - } else { - subscriberOut.onError(response.getException()); - } - } - }).subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(new Observer>() { - private Disposable mDisposable; - - @Override - public void onSubscribe(@NonNull Disposable d) { - mDisposable = d; - } - - @Override - 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(@NonNull Throwable e) { - mDisposable.dispose(); - - // 关闭dialog. - if (null != dialog && dialog.isShowing()) { - try { - dialog.dismiss(); - } catch (Exception e1) { - Logger.e("Dialog-关闭异常:由于Dialog已经关闭或者依赖的Context不存在"); - } - } - - // 提示异常信息。 - if (e instanceof NetworkError) {// 网络不好 - show(dialog, R.string.error_please_check_network); - } else if (e instanceof TimeoutError) {// 请求超时 - show(dialog, R.string.error_timeout); - } else if (e instanceof UnKnownHostError) {// 找不到服务器 - show(dialog, R.string.error_not_found_server); - } else if (e instanceof URLError) {// URL是错的 - show(dialog, R.string.error_url_error); - } else if (e instanceof NotFoundCacheError) { - // 这个异常只会在仅仅查找缓存时没有找到缓存时返回 - show(dialog, R.string.error_not_found_cache); - } else if (e instanceof ProtocolException) { - show(dialog, R.string.error_system_unsupport_method); - } else if (e instanceof ConnectException) {//没有连接到服务器 - show(dialog, R.string.error_no_service); - } else { - Logger.e("NoHttpUtils捕获异常:" + e.toString()); - StackTraceElement[] stackTrace = e.getStackTrace(); - if (null != stackTrace) { - for (StackTraceElement traceElement : stackTrace) { - Logger.e("NoHttpUtils捕获异常:" + traceElement.toString()); - } - } - if (TextUtils.isEmpty(anUnknownErrorHint)) { - show(dialog, R.string.error_unknow); - } else { - show(dialog, anUnknownErrorHint); - } - } - - - if (null != responseInterfa) { - responseInterfa.onError(e); - } - } - - @Override - public void onComplete() { - - } - }); - } - - /** - * 土司提示 - * - * @param stringId 提示内容资源ID - */ - private void show(Dialog dialog, int stringId) { - if (null != dialog) { - Context context = dialog.getContext(); - Toast.makeText(context, context.getResources().getString(stringId), Toast.LENGTH_SHORT).show(); - } - } - - /** - * 土司提示 - * - * @param hint 提示内容 - */ - private void show(Dialog dialog, String hint) { - if (null != dialog) { - Context context = dialog.getContext(); - Toast.makeText(context, hint, Toast.LENGTH_SHORT).show(); - } - } -} diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxPollNoHttpConfig.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxPollNoHttpConfig.java deleted file mode 100644 index 9cf92fe..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxPollNoHttpConfig.java +++ /dev/null @@ -1,162 +0,0 @@ -package com.liqi.nohttputils.nohttp; - -import android.support.annotation.NonNull; - -import com.liqi.nohttputils.interfa.OnRequestRxNoHttpListener; -import com.liqi.nohttputils.nohttp.rx_poll.RxPollUtils; -import com.liqi.nohttputils.nohttp.rx_poll.interfa.OnRxPollConfigBuilderListener; -import com.liqi.nohttputils.nohttp.rx_poll.model.RxInformationModel; -import com.liqi.nohttputils.nohttp.rx_poll.operators.OnObserverEventListener; -import com.yanzhenjie.nohttp.rest.RestRequest; - -import io.reactivex.functions.Consumer; -import io.reactivex.functions.Predicate; - -/** - * NoHttp轮询配置类 - * Created by LiQi on 2017/9/12. - */ - -public class RxPollNoHttpConfig { - /** - * 初始化加载延迟 - */ - private long mInitialDelay; - /** - * 轮询间隔时间-默认3秒 - */ - private long mPeriod = 3 * 1000; - /** - * 设置数据拦截监听对象 - */ - private Predicate> mBooleanFunc1; - /** - * 被观察者产生的行为事件监听器 - */ - private OnObserverEventListener, RxInformationModel> mOnObserverEventListener; - /** - * 观察者根据被观察产生的行为做出相应处理监听器 - */ - private Consumer> mRxInformationModelAction1; - /** - * 网络请求参数对象 - */ - private RxRequestOperate mRxRequestOperate; - - private RxPollNoHttpConfig() { - } - - public long getInitialDelay() { - return mInitialDelay; - } - - public long getPeriod() { - return mPeriod; - } - - public Predicate> getBooleanFunc1() { - return mBooleanFunc1; - } - - public RxRequestOperate getRxRequestOperate() { - return mRxRequestOperate; - } - - public Consumer> getRxInformationModelAction1() { - return mRxInformationModelAction1; - } - - public OnObserverEventListener, RxInformationModel> getOnObserverEventListener() { - return mOnObserverEventListener; - } - - /** - * 构建轮询配置类 - * - * @param - */ - public static class ConfigBuilder implements OnRxPollConfigBuilderListener{ - private RxPollNoHttpConfig mRxPollNoHttpConfig; - - private ConfigBuilder() { - - } - - private ConfigBuilder(@NonNull RxRequestOperate requestOperate) { - mRxPollNoHttpConfig = new RxPollNoHttpConfig<>(); - mRxPollNoHttpConfig.mRxRequestOperate = requestOperate; - } - - public static ConfigBuilder getConfigBuilder(@NonNull RxRequestOperate requestOperate) { - return new ConfigBuilder<>(requestOperate); - } - - /** - * 设置初始化加载延迟 - * - * @param initialDelay 初始化加载延迟 (时间单位:毫秒) - * @return 构建轮询配置类 - */ - public ConfigBuilder setInitialDelay(long initialDelay) { - if (initialDelay >= 0) { - mRxPollNoHttpConfig.mInitialDelay = initialDelay; - } - return this; - } - - /** - * 设置轮询间隔时间-默认3秒 - * - * @param period 轮询间隔时间(时间单位:毫秒) - * @return 构建轮询配置类 - */ - public ConfigBuilder setPeriod(long period) { - if (period >= 0) { - mRxPollNoHttpConfig.mPeriod = period; - } - return this; - } - - /** - * 设置设置数据拦截监听对象 - * - * @param booleanFunc1 设置数据拦截监听对象 - * @return 构建轮询配置类 - */ - public ConfigBuilder setBooleanFunc1(Predicate> booleanFunc1) { - mRxPollNoHttpConfig.mBooleanFunc1 = booleanFunc1; - return this; - } - - /** - * 设置观察者根据被观察产生的行为做出相应处理监听器 - * - * @param rxInformationModelAction1 观察者根据被观察产生的行为做出相应处理监听器 - * @return 构建轮询配置类 - */ - public ConfigBuilder setRxInformationModelAction1(Consumer> rxInformationModelAction1) { - mRxPollNoHttpConfig.mRxInformationModelAction1 = rxInformationModelAction1; - return this; - } - - /** - * 设置被观察者产生的行为事件监听器 - * - * @param onObserverEventListener 被观察者产生的行为事件监听器 - * @return 构建轮询配置类 - */ - public OnRxPollConfigBuilderListener setOnObserverEventListener(OnObserverEventListener, RxInformationModel> onObserverEventListener) { - mRxPollNoHttpConfig.mOnObserverEventListener = onObserverEventListener; - return this; - } - - /** - * 转换成轮询请求类 - * - * @return 轮询开始请求类 - */ - public OnRequestRxNoHttpListener switchPoll() { - return RxPollUtils.getRxPollUtilsNoHttpPoll(mRxPollNoHttpConfig); - } - } -} diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestConfig.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestConfig.java deleted file mode 100644 index 6240cb6..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestConfig.java +++ /dev/null @@ -1,637 +0,0 @@ -package com.liqi.nohttputils.nohttp; - -import android.graphics.Bitmap; -import android.support.annotation.NonNull; -import android.widget.ImageView; - -import com.liqi.nohttputils.interfa.DialogGetListener; -import com.liqi.nohttputils.interfa.OnIsRequestListener; -import com.liqi.nohttputils.interfa.OnRequestRxNoHttpListener; -import com.liqi.nohttputils.nohttp.interfa.OnToInputStreamEntityMethodListener; -import com.liqi.nohttputils.nohttp.interfa.OnToJsonListEntityMethodListener; -import com.liqi.nohttputils.nohttp.interfa.OnToJsonObjectEntityMethodListener; -import com.liqi.nohttputils.nohttp.interfa.OnToStringEntityMethodListener; -import com.yanzhenjie.nohttp.Logger; -import com.yanzhenjie.nohttp.RequestMethod; -import com.yanzhenjie.nohttp.rest.CacheMode; - -import java.io.InputStream; -import java.util.HashMap; -import java.util.Map; - -/** - * 网络请求参数配置对象 - * Created by LiQi on 2017/3/7. - */ - -public class RxRequestConfig { - private RequestMethod mRequestMethod; - private String mUrl; - private Map mParameterMap; - private Map mMapHeader; - private int mMaxWidth = -1; - private int mMaxHeight = -1; - private Bitmap.Config mDecodeConfig; - private ImageView.ScaleType mScaleType; - private DialogGetListener mDialogGetListener; - private OnIsRequestListener mOnIsRequestListener; - private Class mClazz; - private Object mSign; - private boolean isQueue = true; - private int mConnectTimeout = -1; - private int mReadTimeout = -1; - private int mRetryCount = -1; - private String mCacheKey; - private CacheMode mCacheMode = CacheMode.DEFAULT; - private RxRequestEntityBase mRxRequestEntityBase; - private String mAnUnknownErrorHint; - - private RxRequestConfig() { - - } - - RxRequestConfig(Class clazz, OnIsRequestListener onIsRequestListener) { - mClazz = clazz; - mOnIsRequestListener = onIsRequestListener; - } - - public String getAnUnknownErrorHint() { - return mAnUnknownErrorHint; - } - - RxRequestEntityBase getRxRequestEntityBase() { - return mRxRequestEntityBase; - } - - String getCacheKey() { - return mCacheKey; - } - - CacheMode getCacheMode() { - return mCacheMode; - } - - int getRetryCount() { - return mRetryCount; - } - - int getConnectTimeout() { - if (mConnectTimeout > 0) { - return mConnectTimeout * 1000; - } else { - return mConnectTimeout; - } - } - - int getReadTimeout() { - if (mReadTimeout > 0) { - return mReadTimeout * 1000; - } else { - return mReadTimeout; - } - } - - String getUrl() { - return mUrl; - } - - RequestMethod getRequestMethod() { - return mRequestMethod; - } - - Map getParameterMap() { - return mParameterMap; - } - - Map getMapHeader() { - return mMapHeader; - } - - int getMaxWidth() { - return mMaxWidth; - } - - int getMaxHeight() { - return mMaxHeight; - } - - Bitmap.Config getDecodeConfig() { - return mDecodeConfig; - } - - ImageView.ScaleType getScaleType() { - return mScaleType; - } - - public Object getSign() { - return mSign; - } - - public boolean isQueue() { - return isQueue; - } - - public DialogGetListener getDialogGetListener() { - return mDialogGetListener = null == mDialogGetListener ? NoHttpInit.getNoHttpInit().getDialogGetListener() : mDialogGetListener; - } - - public OnIsRequestListener getOnIsRequestListener() { - return mOnIsRequestListener; - } - - Class getShiftDataClazz() { - return mClazz; - } - - public static class ConfigBuilder { - /** - * 请求模式 - */ - private RequestMethod mRequestMethod; - /** - * 请求URL - */ - private String mUrl; - /** - * 参数集合 - */ - private Map mParameterMap; - /** - * 请求头参数集合 - */ - private Map mMapHeader; - /** - * 请求的bitmap最大宽度 - */ - private int mMaxWidth = -1; - /** - * 请求的bitmap最大高度 - */ - private int mMaxHeight = -1; - /** - * bitmap配置 - */ - private Bitmap.Config mDecodeConfig; - /** - * bitmap比例 - */ - private ImageView.ScaleType mScaleType; - /** - * 加载框获取接口 - */ - private DialogGetListener mDialogGetListener; - /** - * 请求标识 - */ - private Object mSign; - /** - * 是否队列请求 - */ - private boolean isQueue = true; - /** - * 下载链接超时时间(默认以全局链接超时时间) - */ - private int mConnectTimeout = -1; - /** - * 读取超时时间(默认以全局读取超时时间) - */ - private int mReadTimeout = -1; - /** - * 请求失败重试计数 - */ - private int mRetryCount = -1; - /** - * 数据缓存对应的KEY(Nohttp底层默认是请求对应的url) - */ - private String mCacheKey; - /** - * 缓存模式(默认:CacheMode.DEFAULT) - */ - private CacheMode mCacheMode = CacheMode.DEFAULT; - /** - * 请求实体(body) - */ - private RxRequestEntityBase mRxRequestEntityBase; - /** - * 未知错误提示语 - */ - private String mAnUnknownErrorHint; - - public ConfigBuilder() { - try { - mAnUnknownErrorHint = RxUtilsConfig.ConfigBuilder.getConfigBuilder().getRxUtilsConfig().getAnUnknownErrorHint(); - } catch (Exception e) { - Logger.e("NoHttpUtils捕获异常:请先初始化框架"); - e.printStackTrace(); - } - } - - public ConfigBuilder get() { - mRequestMethod = RequestMethod.GET; - return this; - } - - public ConfigBuilder post() { - mRequestMethod = RequestMethod.POST; - return this; - } - - public ConfigBuilder put() { - mRequestMethod = RequestMethod.PUT; - return this; - } - - public ConfigBuilder delete() { - mRequestMethod = RequestMethod.DELETE; - return this; - } - - public ConfigBuilder head() { - mRequestMethod = RequestMethod.HEAD; - return this; - } - - public ConfigBuilder patch() { - mRequestMethod = RequestMethod.PATCH; - return this; - } - - public ConfigBuilder options() { - mRequestMethod = RequestMethod.OPTIONS; - return this; - } - - public ConfigBuilder trace() { - mRequestMethod = RequestMethod.TRACE; - return this; - } - - /** - * 设置网络请求地址 - * - * @param url - * @return - */ - public ConfigBuilder url(String url) { - mUrl = url; - return this; - } - - /** - * 添加请求参数 - * - * @param key 参数键 - * @param value 参数值 - * @return - */ - public ConfigBuilder addParameter(String key, Object value) { - mParameterMap = null == mParameterMap ? new HashMap() : mParameterMap; - mParameterMap.put(key, value); - return this; - } - - /** - * 添加请求参数 - * - * @param mapParameter 参数map集合 - * @return - */ - public ConfigBuilder addParameter(Map mapParameter) { - mParameterMap = null == mParameterMap ? new HashMap() : mParameterMap; - mParameterMap.putAll(mapParameter); - return this; - } - - /** - * 设置请求参数 - * - * @param mapParameter 请求参数Map集合 - * @return - */ - public ConfigBuilder setParameterMap(Map mapParameter) { - mParameterMap = mapParameter; - return this; - } - - /** - * 添加请求头 - * - * @param key 请求头键 - * @param value 请求头值 - * @return - */ - public ConfigBuilder addHeader(String key, String value) { - mMapHeader = null == mMapHeader ? new HashMap() : mMapHeader; - mMapHeader.put(key, value); - return this; - } - - /** - * 添加请求头 - * - * @param mapHeader 请求头Map集合 - * @return - */ - public ConfigBuilder addHeader(Map mapHeader) { - mMapHeader = null == mMapHeader ? new HashMap() : mMapHeader; - mMapHeader.putAll(mapHeader); - return this; - } - - /** - * 设置请求头 - * - * @param mapHeader 请求头map集合 - * @return - */ - public ConfigBuilder setHeaderMap(Map mapHeader) { - mMapHeader = mapHeader; - return this; - } - - /** - * 为HTTPS协议添加无证书的参数 - * - * @return - */ - public ConfigBuilder addHttpsIsCertificate() { - mParameterMap = null == mParameterMap ? new HashMap() : mParameterMap; - //https是否需要证书协议参数集合 - Map httpsMap = new HashMap<>(); - httpsMap.put(RxRequestOperate.HTTPS_CERTIFICATE_NO, null); - mParameterMap.put(RxRequestOperate.HTTPS_KEY, httpsMap); - return this; - } - - /** - * 为HTTPS协议添加证书的参数 - * - * @return - */ - public ConfigBuilder addHttpsIsCertificate(InputStream inputStream) { - mParameterMap = null == mParameterMap ? new HashMap() : mParameterMap; - //https是否需要证书协议参数集合 - Map httpsMap = new HashMap<>(); - httpsMap.put(RxRequestOperate.HTTPS_CERTIFICATE, inputStream); - mParameterMap.put(RxRequestOperate.HTTPS_KEY, httpsMap); - return this; - } - - /** - * 设置请求请求位图的最大宽高 - * - * @param maxWidth 框 - * @param maxHeight 高 - * @return - */ - public ConfigBuilder setBitmapMaxWH(int maxWidth, int maxHeight) { - mMaxWidth = maxWidth; - mMaxHeight = maxHeight; - return this; - } - - /** - * 设置请求位图的配置和比例 - * - * @param config bitmap配置 - * @param scaleType bitmap比例 - * @return - */ - public ConfigBuilder setBitmapConfigType(Bitmap.Config config, ImageView.ScaleType scaleType) { - mDecodeConfig = config; - mScaleType = scaleType; - return this; - } - - /** - * 设置请求加载框 - * - * @param dialogGetListener 加载框获取接口 - * @return - */ - public ConfigBuilder setDialogGetListener(DialogGetListener dialogGetListener) { - mDialogGetListener = dialogGetListener; - return this; - } - - /** - * 设置请求标识(必须唯一) - * - * @param sign 请求标识 - */ - public ConfigBuilder setSign(@NonNull Object sign) { - mSign = sign; - return this; - } - - /** - * 设置当前请求是否添加进Rx线程池队列中 - * - * @param queue - * @return - */ - public ConfigBuilder setQueue(boolean queue) { - isQueue = queue; - return this; - } - - /** - * 设置链接超时时间 - * - * @param connectTimeout 时间,单位秒 - * @return - */ - public ConfigBuilder setConnectTimeout(int connectTimeout) { - mConnectTimeout = connectTimeout; - return this; - } - - /** - * 设置读取时间 - * - * @param readTimeout 时间,单位秒 - * @return - */ - public ConfigBuilder setReadTimeout(int readTimeout) { - mReadTimeout = readTimeout; - return this; - } - - /** - * 设置请求失败重试计数。默认值是0,也就是说,失败后不会再次发起请求。 - * - * @param retryCount 重试计数 - * @return - */ - public ConfigBuilder setRetryCount(int retryCount) { - mRetryCount = retryCount; - return this; - } - - /** - * 设置缓存模式(默认:CacheMode.Default) - * - * @param cacheMode 缓存模式共五大缓存模式: - * 1:CacheMode.Default,实现http 304重定向缓存 NoHttp本身是实现了RFC2616,所以这里不用设置或者设置为DEFAULT。 - * 2:CacheMode.REQUEST_NETWORK_FAILED_READ_CACHE,当请求服务器失败的时候,读取缓存 请求服务器成功则返回服务器数据,如果请求服务器失败,读取缓存数据返回。 - * 3:CacheMode.IF_NONE_CACHE_REQUEST_NETWORK,如果发现有缓存直接成功,没有缓存才请求服务器 我们知道ImageLoader的核心除了内存优化外, - * 剩下一个就是发现把内地有图片则直接使用,没有则请求服务器,所以NoHttp这一点非常使用做一个ImageLoader。 - * 4:CacheMode.ONLY_REQUEST_NETWORK,仅仅请求网络 这里不会读取缓存,也不支持Http304。 - * 5:CacheMode.ONLY_READ_CACHE,仅仅读取缓存 仅仅读取缓存,不会请求网络和其它操作。 - * @return - */ - public ConfigBuilder setCacheMode(@NonNull CacheMode cacheMode) { - mCacheMode = cacheMode; - return this; - } - - /** - * 设置数据缓存对应的KEY(默认是请求对应的url) - * - * @param cacheKey key - * @return - */ - public ConfigBuilder setCacheKey(String cacheKey) { - mCacheKey = cacheKey; - return this; - } - - /** - * 设置未知错误提示语 - * - * @param anUnknownErrorHint 未知错误提示语 - * @return - */ - public ConfigBuilder setAnUnknownErrorHint(String anUnknownErrorHint) { - mAnUnknownErrorHint = anUnknownErrorHint; - return this; - } - - /** - * 设置Json请求对象实体对象 - * json样式:{"xx":"xxx","yy":"yyy"} - * - * @return - */ - public OnToJsonObjectEntityMethodListener requestJsonObjectEntity() { - mRxRequestEntityBase = new RxRequestJsonObjectEntity(); - mRxRequestEntityBase.setOnGetConfigBuilderListener(new RxRequestEntityBase.OnGetConfigBuilderListener() { - @Override - public ConfigBuilder getConfigBuilder() { - return ConfigBuilder.this; - } - }); - return (OnToJsonObjectEntityMethodListener) mRxRequestEntityBase; - } - - /** - * 设置Json请求集合实体对象 - * json样式:[{"xx":"xxx"},{"yy":"yyy"}] - * - * @return - */ - public OnToJsonListEntityMethodListener requestJsonListEntity() { - mRxRequestEntityBase = new RxRequestJsonListEntity(); - mRxRequestEntityBase.setOnGetConfigBuilderListener(new RxRequestEntityBase.OnGetConfigBuilderListener() { - @Override - public ConfigBuilder getConfigBuilder() { - return ConfigBuilder.this; - } - }); - return (OnToJsonListEntityMethodListener) mRxRequestEntityBase; - } - - /** - * 设置String请求实体对象 - * - * @param contentType Content-Type - * @return - */ - public OnToStringEntityMethodListener requestStringEntity(@NonNull String contentType) { - mRxRequestEntityBase = new RxRequestStringEntity(contentType); - mRxRequestEntityBase.setOnGetConfigBuilderListener(new RxRequestEntityBase.OnGetConfigBuilderListener() { - @Override - public ConfigBuilder getConfigBuilder() { - return ConfigBuilder.this; - } - }); - return (OnToStringEntityMethodListener) mRxRequestEntityBase; - } - - /** - * 请求输入流实体对象 - * - * @param contentType Content-Type - * @return - */ - public OnToInputStreamEntityMethodListener requestInputStreamEntity(@NonNull String contentType) { - mRxRequestEntityBase = new RxRequestInputStreamEntity(contentType); - mRxRequestEntityBase.setOnGetConfigBuilderListener(new RxRequestEntityBase.OnGetConfigBuilderListener() { - @Override - public ConfigBuilder getConfigBuilder() { - return ConfigBuilder.this; - } - }); - return (OnToInputStreamEntityMethodListener) mRxRequestEntityBase; - } - - /** - * 构建请求轮询处理类 - * - * @param clazz 请求成功后返回数据转换对象 - * @param onIsRequestListener 请求成功或者失败回调接口 - * @param - * @return - */ - public RxPollNoHttpConfig.ConfigBuilder builderPoll(@NonNull Class clazz, OnIsRequestListener onIsRequestListener) { - return RxPollNoHttpConfig.ConfigBuilder.getConfigBuilder(getRxRequestOperate(clazz, onIsRequestListener)); - } - - /** - * 创建请求参数处理对象 - * - * @param clazz 请求成功后返回数据转换对象 - * @param onIsRequestListener 请求成功或者失败回调接口 - * @param - * @return - */ - public OnRequestRxNoHttpListener builder(@NonNull Class clazz, OnIsRequestListener onIsRequestListener) { - return getRxRequestOperate(clazz, onIsRequestListener); - } - - /** - * 获取请求参数类 - * - * @param clazz 请求成功后返回数据转换对象 - * @param onIsRequestListener 请求成功或者失败回调接口 - * @param - * @return - */ - private RxRequestOperate getRxRequestOperate(@NonNull Class clazz, OnIsRequestListener onIsRequestListener) { - return new RxRequestOperate<>(getRxRequestConfig(clazz, onIsRequestListener)); - } - - private RxRequestConfig getRxRequestConfig(@NonNull Class clazz, OnIsRequestListener onIsRequestListener) { - RxRequestConfig requestConfig = new RxRequestConfig<>(clazz, onIsRequestListener); - requestConfig.mRequestMethod = mRequestMethod; - requestConfig.mUrl = mUrl; - requestConfig.mParameterMap = mParameterMap; - requestConfig.mMapHeader = mMapHeader; - requestConfig.mMaxWidth = mMaxWidth; - requestConfig.mMaxHeight = mMaxHeight; - requestConfig.mDecodeConfig = mDecodeConfig; - requestConfig.mScaleType = mScaleType; - requestConfig.mDialogGetListener = mDialogGetListener; - requestConfig.mSign = mSign; - requestConfig.isQueue = isQueue; - requestConfig.mConnectTimeout = mConnectTimeout; - requestConfig.mReadTimeout = mReadTimeout; - requestConfig.mRetryCount = mRetryCount; - requestConfig.mCacheKey = mCacheKey; - requestConfig.mCacheMode = mCacheMode; - requestConfig.mRxRequestEntityBase = mRxRequestEntityBase; - requestConfig.mAnUnknownErrorHint = mAnUnknownErrorHint; - return requestConfig; - } - } -} diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestEntityBase.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestEntityBase.java deleted file mode 100644 index 6b9a014..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestEntityBase.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.liqi.nohttputils.nohttp; - -import android.support.annotation.CallSuper; - -import java.io.InputStream; -import java.util.List; -import java.util.Map; - -/** - * 请求实体基类 - * Created by LiQi on 2017/5/18. - */ - -public class RxRequestEntityBase { - protected String mContentType; - protected String mStringEntity; - protected InputStream mInputStream; - protected Map mStringJsonMap; - protected OnGetConfigBuilderListener mOnGetConfigBuilderListener; - protected List> mJsonMapList; - - RxRequestEntityBase() { - - } - - List> getJsonMapList() { - return mJsonMapList; - } - - String getContentType() { - return mContentType; - } - - String getStringEntity() { - return mStringEntity; - } - - InputStream getInputStream() { - return mInputStream; - } - - Map getStringJsonMap() { - return mStringJsonMap; - } - - void setOnGetConfigBuilderListener(OnGetConfigBuilderListener onGetConfigBuilderListener) { - mOnGetConfigBuilderListener = onGetConfigBuilderListener; - } - - /** - * 切换对象到配置对象 - * - * @return - */ - @CallSuper - public RxRequestConfig.ConfigBuilder transitionToRequest() { - return mOnGetConfigBuilderListener.getConfigBuilder(); - } - - public interface OnGetConfigBuilderListener { - RxRequestConfig.ConfigBuilder getConfigBuilder(); - } -} diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestInputStreamEntity.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestInputStreamEntity.java deleted file mode 100644 index a155132..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestInputStreamEntity.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.liqi.nohttputils.nohttp; - -import android.support.annotation.NonNull; - -import com.liqi.nohttputils.nohttp.interfa.OnToInputStreamEntityMethodListener; - -import java.io.InputStream; - -/** - * 输入流请求实体 - * Created by LiQi on 2017/5/18. - */ -public class RxRequestInputStreamEntity extends RxRequestEntityBase implements OnToInputStreamEntityMethodListener { - - private RxRequestInputStreamEntity() { - - } - - RxRequestInputStreamEntity(@NonNull String contentType) { - mContentType = contentType; - } - - @Override - public RxRequestEntityBase addEntityInputStreamParameter(@NonNull InputStream inputStream) { - mInputStream = inputStream; - return this; - } -} diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestJsonListEntity.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestJsonListEntity.java deleted file mode 100644 index d756396..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestJsonListEntity.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.liqi.nohttputils.nohttp; - -import android.support.annotation.NonNull; - -import com.liqi.nohttputils.nohttp.interfa.OnToJsonListEntityMethodListener; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; - -/** - * 请求Json集合实体 - * Created by LiQi on 2017/5/18. - */ -public class RxRequestJsonListEntity extends RxRequestEntityBase - implements OnToJsonListEntityMethodListener, OnToJsonListEntityMethodListener.OnToJsonAddEntityMethodListener { - private Map mMapParameter; - - RxRequestJsonListEntity() { - mJsonMapList = null == mJsonMapList ? new ArrayList>() : mJsonMapList; - } - - public OnToJsonAddEntityMethodListener addObjectEntityParameter(String keyParameter,@NonNull Object valueParameter) { - mMapParameter = new HashMap<>(); - mMapParameter.put(keyParameter, valueParameter); - return this; - } - - public OnToJsonAddEntityMethodListener addEntityParameter(String keyParameter, @NonNull Object valueParameter) { - if (null != mMapParameter) { - mMapParameter.put(keyParameter, valueParameter); - } - return this; - } - - public OnToJsonListEntityMethodListener objectBrushIntoList() { - if (null != mMapParameter) { - mJsonMapList.add(mMapParameter); - } - return this; - } -} diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestJsonObjectEntity.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestJsonObjectEntity.java deleted file mode 100644 index e848b73..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestJsonObjectEntity.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.liqi.nohttputils.nohttp; - -import android.support.annotation.NonNull; - -import com.liqi.nohttputils.nohttp.interfa.OnToJsonObjectEntityMethodListener; - -import java.util.HashMap; - -/** - * 请求Json实体 - * Created by LiQi on 2017/5/18. - */ -public class RxRequestJsonObjectEntity extends RxRequestEntityBase implements OnToJsonObjectEntityMethodListener { - - RxRequestJsonObjectEntity() { - mStringJsonMap = null == mStringJsonMap ? new HashMap() : mStringJsonMap; - } - - @Override - public RxRequestJsonObjectEntity addEntityParameter(String entityKey, @NonNull Object entityValu) { - mStringJsonMap.put(entityKey, entityValu); - return this; - } -} diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestOperate.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestOperate.java deleted file mode 100644 index 4aa3dac..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestOperate.java +++ /dev/null @@ -1,275 +0,0 @@ -package com.liqi.nohttputils.nohttp; - -import android.graphics.Bitmap; -import android.support.annotation.NonNull; - -import com.liqi.nohttputils.interfa.OnRequestRxNoHttpListener; -import com.liqi.nohttputils.nohttp.gsonutils.JsonUtil; -import com.liqi.nohttputils.nohttp.rx_threadpool.RxMessageSource; -import com.liqi.nohttputils.nohttp.rx_threadpool.model.RxRequestModel; -import com.yanzhenjie.nohttp.Binary; -import com.yanzhenjie.nohttp.Headers; -import com.yanzhenjie.nohttp.Logger; -import com.yanzhenjie.nohttp.rest.RestRequest; - -import java.io.File; -import java.io.InputStream; -import java.util.List; -import java.util.Map; - -/** - * nohttp请求参数处理对象 - * Created by LiQi on 2016/11/7. - */ -public class RxRequestOperate implements OnRequestRxNoHttpListener { - /** - * HTTPS请求map中key标识-->值必须为map - * 例子: - * 参数集合 :Map parameterMap=new HashMap(); - * Https参数集合:Map httpsMap=new HashMap(); - * 需要证书格式添加参数 - * httpsMap.put(HTTPS_CERTIFICATE,InputStream); - * 不需要证书格式添加参数 - * httpsMap.put(HTTPS_CERTIFICATE_NO,null); - *

- * //把https集合放进参数集合里面记得参数集合的键必须为 HTTPS_KEY - * parameterMap.put(HTTPS_KEY,httpsMap); - */ - static final String HTTPS_KEY = "https_liqi"; - //--------------------------https--------------------- - //HTTPS请求map值map中需要证书标识-->值为一个证书输入流 - static final String HTTPS_CERTIFICATE = "https_certificate"; - //HTTPS请求map中无证书标识-->值没有限制,因为无用 - static final String HTTPS_CERTIFICATE_NO = "https_certificate_no"; - private RxRequestConfig mRxRequestConfig; - - //--------------------------https--------------------- - private RxRequestOperate() { - - } - - RxRequestOperate(@NonNull RxRequestConfig requestConfig) { - mRxRequestConfig = requestConfig; - } - - /** - * 开始请求RxNohttp - * - * @return - */ - public void requestRxNoHttp() { - if (mRxRequestConfig.isQueue()) { - RxRequestModel requestModel = new RxRequestModel<>(getRestRequest(), mRxRequestConfig.getOnIsRequestListener()); - requestModel.setDialogGetListener(mRxRequestConfig.getDialogGetListener()); - requestModel.setAnUnknownErrorHint(mRxRequestConfig.getAnUnknownErrorHint()); - Object sign = mRxRequestConfig.getSign(); - if (sign != null) { - requestModel.setSign(sign); - } - RxThreadInterchange.getRxThreadInterchange().start(RxMessageSource.getRxMessageSource().add(requestModel)); - } else { - RxNoHttp.getRxNoHttp().request(addParameter(getTJavaBeanRequest(mRxRequestConfig.getShiftDataClazz())), - mRxRequestConfig.getDialogGetListener(), - mRxRequestConfig.getOnIsRequestListener(), - mRxRequestConfig.getAnUnknownErrorHint()); - } - } - - /** - * 获取参数配置对象 - * - * @return 参数配置对象 - */ - public RxRequestConfig getRxRequestConfig() { - return mRxRequestConfig; - } - - /** - * 获取请求参数对象 - * - * @return 请求参数对象 - */ - public RestRequest getRestRequest() { - return addParameter(getTJavaBeanRequest(mRxRequestConfig.getShiftDataClazz())); - } - - /** - * 添加参数 - * - * @param entityRequest 请求网络参数对象 - * @return - */ - private RestRequest addParameter(RestRequest entityRequest) { - if (null != entityRequest) { - Map parameterMap = mRxRequestConfig.getParameterMap(); - //参数设置 - if (null != parameterMap && !parameterMap.isEmpty()) { - - for (Map.Entry entry : parameterMap.entrySet()) { - String keyParameter = entry.getKey(); - Object valueParameter = entry.getValue(); - //判断是否需要https协议通讯 - if (HTTPS_KEY.equals(keyParameter)) { - if (null != valueParameter) { - if (valueParameter instanceof Map) { - mapValueHttps(entityRequest, (Map) valueParameter); - } else { - Logger.e("Https协议需要请求参数定义为map集合"); - } - } - } else { - if (null != valueParameter) { - if (valueParameter instanceof Integer) { - entityRequest.add(keyParameter, Integer.parseInt(valueParameter.toString())); - } else if (valueParameter instanceof Boolean) { - entityRequest.add(keyParameter, Boolean.parseBoolean(valueParameter.toString())); - } else if (valueParameter instanceof Byte) { - entityRequest.add(keyParameter, Byte.parseByte(valueParameter.toString())); - } else if (valueParameter instanceof Double) { - entityRequest.add(keyParameter, Double.valueOf(valueParameter.toString())); - } else if (valueParameter instanceof File) { - entityRequest.add(keyParameter, (File) valueParameter); - } else if (valueParameter instanceof Float) { - entityRequest.add(keyParameter, Float.parseFloat(valueParameter.toString())); - } else if (valueParameter instanceof Binary) { - entityRequest.add(keyParameter, (Binary) valueParameter); - } else if (valueParameter instanceof Long) { - entityRequest.add(keyParameter, Long.parseLong(valueParameter.toString())); - } else if (valueParameter instanceof List) { - //确保传入的文件List集合中是指定类型 - List list = (List) valueParameter; - if (!list.isEmpty()) { - Object object = list.get(0); - if (object instanceof Binary) { - entityRequest.add(keyParameter, (List) valueParameter); - } else { - Logger.e("文件上传list参数值类型不符合.需要传入的类型:Binary类型"); - } - } else { - Logger.e("文件上传list参数值为空"); - } - } else if (valueParameter instanceof Short) { - entityRequest.add(keyParameter, Short.parseShort(valueParameter.toString())); - } else if (valueParameter instanceof Map) { - try { - Map mapString = (Map) valueParameter; - entityRequest.add(mapString); - } catch (Exception e) { - Logger.e("参数Map传入值类型错误.Map键值类型:key=String类型,value=Object类型"); - } - } else { - entityRequest.add(keyParameter, valueParameter.toString()); - } - } - } - } - } - Map mapHeader = mRxRequestConfig.getMapHeader(); - //请求头数据设置 - if (null != mapHeader && !mapHeader.isEmpty()) { - for (Map.Entry header : mapHeader.entrySet()) { - entityRequest.addHeader(header.getKey(), header.getValue()); - } - } - //设置开启缓存 - entityRequest.setCacheMode(mRxRequestConfig.getCacheMode()); - String cacheKey = mRxRequestConfig.getCacheKey(); - if (null != cacheKey && !"".equals(cacheKey)) { - //设置缓存KEY - entityRequest.setCacheKey(cacheKey); - } - //设置请求实体 - RxRequestEntityBase rxRequestEntityBase = mRxRequestConfig.getRxRequestEntityBase(); - if (null != rxRequestEntityBase) { - if (rxRequestEntityBase instanceof RxRequestJsonObjectEntity) { - String objectToJson = JsonUtil.objectToJson(rxRequestEntityBase.getStringJsonMap()); - entityRequest.setDefineRequestBodyForJson(objectToJson); - Logger.e("JsonObject类型-Body值:" + objectToJson + "\nBody-ContentType类型:" + Headers.HEAD_VALUE_CONTENT_TYPE_JSON); - } else if (rxRequestEntityBase instanceof RxRequestStringEntity) { - String stringEntity = rxRequestEntityBase.getStringEntity(); - String contentType = rxRequestEntityBase.getContentType(); - entityRequest.setDefineRequestBody(stringEntity, contentType); - Logger.e("字符串类型-Body值:" + stringEntity + "\nBody-ContentType类型:" + contentType); - } else if (rxRequestEntityBase instanceof RxRequestInputStreamEntity) { - InputStream inputStream = rxRequestEntityBase.getInputStream(); - String contentType = rxRequestEntityBase.getContentType(); - entityRequest.setDefineRequestBody(inputStream, contentType); - Logger.e("字节流类型-Body值:(字节流" + ")\nBody-ContentType类型:" + contentType); - } else if (rxRequestEntityBase instanceof RxRequestJsonListEntity) { - String objectToJson = JsonUtil.objectToJson(rxRequestEntityBase.getJsonMapList()); - entityRequest.setDefineRequestBodyForJson(objectToJson); - Logger.e("JsonArray类型-Body值:" + objectToJson + "\nBody-ContentType类型:" + Headers.HEAD_VALUE_CONTENT_TYPE_JSON); - } else { - Logger.e("RxRequestEntityBase类型未知"); - } - } - } - return entityRequest; - } - - /** - * 把https证书设置进参数对象里面 - * - * @param entityRequest 参数对象 - * @param mapHttps https参数集合 - */ - private void mapValueHttps(RestRequest entityRequest, Map mapHttps) { - for (Map.Entry entryHttps : mapHttps.entrySet()) { - String keyHttps = entryHttps.getKey(); - Object valueHttps = entryHttps.getValue(); - //需要证书 - if (HTTPS_CERTIFICATE.equals(keyHttps)) { - if (null != valueHttps) { - if (valueHttps instanceof InputStream) { - InputStream inputStream = (InputStream) valueHttps; - entityRequest.setSSLSocketFactory(SSLContextUtil.getSSLContext(inputStream).getSocketFactory()); - break; - } else { - Logger.e("Https集合需要证书值需要InputStream类型"); - } - } else { - Logger.e("Https参数集合值为空"); - } - } - //不需要证书 - if (HTTPS_CERTIFICATE_NO.equals(keyHttps)) { - entityRequest.setSSLSocketFactory(SSLContextUtil.getDefaultSLLContext().getSocketFactory()); - entityRequest.setHostnameVerifier(SSLContextUtil.HOSTNAME_VERIFIER); - break; - } - } - } - - /** - * 创建请求对象 - * - * @param clazz 请求网络返回对象 - * @return - */ - private RestRequest getTJavaBeanRequest(Class clazz) { - RestRequest ntityRequest; - if (clazz != Bitmap.class) { - ntityRequest = new RequestBeanObj<>(mRxRequestConfig.getUrl(), mRxRequestConfig.getRequestMethod(), clazz); - } else { - ntityRequest = new RequestBeanObj<>(mRxRequestConfig.getUrl(), - mRxRequestConfig.getRequestMethod(), - mRxRequestConfig.getMaxWidth(), - mRxRequestConfig.getMaxHeight(), - mRxRequestConfig.getDecodeConfig(), - mRxRequestConfig.getScaleType(), clazz); - } - int connectTimeout = mRxRequestConfig.getConnectTimeout(); - if (connectTimeout > 0) { - ntityRequest.setConnectTimeout(connectTimeout); - } - int readTimeout = mRxRequestConfig.getReadTimeout(); - if (readTimeout > 0) { - ntityRequest.setReadTimeout(readTimeout); - } - int retryCount = mRxRequestConfig.getRetryCount(); - if (retryCount > 0) { - ntityRequest.setRetryCount(retryCount); - } - return ntityRequest; - } -} diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestStringEntity.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestStringEntity.java deleted file mode 100644 index 9e298b2..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxRequestStringEntity.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.liqi.nohttputils.nohttp; - -import android.support.annotation.NonNull; - -import com.liqi.nohttputils.nohttp.interfa.OnToStringEntityMethodListener; - -/** - * 请求String实体 - * Created by LiQi on 2017/5/18. - */ -public class RxRequestStringEntity extends RxRequestEntityBase implements OnToStringEntityMethodListener { - private RxRequestStringEntity() { - - } - - RxRequestStringEntity(@NonNull String contentType) { - mContentType = contentType; - } - - @Override - public RxRequestEntityBase addStringEntityParameter(@NonNull String stringEntity) { - mStringEntity = stringEntity; - return this; - } - - -} diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxThreadInterchange.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxThreadInterchange.java deleted file mode 100644 index ffe5f17..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxThreadInterchange.java +++ /dev/null @@ -1,310 +0,0 @@ -package com.liqi.nohttputils.nohttp; - -import android.app.Dialog; -import android.content.Context; -import android.os.Handler; -import android.os.Message; -import android.support.annotation.NonNull; -import android.text.TextUtils; -import android.widget.Toast; - -import com.liqi.nohttputils.R; -import com.liqi.nohttputils.interfa.DialogGetListener; -import com.liqi.nohttputils.interfa.OnIsRequestListener; -import com.liqi.nohttputils.nohttp.rx_threadpool.interfa.OnRxMessageGetListener; -import com.liqi.nohttputils.nohttp.rx_threadpool.model.BaseRxRequestModel; -import com.liqi.nohttputils.nohttp.rx_threadpool.model.RxRequestModel; -import com.liqi.nohttputils.nohttp.rx_threadpool.thread.RxThreadDispatch; -import com.liqi.nohttputils.nohttp.rx_threadpool.utils.RxThreadPoolUtisl; -import com.yanzhenjie.nohttp.Logger; -import com.yanzhenjie.nohttp.error.NetworkError; -import com.yanzhenjie.nohttp.error.NotFoundCacheError; -import com.yanzhenjie.nohttp.error.TimeoutError; -import com.yanzhenjie.nohttp.error.URLError; -import com.yanzhenjie.nohttp.error.UnKnownHostError; - -import java.net.ConnectException; -import java.net.ProtocolException; - -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线程的中转站对象 - * Created by LiQi on 2017/3/20. - */ - -public class RxThreadInterchange implements RxThreadDispatch.OnRunDataDisListener { - //Rx线程池并发数量处理值 - private static final int RUNSIZE = RxUtilsConfig.ConfigBuilder.getConfigBuilder().getRxUtilsConfig().getRunRequestSize(); - private static RxThreadInterchange mRxThreadInterchange; - private final String REQUEST_REVOCATION = "撤销请求"; - //当队列信息长度达到此长度的时候清空掉 - private final int SIZE = RUNSIZE; - private RxThreadDispatch mRxThreadDispatch; - private OnRxMessageGetListener mOnRxMessageDisListener; - private Handler mHandler = new Handler() { - @Override - public void handleMessage(Message msg) { - RxRequestModel baseRxRequestModel = (RxRequestModel) msg.obj; - runRequest(baseRxRequestModel); - } - }; - - private RxThreadInterchange() { - - } - - static RxThreadInterchange getRxThreadInterchange() { - return mRxThreadInterchange = null == mRxThreadInterchange ? new RxThreadInterchange() : mRxThreadInterchange; - } - - /** - * 转换运行 - * - * @param onRxMessageGetListener 数据源对内暴露接口 - * @param - */ - void start(@NonNull OnRxMessageGetListener onRxMessageGetListener) { - if (null == mOnRxMessageDisListener) { - selectOkState(onRxMessageGetListener); - } else { - if (mOnRxMessageDisListener != onRxMessageGetListener) { - selectOkState(onRxMessageGetListener); - } - } - if (null == mRxThreadDispatch || !mRxThreadDispatch.isAlive()) { - - runToNo(); - //开启中转线程 - mRxThreadDispatch = new RxThreadDispatch(RUNSIZE, mOnRxMessageDisListener.getList()); - mRxThreadDispatch.setOnRunDataDisListener(this); - mRxThreadDispatch.setDaemon(true); - mRxThreadDispatch.start(); - } else { - mRxThreadDispatch.setRunSize(false); - synchronized (this) { - start(); - } - } - } - - /** - * 唤醒中转线程起来做事了 - */ - private void start() { - if (mRxThreadDispatch.isAlive() && !mRxThreadDispatch.isRunState()) { - RxThreadPoolUtisl.threadNotify(RxThreadInterchange.this); - //Logger.e("唤醒中转线程>>>>"); - } - } - - /** - * 判断当前是已经获取数据源对象 - * - * @param onRxMessageDisListener - */ - private void selectOkState(@NonNull OnRxMessageGetListener onRxMessageDisListener) { - mOnRxMessageDisListener = onRxMessageDisListener; - runToNo(); - } - - /** - * 把当前中转线程打死 - */ - private void runToNo() { - if (null != mRxThreadDispatch) { - mRxThreadDispatch.setRunTag(false); - mRxThreadDispatch = null; - } - } - - /** - * 中转线程创建rxJava线程 - * - * @param baseRxRequestModel 要处理的对象 - * @param - */ - private void runRequest(final RxRequestModel baseRxRequestModel) { - if (null != baseRxRequestModel) { - - Dialog dialog = getDialog(baseRxRequestModel); - if (null != dialog && !dialog.isShowing()) { - try { - dialog.show(); - } catch (Exception e) { - Logger.e("Dialog-显示异常:由于Dialog依赖的Context不是栈顶。"); - } - } - - Observable.create(baseRxRequestModel) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(new Observer() { - private Disposable mDisposable; - - @Override - public void onSubscribe(@NonNull Disposable d) { - mDisposable = d; - } - - @Override - 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(@NonNull Throwable e) { - mDisposable.dispose(); - - Dialog dialog = getDialog(baseRxRequestModel); - if (null != dialog && dialog.isShowing()) { - try { - dialog.dismiss(); - } catch (Exception e1) { - Logger.e("Dialog-关闭异常:由于Dialog已经关闭或者依赖的Context不存在"); - } - } - - // 提示异常信息。 - if (e instanceof NetworkError) {// 网络不好 - show(dialog, R.string.error_please_check_network); - } else if (e instanceof TimeoutError) {// 请求超时 - show(dialog, R.string.error_timeout); - } else if (e instanceof UnKnownHostError) {// 找不到服务器 - show(dialog, R.string.error_not_found_server); - } else if (e instanceof URLError) {// URL是错的 - show(dialog, R.string.error_url_error); - } else if (e instanceof NotFoundCacheError) { - // 这个异常只会在仅仅查找缓存时没有找到缓存时返回 - show(dialog, R.string.error_not_found_cache); - } else if (e instanceof ProtocolException) { - show(dialog, R.string.error_system_unsupport_method); - } else if (e instanceof ConnectException) {//没有连接到服务器 - show(dialog, R.string.error_no_service); - } else { - if (e.getMessage().contains(REQUEST_REVOCATION)) { - Logger.e(e.getMessage()); - } else { - Logger.e("NoHttpUtils捕获异常:" + e.toString()); - StackTraceElement[] stackTrace = e.getStackTrace(); - if (null != stackTrace) { - for (StackTraceElement traceElement : stackTrace) { - Logger.e("NoHttpUtils捕获异常:" + traceElement.toString()); - } - } - String anUnknownErrorHint = baseRxRequestModel.getAnUnknownErrorHint(); - if (TextUtils.isEmpty(anUnknownErrorHint)) { - show(dialog, R.string.error_unknow); - } else { - show(dialog, anUnknownErrorHint); - } - } - } - - OnIsRequestListener onIsRequestListener = baseRxRequestModel.getOnIsRequestListener(); - if (null != onIsRequestListener) { - onIsRequestListener.onError(e); - } - - baseRxRequestModel.clearAll(); - messageListDalete(); - } - - @Override - public void onComplete() { - - } - }); - } - - } - - /** - * 当数据源里面的数据长度达到一个指定的值的时候,干掉指定值长度的数据。然后继续浪(运行) - */ - private void messageListDalete() { - synchronized (this) { - int size = mOnRxMessageDisListener.size(); - if (size >= SIZE) { - for (int i = 0; i < size; i++) { - int index = i % size; - BaseRxRequestModel baseRxRequestModel = mOnRxMessageDisListener.get(index); - if (null != baseRxRequestModel && baseRxRequestModel.isRunDispose()) { - mOnRxMessageDisListener.delete(index); - } - } - } - mRxThreadDispatch.addRunSize(); - start(); - } - } - - @Override - public void getRunData(BaseRxRequestModel runData) { - Message message = mHandler.obtainMessage(); - message.obj = runData; - mHandler.sendMessage(message); - } - - @Override - public void waitThread() { - RxThreadPoolUtisl.threadWait(this); - } - - /** - * 土司提示 - * - * @param stringId 提示内容资源ID - */ - private void show(Dialog dialog, int stringId) { - if (null != dialog) { - Context context = dialog.getContext(); - Toast.makeText(context, context.getResources().getString(stringId), Toast.LENGTH_SHORT).show(); - } - } - - /** - * 土司提示 - * - * @param hint 提示内容 - */ - private void show(Dialog dialog, String hint) { - if (null != dialog) { - Context context = dialog.getContext(); - Toast.makeText(context, hint, Toast.LENGTH_SHORT).show(); - } - } - - /** - * 从RxRequestModel对象中获取dialog - * - * @param baseRxRequestModel RxRequestModel对象 - * @return - */ - private Dialog getDialog(RxRequestModel baseRxRequestModel) { - DialogGetListener dialogGetListener = baseRxRequestModel.getDialogGetListener(); - return null == dialogGetListener ? null : dialogGetListener.getDialog(); - } -} diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxUtilsConfig.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxUtilsConfig.java deleted file mode 100644 index faacd94..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/RxUtilsConfig.java +++ /dev/null @@ -1,414 +0,0 @@ -package com.liqi.nohttputils.nohttp; - -import android.content.Context; - -import com.liqi.nohttputils.R; -import com.liqi.nohttputils.interfa.DialogGetListener; -import com.yanzhenjie.nohttp.InitializationConfig; -import com.yanzhenjie.nohttp.cookie.DBCookieStore; - -import java.io.InputStream; - -import javax.net.ssl.HostnameVerifier; - -/** - * 初始化参数配置文件 - * Created by LiQi on 2017/2/22. - */ - -public class RxUtilsConfig { - /** - * nohttp底层请求方式 - */ - private int mRxRequestUtilsWhy; - /** - * 设置全局连接超时时间,单位秒,默认30s。 - */ - private int mConnectTimeout = 30; - /** - * 设置全局服务器响应超时时间,单位秒,默认30s。 - */ - private int mReadTimeout = mConnectTimeout; - /** - * 配置缓存,是否缓存进数据库DBCacheStore - */ - private boolean mDbEnable; - /** - * 配置Cookie,默认保存数据库DBCookieStore - * 是否维护Cookie - */ - private boolean mCookieEnable; - /** - * 是否debug打印 - */ - private boolean isDebug; - /** - * debug打印的名字 - */ - private String mDebugName = "NoHttpRxUtils"; - /** - * 上下文 - */ - private Context mContext; - /** - * 网络请求全局加载框获取接口 - * (全项目请求默认加载框) - */ - private DialogGetListener mDialogGetListener; - /** - * 下载线程池并发数量 - */ - private int mThreadPoolSize = 3; - /** - * 网络请求队列并发数量 - */ - private int mRunRequestSize = 3; - /** - * Cookie管理监听。 - */ - private DBCookieStore.CookieStoreListener mCookieStoreListener; - /** - * 是否使用安全协议通讯 - */ - private boolean isSSL; - /** - * 安全协议证书文件流 - */ - private InputStream mInputStreamSSL; - /** - * 主机验证 - */ - private HostnameVerifier mHostnameVerifier; - /** - * 全局重试次数,配置后每个请求失败都会重试设置的次数。 - */ - private int mRetry; - /** - * NoHttp初始化配置建筑类 - */ - private InitializationConfig.Builder mBuilder; - /** - * 未知错误提示语 - */ - private String mAnUnknownErrorHint; - - private RxUtilsConfig(Context context) { - mContext = context; - mBuilder = InitializationConfig.newBuilder(context); - mAnUnknownErrorHint = mContext.getResources().getString(R.string.error_unknow); - } - - String getAnUnknownErrorHint() { - return mAnUnknownErrorHint; - } - - DialogGetListener getDialogGetListener() { - return mDialogGetListener; - } - - boolean isDebug() { - return isDebug; - } - - String getDebugName() { - return mDebugName; - } - - Context getContext() { - return mContext; - } - - int getRxRequestUtilsWhy() { - return mRxRequestUtilsWhy; - } - - int getConnectTimeout() { - return mConnectTimeout * 1000; - } - - int getReadTimeout() { - return mReadTimeout * 1000; - } - - boolean isDbEnable() { - return mDbEnable; - } - - boolean isCookieEnable() { - return mCookieEnable; - } - - public int getThreadPoolSize() { - return mThreadPoolSize; - } - - int getRunRequestSize() { - return mRunRequestSize; - } - - boolean isSSL() { - return isSSL; - } - - InputStream getInputStreamSSL() { - return mInputStreamSSL; - } - - HostnameVerifier getHostnameVerifier() { - return mHostnameVerifier; - } - - InitializationConfig.Builder getBuilder() { - return mBuilder; - } - - DBCookieStore.CookieStoreListener getCookieStoreListener() { - return mCookieStoreListener; - } - - int getRetry() { - return mRetry; - } - - /** - * RxUtilsConfig建筑创建对象 - */ - public static class ConfigBuilder { - private static ConfigBuilder mConfigBuilder; - private RxUtilsConfig mRxUtilsConfig; - - ConfigBuilder(Context context) { - mRxUtilsConfig = new RxUtilsConfig(context); - } - - public static ConfigBuilder getConfigBuilder(Context context) { - return mConfigBuilder = null == mConfigBuilder ? new ConfigBuilder(context) : mConfigBuilder; - } - - /** - * 获取RxUtilsConfig建筑创建对象 - * - * @return - */ - public static ConfigBuilder getConfigBuilder() { - return mConfigBuilder; - } - - /** - * 获取RxUtilsConfig配置类 - * - * @return - */ - public RxUtilsConfig getRxUtilsConfig() { - return mRxUtilsConfig; - } - - /** - * 设置底层请求方式 - * - * @param rxUtilsConfig NoHttpInit.URLCONNECTION=>URLCONNECTION请求, NoHttpInit.OKHTTP=>OKHTTP请求 - * @return - */ - public ConfigBuilder setRxRequestUtilsWhy(int rxUtilsConfig) { - mRxUtilsConfig.mRxRequestUtilsWhy = rxUtilsConfig; - return this; - } - - /** - * 设置全局连接超时时间,单位秒,默认30s。 - * - * @param connectTimeout 全局连接超时时间,单位秒,默认30s。 - * @return - */ - public ConfigBuilder setConnectTimeout(int connectTimeout) { - mRxUtilsConfig.mConnectTimeout = connectTimeout; - return this; - } - - /** - * 设置全局服务器响应超时时间,单位秒,默认30s。 - * - * @param readTimeout 全局服务器响应超时时间,单位秒,默认30s。 - * @return - */ - public ConfigBuilder setReadTimeout(int readTimeout) { - mRxUtilsConfig.mReadTimeout = readTimeout; - return this; - } - - /** - * 配置缓存,设置是否缓存进数据库DBCacheStore - * - * @param dbEnable 设置是否缓存进数据库DBCacheStore - * @return - */ - public ConfigBuilder setDbEnable(boolean dbEnable) { - mRxUtilsConfig.mDbEnable = dbEnable; - return this; - } - - /** - * 设置是否自动维护Cookie - * - * @param cookieEnable 是否自动维护Cookie. yes true,else false - * @return - */ - public ConfigBuilder setCookieEnable(boolean cookieEnable) { - mRxUtilsConfig.mCookieEnable = cookieEnable; - return this; - } - - /** - * 设置是否debug打印 - * - * @param isDebug 是否debug打印。yes true,else false - * @return - */ - public ConfigBuilder isDebug(boolean isDebug) { - mRxUtilsConfig.isDebug = isDebug; - return this; - } - - /** - * 设置debug打印的名字 - * - * @param debugName debug打印的名字 - * @return - */ - public ConfigBuilder setDebugName(String debugName) { - mRxUtilsConfig.mDebugName = debugName; - return this; - } - - /** - * 设置全局网络请求队列并发数量 - * - * @param runRequestSize 全局网络请求队列并发数量 - * @return - */ - public ConfigBuilder setRunRequestSize(int runRequestSize) { - mRxUtilsConfig.mRunRequestSize = runRequestSize; - return this; - } - - /** - * 设置下载线程池并发数量 - * - * @param threadPoolSize 下载线程池并发数量 - * @return - */ - public ConfigBuilder setThreadPoolSize(int threadPoolSize) { - mRxUtilsConfig.mThreadPoolSize = threadPoolSize; - return this; - } - - /** - * 网络请求全局加载框获取接口 - * - * @param dialogGetListener - * @return - */ - public ConfigBuilder setDialogGetListener(DialogGetListener dialogGetListener) { - mRxUtilsConfig.mDialogGetListener = dialogGetListener; - return this; - } - - /** - * 添加全局请求头 - * - * @param key 请求头键 - * @param value 请求头值 - * @return - */ - public ConfigBuilder addHeader(String key, String value) { - mRxUtilsConfig.mBuilder.addHeader(key, value); - return this; - } - - /** - * 添加全局请求参数-只支持String类型 - * - * @param key 请求参数键 - * @param value 请求参数值 - * @return - */ - public ConfigBuilder addParam(String key, String value) { - mRxUtilsConfig.mBuilder.addParam(key, value); - return this; - } - - /** - * 设置Cookie管理监听。 - * - * @param cookieStoreListener Cookie管理监听 - * @return - */ - public ConfigBuilder setCookieStoreListener(DBCookieStore.CookieStoreListener cookieStoreListener) { - mRxUtilsConfig.mCookieStoreListener = cookieStoreListener; - return this; - } - - /** - * 设置有安全协议证书文件流安全通讯 - * - * @param inputStreamSSL 安全协议证书文件流 - * @return - */ - public ConfigBuilder setInputStreamSSL(InputStream inputStreamSSL) { - mRxUtilsConfig.isSSL = true; - mRxUtilsConfig.mInputStreamSSL = inputStreamSSL; - return this; - } - - /** - * 设置无安全协议证书文件流安全通讯 - * - * @return - */ - public ConfigBuilder setInputStreamSSL() { - mRxUtilsConfig.isSSL = true; - return this; - } - - /** - * 设置主机验证 - * - * @param hostnameVerifier 主机验证 - * @return - */ - public ConfigBuilder setHostnameVerifier(HostnameVerifier hostnameVerifier) { - mRxUtilsConfig.isSSL = true; - mRxUtilsConfig.mHostnameVerifier = hostnameVerifier; - return this; - } - - /** - * 设置全局重试次数,配置后每个请求失败都会重试设置的次数。 - * - * @param retry 全局重试次数 - * @return - */ - public ConfigBuilder setRetry(int retry) { - mRxUtilsConfig.mRetry = retry; - return this; - } - - /** - * 设置未知错误提示语 - * - * @param anUnknownErrorHint 未知错误提示语 - * @return - */ - public ConfigBuilder setAnUnknownErrorHint(String anUnknownErrorHint) { - mRxUtilsConfig.mAnUnknownErrorHint = anUnknownErrorHint; - return this; - } - - /** - * 开始初始化 - */ - public void startInit() { - NoHttpInit.getNoHttpInit().init(mRxUtilsConfig); - } - } -} diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/SSLContextUtil.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/SSLContextUtil.java deleted file mode 100644 index d1e7e72..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/SSLContextUtil.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright 2015 Yan Zhenjie - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.liqi.nohttputils.nohttp; - -import android.annotation.SuppressLint; - -import java.io.InputStream; -import java.security.KeyStore; -import java.security.SecureRandom; -import java.security.cert.Certificate; -import java.security.cert.CertificateException; -import java.security.cert.CertificateFactory; -import java.security.cert.X509Certificate; - -import javax.net.ssl.HostnameVerifier; -import javax.net.ssl.KeyManagerFactory; -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLSession; -import javax.net.ssl.TrustManager; -import javax.net.ssl.TrustManagerFactory; -import javax.net.ssl.X509TrustManager; - -/** - * Https协议证书工具类 - * - * @author LiQi - */ -public class SSLContextUtil { - - static final HostnameVerifier HOSTNAME_VERIFIER = new HostnameVerifier() { - public boolean verify(String hostname, SSLSession session) { - return true; - } - }; - private static TrustManager trustManagers = new X509TrustManager() { - - @Override - public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { - } - - @Override - public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { - - } - - @Override - public X509Certificate[] getAcceptedIssuers() { - return new X509Certificate[0]; - } - }; - - /** - * 拿到https证书, SSLContext (NoHttp已经修补了系统的SecureRandom的bug)。 - */ - @SuppressLint("TrulyRandom") - static SSLContext getSSLContext(InputStream inputStream) { - SSLContext sslContext = null; - try { - sslContext = SSLContext.getInstance("TLS"); - //InputStream inputStream = Application.getInstance().getAssets().open("srca.cer"); - CertificateFactory cerFactory = CertificateFactory.getInstance("X.509"); - Certificate cer = cerFactory.generateCertificate(inputStream); - - KeyStore keyStore = KeyStore.getInstance("PKCS12"); - keyStore.load(null, null); - keyStore.setCertificateEntry("trust", cer); - - KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); - keyManagerFactory.init(keyStore, null); - - TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); - trustManagerFactory.init(keyStore); - - sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), new SecureRandom()); - } catch (Exception e) { - e.printStackTrace(); - } - - return sslContext; - } - - /** - * 如果不需要https证书.(NoHttp已经修补了系统的SecureRandom的bug)。 - */ - static SSLContext getDefaultSLLContext() { - SSLContext sslContext = null; - try { - sslContext = SSLContext.getInstance("TLS"); - sslContext.init(null, new TrustManager[]{trustManagers}, new SecureRandom()); - } catch (Exception e) { - e.printStackTrace(); - } - return sslContext; - } - -} diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/gsonutils/JsonUtil.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/gsonutils/JsonUtil.java deleted file mode 100644 index 0591c7f..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/gsonutils/JsonUtil.java +++ /dev/null @@ -1,201 +0,0 @@ -package com.liqi.nohttputils.nohttp.gsonutils; - -import android.util.Log; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; -import com.google.gson.JsonParseException; -import com.google.gson.JsonPrimitive; -import com.google.gson.JsonSerializationContext; -import com.google.gson.JsonSerializer; -import com.google.gson.JsonSyntaxException; - -import java.lang.reflect.Type; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.List; -import java.util.Map; - -/** - * Gson 简单操作的工具类 - * - * @author Liqi - * - */ -public class JsonUtil { - - private static final String TAG = "JsonUtil"; - private static Gson gson = null; - - static { - if (gson == null) { - gson = new Gson(); - } - } - - private JsonUtil() { - } - - /** - * 将对象转换成json格式 - * - * @param object - * @return - */ - public static String objectToJson(Object object) { - String jsonStr = null; - try { - if (gson != null) { - jsonStr = gson.toJson(object); - } - } catch (Exception e) { - Log.e(TAG, "object to json string error >>" + e.getMessage()); - } - return jsonStr; - } - - /** - * 将对象转换成json格式(并自定义日期格式) - * - * @param ts - * @return - */ - @Deprecated - public static String objectToJsonDateSerializer(Object ts, - final String dateformat) { - String jsonStr = null; - Gson gson = new GsonBuilder().registerTypeHierarchyAdapter(Date.class, // - new JsonSerializer() {// - public JsonElement serialize(Date src, Type typeOfSrc, - JsonSerializationContext context) { - SimpleDateFormat format = new SimpleDateFormat( - dateformat); - return new JsonPrimitive(format.format(src)); - } - - }).setDateFormat(dateformat).create(); - if (gson != null) { - jsonStr = gson.toJson(ts); - } - return jsonStr; - } - - /** - * 将json格式转换成list对象 - * - * @param jsonStr - * @return - */ - public static List jsonToList(String jsonStr) throws JsonSyntaxException { - List list = null; - try { - if (gson != null) { - Type type = new com.google.gson.reflect.TypeToken>() { - }.getType(); - list = gson.fromJson(jsonStr, type); - } - } catch (Exception e) { - Log.e(TAG, "json string to list error >>" + e.getMessage()); - } - return list; - } - - /** - * 将json格式转换成map对象 - * - * @param jsonStr - * @return - */ - public static Map jsonToMap(String jsonStr) { - Map map = null; - try { - if (gson != null) { - Type type = new com.google.gson.reflect.TypeToken>() { - }.getType(); - map = gson.fromJson(jsonStr, type); - } - return map; - } catch (Exception e) { - Log.e(TAG, "json string to map error >>" + e.getMessage()); - } - return map; - } - - /** - * 将json转换成bean对象 - * - * @param jsonStr - * @return - */ - public static T jsonToBean(String jsonStr, Class cl) { - T t = null; - try { - if (gson != null) { - t = gson.fromJson(jsonStr, cl); - } - return t; - } catch (Exception e) { - Log.e(TAG, "json string to bean object error >>" + jsonStr); - } - return t; - } - - /** - * 将json转换成bean对象 - * - * @param jsonStr - * @param cl - * @return - */ - @SuppressWarnings("unchecked") - public static T jsonToBeanDateSerializer(String jsonStr, Class cl, - final String pattern) { - Object obj = null; - Gson gson = new GsonBuilder().registerTypeAdapter(// - Date.class, // - new JsonDeserializer() {// - public Date deserialize(JsonElement json, Type typeOfT, - JsonDeserializationContext context) - throws JsonParseException { - SimpleDateFormat format = new SimpleDateFormat(pattern); - String dateStr = json.getAsString(); - try { - return format.parse(dateStr); - } catch (ParseException e) { - e.printStackTrace(); - } - return null; - } - }).setDateFormat(pattern).create(); - if (gson != null) { - obj = gson.fromJson(jsonStr, cl); - } - return (T) obj; - } - - /** - * 根据 - * - * @param jsonStr - * @param key - * @return - */ - public static Object getJsonValue(String jsonStr, String key) { - Object obj = null; - - try { - Map map = jsonToMap(jsonStr); - if (map != null && map.size() > 0) { - obj = map.get(key); - System.out.println(">>>>Map对象装换>>" + obj); - } - } catch (Exception e) { - Log.e(TAG, "get value from map error >>" + e.getMessage()); - } - return obj; - } -} diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToInputStreamEntityMethodListener.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToInputStreamEntityMethodListener.java deleted file mode 100644 index d6096a8..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToInputStreamEntityMethodListener.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.liqi.nohttputils.nohttp.interfa; - -import android.support.annotation.NonNull; - -import com.liqi.nohttputils.nohttp.RxRequestEntityBase; - -import java.io.InputStream; - -/** - * 请求InputStream实体暴露给外界调用方法 - * Created by LiQi on 2017/5/19. - */ - -public interface OnToInputStreamEntityMethodListener { - /** - * 添加输入流进实体中 - * @param inputStream 输入流 - * @return - */ - RxRequestEntityBase addEntityInputStreamParameter(@NonNull InputStream inputStream); -} diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToJsonListEntityMethodListener.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToJsonListEntityMethodListener.java deleted file mode 100644 index df673e1..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToJsonListEntityMethodListener.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.liqi.nohttputils.nohttp.interfa; - -import android.support.annotation.NonNull; - -import com.liqi.nohttputils.nohttp.RxRequestConfig; - - -/** - * 请求Json集合实体暴露给外界调用方法 - * Created by LiQi on 2017/5/19. - */ - -public interface OnToJsonListEntityMethodListener { - /** - * 添加对象参数 - * - * @param keyParameter 对象里面的key - * @param valueParameter 对象里面的value - * @return - */ - OnToJsonAddEntityMethodListener addObjectEntityParameter(String keyParameter, @NonNull Object valueParameter); - - /** - * 切换对象到配置对象 - * - * @return - */ - RxRequestConfig.ConfigBuilder transitionToRequest(); - - interface OnToJsonAddEntityMethodListener { - /** - * 往参数对象添加参数 - * - * @param keyParameter 参数key - * @param valueParameter 参数value - * @return - */ - OnToJsonAddEntityMethodListener addEntityParameter(String keyParameter, @NonNull Object valueParameter); - - /** - * 把参数刷进list里面 - * - * @return - */ - OnToJsonListEntityMethodListener objectBrushIntoList(); - } -} - diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToJsonObjectEntityMethodListener.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToJsonObjectEntityMethodListener.java deleted file mode 100644 index b101cad..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToJsonObjectEntityMethodListener.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.liqi.nohttputils.nohttp.interfa; - -import android.support.annotation.NonNull; - -import com.liqi.nohttputils.nohttp.RxRequestJsonObjectEntity; - - -/** - * 请求Json实体暴露给外界调用方法 - * Created by LiQi on 2017/5/19. - */ - -public interface OnToJsonObjectEntityMethodListener { - /** - * 添加参数 - * @param entityKey key - * @param entityValu Valu - * @return - */ - RxRequestJsonObjectEntity addEntityParameter(String entityKey, @NonNull Object entityValu); -} diff --git a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToStringEntityMethodListener.java b/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToStringEntityMethodListener.java deleted file mode 100644 index 2ddd06c..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/interfa/OnToStringEntityMethodListener.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.liqi.nohttputils.nohttp.interfa; - -import android.support.annotation.NonNull; - -import com.liqi.nohttputils.nohttp.RxRequestEntityBase; - -/** - * 请求String实体暴露给外界调用方法 - * Created by LiQi on 2017/5/19. - */ - -public interface OnToStringEntityMethodListener { - /** - * 添加String进请求实体 - * - * @param stringEntity String类型值 - * @return - */ - RxRequestEntityBase addStringEntityParameter(@NonNull String stringEntity); -} diff --git a/nohttputils-r2/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 deleted file mode 100644 index 8f7585b..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/RxPollUtils.java +++ /dev/null @@ -1,98 +0,0 @@ -package com.liqi.nohttputils.nohttp.rx_poll; - -import android.support.annotation.NonNull; - -import com.liqi.nohttputils.interfa.OnRequestRxNoHttpListener; -import com.liqi.nohttputils.nohttp.RxPollNoHttpConfig; -import com.liqi.nohttputils.nohttp.RxRequestConfig; -import com.liqi.nohttputils.nohttp.RxRequestOperate; -import com.liqi.nohttputils.nohttp.rx_poll.extend.ObservableExpand; -import com.liqi.nohttputils.nohttp.rx_poll.model.RxInformationModel; -import com.liqi.nohttputils.nohttp.rx_poll.model.RxInformationPoolModel; -import com.liqi.nohttputils.nohttp.rx_poll.operators.OnObserverEventListener; -import com.liqi.nohttputils.nohttp.rx_poll.pond.RxInformationPool; -import com.yanzhenjie.nohttp.rest.RestRequest; - -import java.util.concurrent.TimeUnit; - -import io.reactivex.android.schedulers.AndroidSchedulers; -import io.reactivex.functions.Consumer; -import io.reactivex.functions.Predicate; -import io.reactivex.schedulers.Schedulers; - - -/** - * RxJava轮询控制类 - * Created by LiQi on 2017/9/12. - */ - -public class RxPollUtils implements OnRequestRxNoHttpListener { - - private RxPollNoHttpConfig mRxPollNoHttpConfig; - - private RxPollUtils() { - - } - - private RxPollUtils(@NonNull RxPollNoHttpConfig rxPollNoHttpConfig) { - mRxPollNoHttpConfig = rxPollNoHttpConfig; - } - - /** - * 获取NoHttp轮询操作接口 - * - * @param rxPollNoHttpConfig NoHttp轮询配置类 - * @param - * @return NoHttp轮询操作接口 - */ - public static OnRequestRxNoHttpListener getRxPollUtilsNoHttpPoll(@NonNull RxPollNoHttpConfig rxPollNoHttpConfig) { - return new RxPollUtils<>(rxPollNoHttpConfig); - } - - @Override - public void requestRxNoHttp() { - RxRequestOperate rxRequestOperate = mRxPollNoHttpConfig.getRxRequestOperate(); - - RxRequestConfig rxRequestConfig = rxRequestOperate.getRxRequestConfig(); - - RxInformationPoolModel informationPoolModel = new RxInformationPoolModel<>( - rxRequestConfig.getOnIsRequestListener(), - rxRequestConfig.getDialogGetListener(), - rxRequestConfig.getAnUnknownErrorHint()); - Object sign = rxRequestConfig.getSign(); - if (null != sign) { - informationPoolModel.setSign(sign); - } - //赋值拦截器 - Predicate> booleanFunc1 = mRxPollNoHttpConfig.getBooleanFunc1(); - if (null != booleanFunc1) { - informationPoolModel.setBooleanFunc1(booleanFunc1); - } - - //设置观察者相应处理事件 - Consumer> rxInformationModelAction1 = mRxPollNoHttpConfig.getRxInformationModelAction1(); - if (null != rxInformationModelAction1) { - informationPoolModel.setRxInformationModelAction1(rxInformationModelAction1); - } - - //赋值被观察者处理事件 - OnObserverEventListener, RxInformationModel> onObserverEventListener = mRxPollNoHttpConfig.getOnObserverEventListener(); - if (null != onObserverEventListener) { - informationPoolModel.setOnObserverEventListener(onObserverEventListener); - } - //如果没有实现被观察者处理事件就内部管理 - else{ - RxInformationPool.getRxInformationPoolList().add(informationPoolModel); - } - - //开启rxJava扩展轮询 - ObservableExpand.intervalPolling(mRxPollNoHttpConfig.getInitialDelay(), - mRxPollNoHttpConfig.getPeriod(), - TimeUnit.MILLISECONDS, - informationPoolModel.getOnObserverEventListener()) - .subscribeOn(Schedulers.io(), rxRequestOperate.getRestRequest()) - .takeUntil(informationPoolModel.getBooleanFunc1()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(informationPoolModel.getRxInformationModelAction1()); - } -} diff --git a/nohttputils-r2/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 deleted file mode 100644 index ad38223..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/extend/ObservableExpand.java +++ /dev/null @@ -1,84 +0,0 @@ -package com.liqi.nohttputils.nohttp.rx_poll.extend; - - -import com.liqi.nohttputils.nohttp.rx_poll.operators.OnObserverEventListener; -import com.liqi.nohttputils.nohttp.rx_poll.operators.OnSubscribeTimerPeriodically; - -import java.util.concurrent.TimeUnit; - -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 { - - - /** - * 轮询间隔方法 - * - * @param initialDelay 初始化加载延迟 - * @param period 轮询间隔时间 - * @param unit 时间单位 - * @param observerEventListener 可观察者事件监听器 - * @param - * @return 订阅间隔计时Builder - */ - public static Builder intervalPolling(long initialDelay, long period, TimeUnit unit, OnObserverEventListener observerEventListener) { - - return new Builder<>(initialDelay, period, unit, observerEventListener); - } - - /** - * 订阅间隔计时Builder - * - * @param - */ - public static class Builder { - /** - * 初始化加载延迟 - */ - private long initialDelay; - /** - * 轮询间隔时间 - */ - private long period; - /** - * 时间单位 - */ - private TimeUnit unit; - /** - * 可观察者事件监听器 - */ - private OnObserverEventListener observerEventListener; - - public Builder(long initialDelay, long period, TimeUnit unit, OnObserverEventListener observerEventListener) { - this.initialDelay = initialDelay; - this.period = period; - this.unit = unit; - this.observerEventListener = observerEventListener; - } - - /** - * 设置可观察者监听器线程线路 - * - * @param eventScheduler 线程线路 - * @param transferValue 待处理或者待传输的对象 - * @return - */ - public Observable subscribeOn(Scheduler eventScheduler, V transferValue) { - - 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 RxJavaPlugins.onAssembly(timerPeriodically); - } - } -} \ No newline at end of file diff --git a/nohttputils-r2/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 deleted file mode 100644 index bec199f..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/interfa/OnRxInformationCancelListener.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.liqi.nohttputils.nohttp.rx_poll.interfa; - -/** - * RxJava轮询数据共享对象中取消轮询接口 - * Created by LiQi on 2017/9/11. - */ - -public interface OnRxInformationCancelListener { - /** - * 取消指定标识轮询线程 - * - * @param sign 标识 - */ - void cancel(Object sign); - - /** - * 批量取消指定标识轮询线程 - * - * @param sign 标识 - */ - void cancel(Object[] sign); - - /** - * 取消全部轮询线程 - */ - void cancelAll(); -} diff --git a/nohttputils-r2/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 deleted file mode 100644 index c27d9a5..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/interfa/OnRxInformationListListener.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.liqi.nohttputils.nohttp.rx_poll.interfa; - - -import com.liqi.nohttputils.nohttp.rx_poll.model.RxInformationPoolModel; - -/** - * RxJava轮询数据共享对象中list操作接口 - * Created by LiQi on 2017/9/11. - */ - -public interface OnRxInformationListListener { - /** - * 数据添加 - * - * @param informationPoolModel rxJava轮询操作数据源对象 - * @param - */ - void add(RxInformationPoolModel informationPoolModel); - - /** - * 根据索引获取listview中指定对象 - * - * @param index 索引 - * @return rxJava轮询操作数据源对象 - */ - RxInformationPoolModel get(int index); - - /** - * 获取list长度 - * - * @return - */ - int size(); - - /** - * 根据索引删除list指定数据 - * - * @param index 索引 - */ - void delete(int index); -} diff --git a/nohttputils-r2/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 deleted file mode 100644 index 1a45af5..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/interfa/OnRxPollConfigBuilderListener.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.liqi.nohttputils.nohttp.rx_poll.interfa; - -import com.liqi.nohttputils.nohttp.RxPollNoHttpConfig; -import com.liqi.nohttputils.nohttp.rx_poll.model.RxInformationModel; - -import io.reactivex.functions.Predicate; - -/** - * RX轮询构建轮询配置类业务操作方法接口 - * Created by LiQi on 2017/9/13. - */ - -public interface OnRxPollConfigBuilderListener { - /** - * 设置设置数据拦截监听对象 - * - * @param booleanFunc1 设置数据拦截监听对象 - * @return 构建轮询配置类 - */ - RxPollNoHttpConfig.ConfigBuilder setBooleanFunc1(Predicate> booleanFunc1); -} diff --git a/nohttputils-r2/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 deleted file mode 100644 index a60fe02..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/model/RxInformationModel.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.liqi.nohttputils.nohttp.rx_poll.model; - -/** - * rxJava轮询转换对象 - * Created by LiQi on 2017/9/8. - */ - -public class RxInformationModel { - //装换对象 - private T mData; - - //是否停止当前对象轮询 - private boolean isStop; - - //是否是异常 - private boolean isException; - //异常信息 - private Throwable mThrowable; - - public RxInformationModel() { - } - - public boolean isException() { - return isException; - } - - public void setException(boolean exception) { - isException = exception; - } - - public Throwable getThrowable() { - return mThrowable; - } - - public void setThrowable(Throwable throwable) { - mThrowable = throwable; - } - - public T getData() { - return mData; - } - - public void setData(T data) { - mData = data; - } - - public boolean isStop() { - return isStop; - } - - public void setStop(boolean stop) { - isStop = stop; - } - - @Override - public String toString() { - return "RxInformationModel{" + - "mData=" + mData + - ", isStop=" + isStop + - ", isException=" + isException + - ", mThrowable=" + mThrowable + - '}'; - } -} diff --git a/nohttputils-r2/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 deleted file mode 100644 index bd83816..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/model/RxInformationPoolModel.java +++ /dev/null @@ -1,318 +0,0 @@ -package com.liqi.nohttputils.nohttp.rx_poll.model; - - -import android.app.Dialog; -import android.content.Context; -import android.support.annotation.NonNull; -import android.text.TextUtils; -import android.widget.Toast; - -import com.liqi.nohttputils.R; -import com.liqi.nohttputils.interfa.DialogGetListener; -import com.liqi.nohttputils.interfa.OnIsRequestListener; -import com.liqi.nohttputils.nohttp.rx_poll.operators.OnObserverEventListener; -import com.yanzhenjie.nohttp.Logger; -import com.yanzhenjie.nohttp.NoHttp; -import com.yanzhenjie.nohttp.error.NetworkError; -import com.yanzhenjie.nohttp.error.NotFoundCacheError; -import com.yanzhenjie.nohttp.error.TimeoutError; -import com.yanzhenjie.nohttp.error.URLError; -import com.yanzhenjie.nohttp.error.UnKnownHostError; -import com.yanzhenjie.nohttp.rest.Response; -import com.yanzhenjie.nohttp.rest.RestRequest; - -import java.net.ConnectException; -import java.net.ProtocolException; - -import io.reactivex.android.schedulers.AndroidSchedulers; -import io.reactivex.functions.Consumer; -import io.reactivex.functions.Predicate; - -/** - * rxJava轮询操作数据源对象 - * Created by LiQi on 2017/9/8. - */ - -public class RxInformationPoolModel { - private Object mSign; - private DialogGetListener mDialogGetListener; - private RestRequest mRestRequest; - private OnIsRequestListener mOnIsRequestListener; - private RxInformationModel mRxInformationModel; - - - private Predicate> mBooleanFunc1; - private OnObserverEventListener, RxInformationModel> mOnObserverEventListener; - private Consumer> mRxInformationModelAction1; - - public RxInformationPoolModel(@NonNull OnIsRequestListener onIsRequestListener, DialogGetListener dialogGetListener, String anUnknownErrorHint) { - mOnIsRequestListener = onIsRequestListener; - mDialogGetListener = dialogGetListener; - mRxInformationModel = new RxInformationModel<>(); - initOnObserverEventListener(); - initBooleanFunc1(); - initRxInformationModelAction1(anUnknownErrorHint); - - - } - - /** - * 内部实现可观察者事件产生对应行动监听器 - */ - private void initRxInformationModelAction1(final String anUnknownErrorHint) { - - mRxInformationModelAction1 = new Consumer>() { - @Override - public void accept(RxInformationModel tRxInformationModel) throws Exception { - Logger.e(mRestRequest.url() + ":轮询运行完毕"); - - Dialog dialog = null == mDialogGetListener ? null : mDialogGetListener.getDialog(); - if (null != dialog && dialog.isShowing()) { - dialog.dismiss(); - } - - if (!tRxInformationModel.isStop()) { - //正常 - if (!tRxInformationModel.isException()) { - if (null != mOnIsRequestListener) { - mOnIsRequestListener.onNext(tRxInformationModel.getData()); - } - } - //异常 - else { - Throwable e = tRxInformationModel.getThrowable(); - // 提示异常信息。 - if (e instanceof NetworkError) {// 网络不好 - show(dialog, R.string.error_please_check_network); - } else if (e instanceof TimeoutError) {// 请求超时 - show(dialog, R.string.error_timeout); - } else if (e instanceof UnKnownHostError) {// 找不到服务器 - show(dialog, R.string.error_not_found_server); - } else if (e instanceof URLError) {// URL是错的 - show(dialog, R.string.error_url_error); - } else if (e instanceof NotFoundCacheError) { - // 这个异常只会在仅仅查找缓存时没有找到缓存时返回 - show(dialog, R.string.error_not_found_cache); - } else if (e instanceof ProtocolException) { - show(dialog, R.string.error_system_unsupport_method); - } else if (e instanceof ConnectException) {//没有连接到服务器 - show(dialog, R.string.error_no_service); - } else { - Logger.e("NoHttpUtils捕获轮询异常:" + e.toString()); - StackTraceElement[] stackTrace = e.getStackTrace(); - if (null != stackTrace) { - for (StackTraceElement traceElement : stackTrace) { - Logger.e("NoHttpUtils捕获轮询异常:" + traceElement.toString()); - } - } - - if (TextUtils.isEmpty(anUnknownErrorHint)) { - show(dialog, R.string.error_unknow); - } else { - show(dialog, anUnknownErrorHint); - } - } - - - if (null != mOnIsRequestListener) { - mOnIsRequestListener.onError(e); - } - } - } else { - Logger.e(mRestRequest.url() + ":取消轮询请求线程"); - } - } - }; - } - - /** - * 内部实现是否取消轮询-拦截器 - */ - private void initBooleanFunc1() { - mBooleanFunc1 = new Predicate>() { - @Override - public boolean test(RxInformationModel tRxInformationModel) throws Exception { - //拦截传输过来的对象为null之时,创建一个对象关闭临时关闭轮询对象 - if (null == tRxInformationModel) { - tRxInformationModel = new RxInformationModel<>(); - tRxInformationModel.setStop(true); - } - Logger.e(mRestRequest.url() + ":轮询运行拦截>>拦截状态:" + tRxInformationModel.isStop()); - return tRxInformationModel.isStop(); - } - }; - } - - /** - * 内部实现轮询操作处理 - */ - private void initOnObserverEventListener() { - mOnObserverEventListener = new OnObserverEventListener, RxInformationModel>() { - @Override - public RxInformationModel onObserverEvent(RestRequest restRequest) { - mRestRequest = restRequest; - initTransitionModel(); - if (null != mDialogGetListener) { - //对话框放到主线程去运行 - AndroidSchedulers.mainThread().createWorker().schedule(new Runnable() { - @Override - public void run() { - Dialog dialog = mDialogGetListener.getDialog(); - if (null != dialog) { - dialog.show(); - } - } - }); - } - if (null != mRestRequest) { - Logger.e(mRestRequest.url() + ":轮询运行开始"); - Response response = NoHttp.startRequestSync(mRestRequest); - //正确 - if (response.isSucceed() || response.isFromCache()) { - mRxInformationModel.setData(response.get()); - } - //错误 - else { - mRxInformationModel.setException(true); - mRxInformationModel.setThrowable(response.getException()); - } - } else { - mRxInformationModel.setException(true); - mRxInformationModel.setThrowable(new NullPointerException("轮询运行开始请求参数为null,请反馈给框架提供者")); - } - return mRxInformationModel; - } - }; - } - - - /** - * 获取数据拦截监听对象 - * - * @return 数据拦截监听对象 - */ - public Predicate> getBooleanFunc1() { - return mBooleanFunc1; - } - - /** - * 设置数据拦截监听对象 - * - * @param booleanFunc1 数据拦截监听对象 - */ - public void setBooleanFunc1(Predicate> booleanFunc1) { - if (null != booleanFunc1) { - mBooleanFunc1 = booleanFunc1; - } - } - - /** - * 获取可观察者事件对象 - * - * @return 可观察者事件对象 - */ - public OnObserverEventListener, RxInformationModel> getOnObserverEventListener() { - return mOnObserverEventListener; - } - - /** - * 设置可观察者事件对象 - * - * @param onObserverEventListener 可观察者事件对象 - */ - public void setOnObserverEventListener(OnObserverEventListener, RxInformationModel> onObserverEventListener) { - mOnObserverEventListener = onObserverEventListener; - } - - /** - * 获取可观察者事件产生对应行动监听器 - * - * @return 可观察者事件产生对应行动监听器 - */ - public Consumer> getRxInformationModelAction1() { - return mRxInformationModelAction1; - } - - /** - * 赋值可观察者事件产生对应行动监听器 - * - * @param rxInformationModelAction1 可观察者事件产生对应行动监听器 - */ - public void setRxInformationModelAction1(Consumer> rxInformationModelAction1) { - mRxInformationModelAction1 = rxInformationModelAction1; - } - - /** - * 判断此标识是否是当前对象,并取消当前sign的请求 - * - * @param sign 标识 - * @return ok is true ,else false. - */ - public boolean isCancel(Object sign) { - return mSign == sign; - } - - /** - * 设置标识 - * - * @param sign 标识 - */ - public void setSign(Object sign) { - mSign = sign; - } - - - /** - * 取消请求 - */ - public void cancel() { - if (null != mRestRequest) { - mRestRequest.cancel(); - } - setRxPollStopState(true); - } - - /** - * 设置是否停止轮询 - * - * @param isStop true是停止,false是不停止 - */ - private void setRxPollStopState(boolean isStop) { - mRxInformationModel.setStop(isStop); - } - - /** - * 初始化转换对象内部信息 - */ - private void initTransitionModel() { - if (null != mRxInformationModel) { - mRxInformationModel.setData(null); - mRxInformationModel.setException(false); - mRxInformationModel.setThrowable(null); - } - } - - /** - * 土司提示 - * - * @param stringId 提示内容资源ID - */ - private void show(Dialog dialog, int stringId) { - if (null != dialog) { - Context context = dialog.getContext(); - Toast.makeText(context, context.getResources().getString(stringId), Toast.LENGTH_SHORT).show(); - } - } - - /** - * 土司提示 - * - * @param hint 提示内容 - */ - private void show(Dialog dialog, String hint) { - if (null != dialog) { - Context context = dialog.getContext(); - Toast.makeText(context, hint, Toast.LENGTH_SHORT).show(); - } - } -} diff --git a/nohttputils-r2/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 deleted file mode 100644 index 1fb6f54..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/operators/OnObserverEventListener.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.liqi.nohttputils.nohttp.rx_poll.operators; - -/**被观察者行动事件 - * Created by LiQi on 2017/9/6. - */ - -public interface OnObserverEventListener { - T onObserverEvent(V transferValue); -} 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 deleted file mode 100644 index b0228ff..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/operators/OnSubscribeTimerPeriodically.java +++ /dev/null @@ -1,206 +0,0 @@ -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-r2/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 deleted file mode 100644 index d091011..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_poll/pond/RxInformationPool.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.liqi.nohttputils.nohttp.rx_poll.pond; - - -import com.liqi.nohttputils.nohttp.rx_poll.interfa.OnRxInformationCancelListener; -import com.liqi.nohttputils.nohttp.rx_poll.interfa.OnRxInformationListListener; -import com.liqi.nohttputils.nohttp.rx_poll.model.RxInformationPoolModel; - -import java.util.LinkedList; -import java.util.List; - -/** - * RxJava轮询数据共享对象 - * Created by LiQi on 2017/9/8. - */ - -public class RxInformationPool implements OnRxInformationListListener, OnRxInformationCancelListener { - private static RxInformationPool mRxInformationPool; - private final List mPoolModels; - - private RxInformationPool() { - mPoolModels = new LinkedList<>(); - } - - public static OnRxInformationListListener getRxInformationPoolList() { - return mRxInformationPool = null == mRxInformationPool ? new RxInformationPool() : mRxInformationPool; - } - - public static OnRxInformationCancelListener getRxInformationPoolCancel() { - return mRxInformationPool = null == mRxInformationPool ? new RxInformationPool() : mRxInformationPool; - } - - @Override - public void add(RxInformationPoolModel informationPoolModel) { - mPoolModels.add(informationPoolModel); - } - - @Override - public RxInformationPoolModel get(int index) { - return mPoolModels.get(index % mPoolModels.size()); - } - - @Override - public int size() { - return mPoolModels.size(); - } - - @Override - public void delete(int index) { - mPoolModels.remove(index % mPoolModels.size()); - } - - private void deleteAll() { - if (!mPoolModels.isEmpty()) { - mPoolModels.clear(); - } - } - - @Override - public void cancel(Object sign) { - if (!mPoolModels.isEmpty()) { - for (int i = 0; i < mPoolModels.size(); i++) { - RxInformationPoolModel poolModel = mPoolModels.get(i); - if (poolModel.isCancel(sign)) { - poolModel.cancel(); - mPoolModels.remove(i); - } - } - } - } - - @Override - public void cancel(Object[] sign) { - if (null != sign && sign.length > 0) { - for (int i = 0; i < sign.length; i++) { - cancel(sign[i]); - } - } - } - - @Override - public void cancelAll() { - if (!mPoolModels.isEmpty()) { - for (int i = 0; i < size(); i++) { - mPoolModels.get(i).cancel(); - - } - } - deleteAll(); - } -} diff --git a/nohttputils-r2/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 deleted file mode 100644 index 76bbdf2..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/RxMessageSource.java +++ /dev/null @@ -1,112 +0,0 @@ -package com.liqi.nohttputils.nohttp.rx_threadpool; - - -import android.support.annotation.NonNull; - -import com.liqi.nohttputils.nohttp.rx_threadpool.interfa.OnRxMessageGetListener; -import com.liqi.nohttputils.nohttp.rx_threadpool.interfa.OnRxMessageSetListener; -import com.liqi.nohttputils.nohttp.rx_threadpool.model.RxRequestModel; - -import java.util.ArrayList; -import java.util.List; - -/** - * 数据源共享对象 - * Created by LiQi on 2017/3/20. - */ - -public class RxMessageSource implements OnRxMessageSetListener, OnRxMessageGetListener { - private static RxMessageSource mRxMessageSource; - //信息源集合 - private final List mRequestModels; - - private RxMessageSource() { - mRequestModels = new ArrayList<>(); - } - - public static RxMessageSource getRxMessageSource() { - return mRxMessageSource = null == mRxMessageSource ? new RxMessageSource() : mRxMessageSource; - } - - @Override - public RxMessageSource add(@NonNull RxRequestModel parameter, @NonNull Object sign) { - parameter.setSign(sign); - mRequestModels.add(parameter); - return mRxMessageSource; - } - - @Override - public List getList() { - return mRequestModels; - } - - @Override - public RxRequestModel get(int index) { - return mRequestModels.get(index % size()); - } - - @Override - public synchronized void delete(int index) { - mRequestModels.remove(index % size()); - } - - @Override - public int size() { - return mRequestModels.size(); - } - - private void deleteAll() { - if (!mRequestModels.isEmpty()) { - mRequestModels.clear(); - } - } - - - @Override - public RxMessageSource cancel(Object sign) { - if (!mRequestModels.isEmpty()) { - for (int i = 0; i < size(); i++) { - int index=i%size(); - RxRequestModel requestModel = mRequestModels.get(index); - if (null!=requestModel&&requestModel.isCancel(sign)) { - requestModel.cancel(); - delete(index); - } - } - } - return mRxMessageSource; - } - - @Override - public RxMessageSource cancel(Object[] sign) { - if (null != sign && sign.length > 0) { - for (int i = 0; i < sign.length; i++) { - cancel(sign[i]); - } - } - return mRxMessageSource; - } - - @Override - public RxMessageSource cancelAll() { - if (!mRequestModels.isEmpty()) { - for (int i = 0; i < size(); i++) { - int index=i%size(); - RxRequestModel requestModel = mRequestModels.get(index); - if (null!=requestModel) { - requestModel.cancel(); - requestModel.clear(); - } - - } - } - deleteAll(); - return mRxMessageSource; - } - - @Override - public RxMessageSource add(@NonNull RxRequestModel parameter) { - mRequestModels.add(parameter); - return mRxMessageSource; - } -} diff --git a/nohttputils-r2/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 deleted file mode 100644 index 0b45a8c..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/interfa/OnRxMessageGetListener.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.liqi.nohttputils.nohttp.rx_threadpool.interfa; - - -import com.liqi.nohttputils.nohttp.rx_threadpool.model.BaseRxRequestModel; - -import java.util.List; - -/** - * 数据源共享对象对内暴露接口 - * Created by LiQi on 2017/3/21. - */ - -public interface OnRxMessageGetListener { - /** - * 获取共享数据源 - * - * @return - */ - List getList(); - - /** - * 获取共享数据源数据 - * - * @param index - * @return - */ - T get(int index); - - /** - * 删除共享数据源数据 - * - * @param indxe - */ - void delete(int indxe); - - /** - * 获取共享数据源长度 - * - * @return - */ - int size(); -} diff --git a/nohttputils-r2/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 deleted file mode 100644 index 9ad339d..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/interfa/OnRxMessageSetListener.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.liqi.nohttputils.nohttp.rx_threadpool.interfa; - -import android.support.annotation.NonNull; - -import com.liqi.nohttputils.nohttp.rx_threadpool.RxMessageSource; -import com.liqi.nohttputils.nohttp.rx_threadpool.model.BaseRxRequestModel; - - -/** - * 数据源共享对象外部操作暴露接口 - * Created by LiQi on 2017/3/21. - */ - -public interface OnRxMessageSetListener { - /** - * 取消指定标识运行线程 - * - * @param sign 标识 - * @return - */ - RxMessageSource cancel(Object sign); - /** - * 批量取消指定标识运行线程 - * - * @param sign 标识 - * @return - */ - RxMessageSource cancel(Object[] sign); - /** - * 取消全部运行线程 - * - * @return - */ - RxMessageSource cancelAll(); - - /** - * 把要处理的数据添加进共享数据源 - * - * @param parameter 要处理的数据 - * @param sign 标识 - * @return - */ - RxMessageSource add(@NonNull T parameter, Object sign); - - /** - * 把要处理的数据添加进共享数据源 - * - * @param parameter 要处理的数据 - * @return - */ - RxMessageSource add(@NonNull T parameter); -} diff --git a/nohttputils-r2/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 deleted file mode 100644 index d696c4d..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/model/BaseRxRequestModel.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.liqi.nohttputils.nohttp.rx_threadpool.model; - - -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 ObservableOnSubscribe { - //是否需要打断线程标识 - private boolean isRunOff; - //是否已经有线程处理了 - private boolean isRunDispose; - //错误信息对象 - private Throwable mThrowable; - - /** - * 设置错误信息 - * - * @param throwable 错误对象 - */ - protected void setThrowable(Throwable throwable) { - mThrowable = throwable; - isRunOff = true; - } - - public boolean isRunDispose() { - return isRunDispose; - } - - public void setRunDispose(boolean runDispose) { - isRunDispose = runDispose; - } - - public boolean isRunOff() { - return isRunOff; - } - - public void setRunOff(boolean runOff) { - this.isRunOff = runOff; - } - - @Override - public void subscribe(@NonNull ObservableEmitter subscriber) throws Exception { - T t = run(); - if (!isRunOff()) - subscriber.onNext(t); - else { - subscriber.onError(mThrowable); - } - } - - /** - * 执行在子线程的方法 - * - * @return - */ - protected abstract T run(); -} diff --git a/nohttputils-r2/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 deleted file mode 100644 index 50bf50c..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/model/RxRequestModel.java +++ /dev/null @@ -1,118 +0,0 @@ -package com.liqi.nohttputils.nohttp.rx_threadpool.model; - -import android.support.annotation.NonNull; - -import com.liqi.nohttputils.interfa.DialogGetListener; -import com.liqi.nohttputils.interfa.OnIsRequestListener; -import com.yanzhenjie.nohttp.Logger; -import com.yanzhenjie.nohttp.NoHttp; -import com.yanzhenjie.nohttp.rest.Response; -import com.yanzhenjie.nohttp.rest.RestRequest; - - -/** - * 要处理网络请求数据模型 - * Created by LiQi on 2017/3/20. - */ - -public class RxRequestModel extends BaseRxRequestModel { - private DialogGetListener mDialogGetListener; - private RestRequest mRestRequest; - private OnIsRequestListener mOnIsRequestListener; - private Object mSign; - //未知错误提示语 - private String mAnUnknownErrorHint; - - private RxRequestModel() { - - } - - public RxRequestModel(@NonNull RestRequest restRequest, @NonNull OnIsRequestListener onIsRequestListener) { - mRestRequest = restRequest; - mOnIsRequestListener = onIsRequestListener; - } - - public String getAnUnknownErrorHint() { - return mAnUnknownErrorHint; - } - - public void setAnUnknownErrorHint(String anUnknownErrorHint) { - mAnUnknownErrorHint = anUnknownErrorHint; - } - - /** - * 释放当前对象内存 - */ - public void clear() { - mDialogGetListener = null; - mOnIsRequestListener = null; - mSign = null; - } - - /** - * 释放当前全部对象内存 - */ - public void clearAll() { - clear(); - mRestRequest = null; - } - - /** - * 判断此标识是否是当前对象,并取消当前sign的请求 - * - * @param sign 标识 - * @return - */ - public boolean isCancel(@NonNull Object sign) { - if (null != mRestRequest) { - if (mSign == sign) { - return true; - } - } - return false; - } - - public void setSign(@NonNull Object sign) { - mSign = sign; - } - - public void cancel() { - if (null != mRestRequest) { - mRestRequest.cancel(); - setRunOff(true); - } - } - - public DialogGetListener getDialogGetListener() { - return mDialogGetListener; - } - - public RxRequestModel setDialogGetListener(DialogGetListener dialogGetListener) { - mDialogGetListener = dialogGetListener; - return this; - } - - public OnIsRequestListener getOnIsRequestListener() { - return mOnIsRequestListener; - } - - @Override - protected T run() { - if (null != mRestRequest) { - Logger.e(mRestRequest.url() + "线程运行>>>"); - Response response = NoHttp.startRequestSync(mRestRequest); - if (response.isSucceed() || response.isFromCache()) { - if (!isRunOff()) { - return response.get(); - } else { - setThrowable(new Exception(mRestRequest.url() + " -->撤销请求")); - } - } else { - setThrowable(response.getException()); - } - } else { - setThrowable(new NullPointerException()); - } - return (T) mRestRequest.url(); - } -} diff --git a/nohttputils-r2/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 deleted file mode 100644 index 5d6202c..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/thread/RxThreadDispatch.java +++ /dev/null @@ -1,147 +0,0 @@ -package com.liqi.nohttputils.nohttp.rx_threadpool.thread; - -import android.support.annotation.NonNull; - -import com.liqi.nohttputils.nohttp.rx_threadpool.model.BaseRxRequestModel; -import com.yanzhenjie.nohttp.Logger; - -import java.util.List; - - -/** - * 分配线程(核心算法) - * Created by LiQi on 2017/3/20. - */ - -public class RxThreadDispatch extends Thread { - //固定并发最大值 - private int mFixedRunSize; - //要操作的并发最大值 - private int mRunSize; - //数据源 - private List mList; - //交个其它线程处理接口 - private OnRunDataDisListener mOnRunDataDisListener; - //线程状态 - private boolean isRunState = true; - //是否把数据源数据运行标识 - private boolean isRunSize; - //是否终止运行标识 - private boolean isRunTag = true; - - public RxThreadDispatch(int runSize, @NonNull List list) { - this.mRunSize = runSize; - this.mFixedRunSize = runSize; - this.mList = list; - } - - private RxThreadDispatch() { - } - - public boolean isRunState() { - return isRunState; - } - - public RxThreadDispatch setRunSize(boolean runSize) { - isRunSize = runSize; - return this; - } - - public void setRunTag(boolean runTag) { - this.isRunTag = runTag; - } - - public void setOnRunDataDisListener(OnRunDataDisListener onRunDataDisListener) { - mOnRunDataDisListener = onRunDataDisListener; - } - - @Override - public void run() { - while (isRunTag) { - isRunState = true; - Logger.e("运行次数前mRunSize:" + mRunSize + "<<<<数据源总长度:>>>>" + mList.size()); - - //开启多少条线程并发 - tag: - while (!isRunSize && mRunSize - 1 >= 0) { - mRunSize = mRunSize - 1; - // Logger.e("运行次数后mRunSize:"+mRunSize); - - if (!mList.isEmpty()) { - int runDisposeSize = 0; - for (int i = 0; i < mList.size(); i++) { - if (i < mList.size()) { - BaseRxRequestModel baseRxRequestModel = mList.get(i); - if (null != baseRxRequestModel) { - //判断是否有线程处理此对象 - if (!baseRxRequestModel.isRunDispose()) { - baseRxRequestModel.setRunDispose(true); - if (null != mOnRunDataDisListener) { - mOnRunDataDisListener.getRunData(baseRxRequestModel); - continue tag; - } - } else { - ++runDisposeSize; - } - } - } else { - break; - } - } - - if (runDisposeSize >= mList.size()) { - //如果全部数据源对象全部在处理,那么线程用掉的次数回滚 - ++mRunSize; - isRunState = false; - isRunSize = true; - // Logger.e("线程休眠01:"+"已经运行>>" + runDisposeSize + "总数据源:" + size); - if (null != mOnRunDataDisListener) { - mOnRunDataDisListener.waitThread(); - } - } - - } else { - //如果数据源对象为空,那么线程用掉的次数回滚 - ++mRunSize; - isRunState = false; - isRunSize = true; - // Logger.e("线程休眠02:数据源为空"); - if (null != mOnRunDataDisListener) { - mOnRunDataDisListener.waitThread(); - } - } - } - isRunState = false; - if (null != mOnRunDataDisListener) { - // Logger.e("线程休眠03:运行次数用完"); - mOnRunDataDisListener.waitThread(); - } - // Logger.e("线程运行:线程运行中"); - } - } - - /** - * rxjava线程处理完数据,退回分配线程的次数 - */ - public void addRunSize() { - synchronized (this) { - ++mRunSize; - - if (mRunSize > mFixedRunSize) { - mRunSize = mFixedRunSize; - } - //Logger.e("运行次数:++运行次数相加" + mRunSize); - } - } - - /** - * 分配线程用来跟中转对象通讯的接口 - */ - public interface OnRunDataDisListener { - //获取分配拿出来的要处理数据 - void getRunData(BaseRxRequestModel runData); - - //休眠分配线程 - void waitThread(); - } -} diff --git a/nohttputils-r2/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 deleted file mode 100644 index e942663..0000000 --- a/nohttputils-r2/src/main/java/com/liqi/nohttputils/nohttp/rx_threadpool/utils/RxThreadPoolUtisl.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.liqi.nohttputils.nohttp.rx_threadpool.utils; - - -import com.liqi.nohttputils.nohttp.RxThreadInterchange; - -/** - * 分配线程唤醒和休眠工具 - * Created by LiQi on 2017/3/20. - */ - -public class RxThreadPoolUtisl { - - public static void threadWait(RxThreadInterchange rxThreadInterchange) { - synchronized (rxThreadInterchange) { - try { - rxThreadInterchange.wait(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - } - - public static void threadNotify(RxThreadInterchange rxThreadInterchange) { - synchronized (rxThreadInterchange) { - rxThreadInterchange.notify(); - } - } -} diff --git a/nohttputils-r2/src/main/res/values/strings.xml b/nohttputils-r2/src/main/res/values/strings.xml deleted file mode 100644 index 7c851e7..0000000 --- a/nohttputils-r2/src/main/res/values/strings.xml +++ /dev/null @@ -1,14 +0,0 @@ - - NoHttpUtils - 请检查网络。 - 请求超时,网络不好或者服务器不稳定。 - 未发现指定服务器,清切换网络后重试。 - URL错误。 - 没有找到缓存. - 系统不支持的请求方法。 - 解析数据时发生错误。 - 未能连接到服务器 - 连接出错,请查看错误日志。 - 服务器响应码%1$d - 服务器响应码%1$d,如果你们服务器在这种情况下也返回数据你可以处理。 - diff --git a/settings.gradle b/settings.gradle index f8549d5..fdae6a5 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1 @@ -include ':sample', ':nohttputils-r1', ':nohttputils-r2' +include ':sample', ':nohttputils-r1'