├── .gitignore
├── LICENSE
├── README.md
├── app
├── build.gradle
├── proguard-rules.pro
├── release
│ ├── app-release.apk
│ └── output.json
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── per
│ │ └── goweii
│ │ └── android
│ │ └── rxhttp
│ │ ├── MainActivity.java
│ │ ├── TestDownloadActivity.java
│ │ ├── TestRequestActivity.java
│ │ ├── bean
│ │ ├── RecommendPoetryBean.java
│ │ ├── SinglePoetryBean.java
│ │ └── WeatherBean.java
│ │ └── http
│ │ ├── FreeApi.java
│ │ └── ResponseBean.java
│ └── res
│ ├── drawable-v24
│ └── ic_launcher_foreground.xml
│ ├── drawable
│ └── ic_launcher_background.xml
│ ├── layout
│ ├── activity_main.xml
│ ├── activity_test_download.xml
│ └── activity_test_request.xml
│ ├── mipmap-anydpi-v26
│ ├── ic_launcher.xml
│ └── ic_launcher_round.xml
│ ├── mipmap-hdpi
│ ├── ic_launcher.png
│ └── ic_launcher_round.png
│ ├── mipmap-mdpi
│ ├── ic_launcher.png
│ └── ic_launcher_round.png
│ ├── mipmap-xhdpi
│ ├── ic_launcher.png
│ └── ic_launcher_round.png
│ ├── mipmap-xxhdpi
│ ├── ic_launcher.png
│ └── ic_launcher_round.png
│ ├── mipmap-xxxhdpi
│ ├── ic_launcher.png
│ └── ic_launcher_round.png
│ └── values
│ ├── colors.xml
│ ├── strings.xml
│ └── styles.xml
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── rxhttp
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── per
│ │ └── goweii
│ │ └── rxhttp
│ │ ├── core
│ │ ├── RxHttp.java
│ │ ├── RxLife.java
│ │ ├── exception
│ │ │ └── RxHttpUninitializedException.java
│ │ ├── manager
│ │ │ └── BaseClientManager.java
│ │ └── utils
│ │ │ ├── BaseUrlUtils.java
│ │ │ └── SDCardUtils.java
│ │ ├── download
│ │ ├── DownloadApi.java
│ │ ├── DownloadClientManager.java
│ │ ├── DownloadInfo.java
│ │ ├── RxDownload.java
│ │ ├── exception
│ │ │ ├── RangeLengthIsZeroException.java
│ │ │ ├── SaveFileBrokenPointException.java
│ │ │ ├── SaveFileDirMakeException.java
│ │ │ └── SaveFileWriteException.java
│ │ ├── interceptor
│ │ │ ├── DownloadResponseBody.java
│ │ │ └── RealNameInterceptor.java
│ │ ├── setting
│ │ │ ├── DefaultDownloadSetting.java
│ │ │ └── DownloadSetting.java
│ │ └── utils
│ │ │ ├── DownloadInfoChecker.java
│ │ │ ├── RxNotify.java
│ │ │ └── UnitFormatUtils.java
│ │ └── request
│ │ ├── Api.java
│ │ ├── RequestClientManager.java
│ │ ├── RxRequest.java
│ │ ├── RxResponse.java
│ │ ├── base
│ │ ├── BaseBean.java
│ │ └── BaseResponse.java
│ │ ├── exception
│ │ ├── ApiException.java
│ │ ├── ExceptionHandle.java
│ │ └── NullRequestSettingException.java
│ │ ├── gson
│ │ ├── BaseBeanDeserializer.java
│ │ ├── IntDeserializer.java
│ │ ├── ListDeserializer.java
│ │ └── StringDeserializer.java
│ │ ├── interceptor
│ │ ├── BaseCacheControlInterceptor.java
│ │ ├── BaseUrlRedirectInterceptor.java
│ │ ├── CacheControlInterceptor.java
│ │ ├── CacheControlNetworkInterceptor.java
│ │ ├── PublicHeadersInterceptor.java
│ │ └── PublicQueryParameterInterceptor.java
│ │ ├── setting
│ │ ├── DefaultRequestSetting.java
│ │ ├── ParameterGetter.java
│ │ └── RequestSetting.java
│ │ └── utils
│ │ ├── FileUtils.java
│ │ ├── HttpsCompat.java
│ │ ├── JsonFormatUtils.java
│ │ ├── JsonObjUtils.java
│ │ ├── NetUtils.java
│ │ ├── NonNullUtils.java
│ │ └── RequestBodyUtils.java
│ └── res
│ └── values
│ └── strings.xml
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /.idea
4 | .DS_Store
5 | /build
6 | /captures
7 | .externalNativeBuild
8 | ### Android template
9 | # Built application files
10 |
11 | # Files for the ART/Dalvik VM
12 | *.dex
13 |
14 | # Java class files
15 | *.class
16 |
17 | # Generated files
18 | bin/
19 | gen/
20 | out/
21 |
22 | # Gradle files
23 | .gradle/
24 | build/
25 |
26 | # Local configuration file (sdk path, etc)
27 | local.properties
28 |
29 | # Proguard folder generated by Eclipse
30 | proguard/
31 |
32 | # Log Files
33 | *.log
34 |
35 | # Android Studio Navigation editor temp files
36 | .navigation/
37 |
38 | # Android Studio captures folder
39 | captures/
40 |
41 | # IntelliJ
42 | *.iml
43 | .idea/
44 |
45 |
46 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 28
5 | buildToolsVersion '28.0.3'
6 | defaultConfig {
7 | applicationId "per.goweii.android.rxhttp"
8 | minSdkVersion 14
9 | targetSdkVersion 28
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | compileOptions {
21 | sourceCompatibility JavaVersion.VERSION_1_8
22 | targetCompatibility JavaVersion.VERSION_1_8
23 | }
24 | }
25 |
26 | dependencies {
27 | implementation fileTree(include: ['*.jar'], dir: 'libs')
28 | implementation 'com.android.support:appcompat-v7:28.0.0'
29 | api project(':rxhttp')
30 | }
31 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/release/app-release.apk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/goweii/RxHttp/e50b8110a88cc4b3b38b47d7daaabf213ceea877/app/release/app-release.apk
--------------------------------------------------------------------------------
/app/release/output.json:
--------------------------------------------------------------------------------
1 | [{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":1,"versionName":"1.0","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/app/src/main/java/per/goweii/android/rxhttp/MainActivity.java:
--------------------------------------------------------------------------------
1 | package per.goweii.android.rxhttp;
2 |
3 | import android.content.Intent;
4 | import android.os.Bundle;
5 | import android.support.v7.app.AppCompatActivity;
6 | import android.view.View;
7 |
8 | import per.goweii.rxhttp.core.RxHttp;
9 |
10 | public class MainActivity extends AppCompatActivity {
11 |
12 | @Override
13 | protected void onCreate(Bundle savedInstanceState) {
14 | super.onCreate(savedInstanceState);
15 | setContentView(R.layout.activity_main);
16 |
17 | RxHttp.init(this);
18 |
19 | findViewById(R.id.tv_go_test_request).setOnClickListener(new View.OnClickListener() {
20 | @Override
21 | public void onClick(View v) {
22 | Intent intent = new Intent(MainActivity.this, TestRequestActivity.class);
23 | startActivity(intent);
24 | }
25 | });
26 |
27 | findViewById(R.id.tv_go_test_download).setOnClickListener(new View.OnClickListener() {
28 | @Override
29 | public void onClick(View v) {
30 | Intent intent = new Intent(MainActivity.this, TestDownloadActivity.class);
31 | startActivity(intent);
32 | }
33 | });
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/app/src/main/java/per/goweii/android/rxhttp/TestDownloadActivity.java:
--------------------------------------------------------------------------------
1 | package per.goweii.android.rxhttp;
2 |
3 | import android.content.SharedPreferences;
4 | import android.os.Bundle;
5 | import android.preference.PreferenceManager;
6 | import android.support.v7.app.AppCompatActivity;
7 | import android.text.TextUtils;
8 | import android.view.KeyEvent;
9 | import android.view.View;
10 | import android.widget.EditText;
11 | import android.widget.ProgressBar;
12 | import android.widget.TextView;
13 |
14 | import per.goweii.rxhttp.download.DownloadInfo;
15 | import per.goweii.rxhttp.download.RxDownload;
16 | import per.goweii.rxhttp.download.utils.UnitFormatUtils;
17 |
18 | public class TestDownloadActivity extends AppCompatActivity {
19 |
20 | public static final String url_1 = "https://imtt.dd.qq.com/16891/601BBD228F1F77DB1FB03FE38EF9BC93.apk?fsname=com.tencent.tmgp.sgame_1.41.2.4_41020401.apk&csr=1bbd";
21 | public static final String url = "https://imtt.dd.qq.com/16891/513D2C5324E6EBE77F94C85D7C76EBAE.apk?fsname=com.tencent.mobileqq_7.8.2_926.apk&csr=1bbd";
22 | private RxDownload mRxDownload;
23 | private boolean isStart = false;
24 |
25 | @Override
26 | protected void onCreate(Bundle savedInstanceState) {
27 | super.onCreate(savedInstanceState);
28 | setContentView(R.layout.activity_test_download);
29 |
30 | EditText et_url = findViewById(R.id.et_url);
31 | ProgressBar pb_1 = findViewById(R.id.pb_1);
32 | pb_1.setMax(10000);
33 | TextView tv_download_length = findViewById(R.id.tv_download_length);
34 | TextView tv_content_length = findViewById(R.id.tv_content_length);
35 | TextView tv_speed = findViewById(R.id.tv_speed);
36 | TextView tv_start_stop = findViewById(R.id.tv_start_stop);
37 | TextView tv_cancel = findViewById(R.id.tv_cancel);
38 | TextView tv_clean = findViewById(R.id.tv_clean);
39 |
40 | et_url.setText(url);
41 |
42 | tv_start_stop.setOnClickListener(new View.OnClickListener() {
43 | @Override
44 | public void onClick(View v) {
45 | if (isStart) {
46 | mRxDownload.stop();
47 | isStart = false;
48 | } else {
49 | mRxDownload.start();
50 | isStart = true;
51 | }
52 | }
53 | });
54 |
55 | tv_cancel.setOnClickListener(new View.OnClickListener() {
56 | @Override
57 | public void onClick(View v) {
58 | mRxDownload.cancel();
59 | isStart = false;
60 | }
61 | });
62 |
63 | tv_clean.setOnClickListener(new View.OnClickListener() {
64 | @Override
65 | public void onClick(View v) {
66 | cleanDownloadInfo();
67 | tv_download_length.setText("");
68 | tv_content_length.setText("");
69 | tv_speed.setText("");
70 | pb_1.setProgress(0);
71 | tv_start_stop.setText("开始下载");
72 | tv_cancel.setText("取消下载");
73 | }
74 | });
75 |
76 | DownloadInfo downloadInfo = getDownloadInfo();
77 | if (downloadInfo == null) {
78 | mRxDownload = RxDownload.create(DownloadInfo.create(et_url.getText().toString()));
79 | } else {
80 | pb_1.setProgress((int) (((float)downloadInfo.downloadLength / (float)downloadInfo.contentLength) * 10000));
81 | tv_download_length.setText(UnitFormatUtils.formatBytesLength(downloadInfo.downloadLength));
82 | tv_content_length.setText(UnitFormatUtils.formatBytesLength(downloadInfo.contentLength));
83 | DownloadInfo info = DownloadInfo.create(downloadInfo.url,
84 | downloadInfo.saveDirPath, downloadInfo.saveFileName,
85 | downloadInfo.downloadLength, downloadInfo.contentLength);
86 | mRxDownload = RxDownload.create(info);
87 | }
88 | mRxDownload.setDownloadListener(new RxDownload.DownloadListener() {
89 | @Override
90 | public void onStarting(DownloadInfo info) {
91 | tv_start_stop.setText("暂停下载");
92 | tv_cancel.setText("取消下载");
93 | }
94 |
95 | @Override
96 | public void onDownloading(DownloadInfo info) {
97 | tv_start_stop.setText("暂停下载");
98 | }
99 |
100 | @Override
101 | public void onError(DownloadInfo info, Throwable e) {
102 | saveDownloadInfo();
103 | tv_start_stop.setText("开始下载");
104 | tv_speed.setText("");
105 | }
106 |
107 | @Override
108 | public void onStopped(DownloadInfo info) {
109 | saveDownloadInfo();
110 | tv_start_stop.setText("开始下载");
111 | tv_speed.setText("");
112 | }
113 |
114 | @Override
115 | public void onCanceled(DownloadInfo info) {
116 | saveDownloadInfo();
117 | tv_start_stop.setText("开始下载");
118 | tv_cancel.setText("已取消");
119 | pb_1.setProgress(0);
120 | tv_speed.setText("");
121 | tv_download_length.setText("");
122 | tv_content_length.setText("");
123 | }
124 |
125 | @Override
126 | public void onCompletion(DownloadInfo info) {
127 | saveDownloadInfo();
128 | tv_start_stop.setText("下载成功");
129 | tv_speed.setText("");
130 | }
131 | })
132 | .setProgressListener(new RxDownload.ProgressListener() {
133 | @Override
134 | public void onProgress(float progress, long downloadLength, long contentLength) {
135 | pb_1.setProgress((int) (progress * 10000));
136 | tv_download_length.setText(UnitFormatUtils.formatBytesLength(downloadLength));
137 | tv_content_length.setText(UnitFormatUtils.formatBytesLength(contentLength));
138 | }
139 | })
140 | .setSpeedListener(new RxDownload.SpeedListener() {
141 | @Override
142 | public void onSpeedChange(float bytePerSecond, String speedFormat) {
143 | tv_speed.setText(speedFormat);
144 | }
145 | });
146 | }
147 |
148 | @Override
149 | public boolean onKeyDown(int keyCode, KeyEvent event) {
150 | if (event.getAction() == KeyEvent.ACTION_DOWN) {
151 | if (keyCode == KeyEvent.KEYCODE_BACK) {
152 | if (isStart) {
153 | mRxDownload.stop();
154 | isStart = false;
155 | return false;
156 | }
157 | }
158 | }
159 | return super.onKeyDown(keyCode, event);
160 | }
161 |
162 | private DownloadInfo getDownloadInfo() {
163 | SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
164 | String url = sp.getString("url", "");
165 | String saveDirName = sp.getString("saveDirPath", "");
166 | String saveFileName = sp.getString("saveFileName", "");
167 | long downloadLength = sp.getLong("downloadLength", 0);
168 | long contentLength = sp.getLong("contentLength", 0);
169 | if (TextUtils.isEmpty(url) || TextUtils.isEmpty(saveDirName) || TextUtils.isEmpty(saveFileName)){
170 | return null;
171 | }
172 | if (downloadLength == 0){
173 | return null;
174 | }
175 | if (contentLength < downloadLength){
176 | return null;
177 | }
178 | return DownloadInfo.create(url, saveDirName, saveFileName, downloadLength, contentLength);
179 | }
180 |
181 | private void saveDownloadInfo() {
182 | DownloadInfo info = mRxDownload.getDownloadInfo();
183 | SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
184 | SharedPreferences.Editor editor = sp.edit();
185 | editor.putString("url", info.url);
186 | editor.putString("saveDirPath", info.saveDirPath);
187 | editor.putString("saveFileName", info.saveFileName);
188 | editor.putLong("downloadLength", info.downloadLength);
189 | editor.putLong("contentLength", info.contentLength);
190 | editor.apply();
191 | }
192 |
193 | private void cleanDownloadInfo() {
194 | SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
195 | sp.edit().clear().apply();
196 | }
197 | }
198 |
--------------------------------------------------------------------------------
/app/src/main/java/per/goweii/android/rxhttp/TestRequestActivity.java:
--------------------------------------------------------------------------------
1 | package per.goweii.android.rxhttp;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.NonNull;
5 | import android.support.v7.app.AppCompatActivity;
6 | import android.text.TextUtils;
7 | import android.util.Log;
8 | import android.view.View;
9 | import android.widget.EditText;
10 | import android.widget.TextView;
11 |
12 | import java.util.HashMap;
13 | import java.util.Map;
14 |
15 | import javax.net.ssl.HostnameVerifier;
16 | import javax.net.ssl.SSLSession;
17 |
18 | import okhttp3.OkHttpClient;
19 | import per.goweii.android.rxhttp.bean.RecommendPoetryBean;
20 | import per.goweii.android.rxhttp.bean.SinglePoetryBean;
21 | import per.goweii.android.rxhttp.bean.WeatherBean;
22 | import per.goweii.android.rxhttp.http.FreeApi;
23 | import per.goweii.rxhttp.core.RxHttp;
24 | import per.goweii.rxhttp.core.RxLife;
25 | import per.goweii.rxhttp.request.RxRequest;
26 | import per.goweii.rxhttp.request.base.BaseBean;
27 | import per.goweii.rxhttp.request.exception.ExceptionHandle;
28 | import per.goweii.rxhttp.request.setting.DefaultRequestSetting;
29 | import per.goweii.rxhttp.request.setting.ParameterGetter;
30 |
31 | public class TestRequestActivity extends AppCompatActivity {
32 | private static final String TAG = "TestRequestActivity";
33 |
34 | private RxLife mRxLife;
35 | private TextView tvLog;
36 |
37 | @Override
38 | protected void onCreate(Bundle savedInstanceState) {
39 | super.onCreate(savedInstanceState);
40 | setContentView(R.layout.activity_test_request);
41 | RxHttp.initRequest(new DefaultRequestSetting() {
42 | @NonNull
43 | @Override
44 | public String getBaseUrl() {
45 | return FreeApi.Config.BASE_URL;
46 | }
47 |
48 | @Override
49 | public Map getRedirectBaseUrl() {
50 | Map urls = new HashMap<>(2);
51 | urls.put(FreeApi.Config.BASE_URL_OTHER_NAME, FreeApi.Config.BASE_URL_OTHER);
52 | urls.put(FreeApi.Config.BASE_URL_ERROR_NAME, FreeApi.Config.BASE_URL_ERROR);
53 | urls.put(FreeApi.Config.BASE_URL_HTTPS_NAME, FreeApi.Config.BASE_URL_HTTPS);
54 | return urls;
55 | }
56 |
57 | @Override
58 | public int getSuccessCode() {
59 | return FreeApi.Code.SUCCESS;
60 | }
61 |
62 | @Override
63 | public Map getStaticPublicQueryParameter() {
64 | Map parameters = new HashMap<>(2);
65 | parameters.put("system", "android");
66 | parameters.put("version_code", "1");
67 | parameters.put("device_num", "666");
68 | return parameters;
69 | }
70 |
71 | @Override
72 | public Map getDynamicPublicQueryParameter() {
73 | Map parameters = new HashMap<>(2);
74 | parameters.put("user_id", new ParameterGetter() {
75 | @io.reactivex.annotations.NonNull
76 | @Override
77 | public String get() {
78 | return "100001";
79 | }
80 | });
81 | return parameters;
82 | }
83 |
84 | @Override
85 | public void setOkHttpClient(@io.reactivex.annotations.NonNull OkHttpClient.Builder builder) {
86 | builder.hostnameVerifier(new HostnameVerifier() {
87 | @Override
88 | public boolean verify(String hostname, SSLSession session) {
89 | return true;
90 | }
91 | });
92 | }
93 | });
94 | mRxLife = RxLife.create();
95 |
96 | tvLog = findViewById(R.id.tv_log);
97 | findViewById(R.id.tv_get_singlePoetry).setOnClickListener(new View.OnClickListener() {
98 | @Override
99 | public void onClick(View v) {
100 | getSinglePoetry();
101 | }
102 | });
103 | findViewById(R.id.tv_get_recommendPoetry).setOnClickListener(new View.OnClickListener() {
104 | @Override
105 | public void onClick(View v) {
106 | getRecommendPoetry();
107 | }
108 | });
109 | findViewById(R.id.tv_get_weather).setOnClickListener(new View.OnClickListener() {
110 | @Override
111 | public void onClick(View v) {
112 | EditText etWeatherCity = findViewById(R.id.et_weather_city);
113 | getWeather(etWeatherCity.getText().toString());
114 | }
115 | });
116 | findViewById(R.id.tv_http_host_error).setOnClickListener(new View.OnClickListener() {
117 | @Override
118 | public void onClick(View v) {
119 | getErrorHost();
120 | }
121 | });
122 | findViewById(R.id.tv_https).setOnClickListener(new View.OnClickListener() {
123 | @Override
124 | public void onClick(View v) {
125 | getHttps();
126 | }
127 | });
128 | }
129 |
130 | @Override
131 | protected void onDestroy() {
132 | super.onDestroy();
133 | mRxLife.destroy();
134 | }
135 |
136 | private void getSinglePoetry() {
137 | mRxLife.add(RxHttp.request(FreeApi.api().singlePoetry()).listener(new RxRequest.RequestListener() {
138 | private long timeStart = 0;
139 |
140 | @Override
141 | public void onStart() {
142 | log(null);
143 | log("onStart()");
144 | timeStart = System.currentTimeMillis();
145 | }
146 |
147 | @Override
148 | public void onError(@NonNull ExceptionHandle handle) {
149 | log("onError(" + handle.getMsg() + ")");
150 | }
151 |
152 | @Override
153 | public void onFinish() {
154 | long cast = System.currentTimeMillis() - timeStart;
155 | log("onFinish(cast=" + cast + ")");
156 | }
157 | }).request(new RxRequest.ResultCallback() {
158 | @Override
159 | public void onSuccess(int code, SinglePoetryBean data) {
160 | log("onSuccess(code=" + code + ",data=" + data.toFormatJson() + ")");
161 | }
162 |
163 | @Override
164 | public void onFailed(int code, String msg) {
165 | log("onFailed(code=" + code + ",msg=" + msg + ")");
166 | }
167 | }));
168 | }
169 |
170 | private void getRecommendPoetry() {
171 | mRxLife.add(RxRequest.create(FreeApi.api().recommendPoetry()).listener(new RxRequest.RequestListener() {
172 | private long timeStart = 0;
173 |
174 | @Override
175 | public void onStart() {
176 | log(null);
177 | log("onStart()");
178 | timeStart = System.currentTimeMillis();
179 | }
180 |
181 | @Override
182 | public void onError(@NonNull ExceptionHandle handle) {
183 | log("onError(" + handle.getMsg() + ")");
184 | }
185 |
186 | @Override
187 | public void onFinish() {
188 | long cast = System.currentTimeMillis() - timeStart;
189 | log("onFinish(cast=" + cast + ")");
190 | }
191 | }).request(new RxRequest.ResultCallback() {
192 | @Override
193 | public void onSuccess(int code, RecommendPoetryBean data) {
194 | log("onSuccess(code=" + code + ",data=" + data.toFormatJson() + ")");
195 | }
196 |
197 | @Override
198 | public void onFailed(int code, String msg) {
199 | log("onFailed(code=" + code + ",msg=" + msg + ")");
200 | }
201 | }));
202 | }
203 |
204 | private void getWeather(String city) {
205 | mRxLife.add(RxRequest.create(FreeApi.api().weather(city)).listener(new RxRequest.RequestListener() {
206 | private long timeStart = 0;
207 |
208 | @Override
209 | public void onStart() {
210 | log(null);
211 | log("onStart()");
212 | timeStart = System.currentTimeMillis();
213 | }
214 |
215 | @Override
216 | public void onError(@NonNull ExceptionHandle handle) {
217 | log("onError(" + handle.getMsg() + ")");
218 | }
219 |
220 | @Override
221 | public void onFinish() {
222 | long cast = System.currentTimeMillis() - timeStart;
223 | log("onFinish(cast=" + cast + ")");
224 | }
225 | }).request(new RxRequest.ResultCallback() {
226 | @Override
227 | public void onSuccess(int code, WeatherBean data) {
228 | log("onSuccess(code=" + code + ",data=" + data.toFormatJson() + ")");
229 | }
230 |
231 | @Override
232 | public void onFailed(int code, String msg) {
233 | log("onFailed(code=" + code + ",msg=" + msg + ")");
234 | }
235 | }));
236 | }
237 |
238 | private void getErrorHost() {
239 | mRxLife.add(RxRequest.create(FreeApi.api().errorHost()).listener(new RxRequest.RequestListener() {
240 | private long timeStart = 0;
241 |
242 | @Override
243 | public void onStart() {
244 | log(null);
245 | log("onStart()");
246 | timeStart = System.currentTimeMillis();
247 | }
248 |
249 | @Override
250 | public void onError(@NonNull ExceptionHandle handle) {
251 | log("onError(" + handle.getMsg() + ")");
252 | }
253 |
254 | @Override
255 | public void onFinish() {
256 | long cast = System.currentTimeMillis() - timeStart;
257 | log("onFinish(cast=" + cast + ")");
258 | }
259 | }).request(new RxRequest.ResultCallback() {
260 | @Override
261 | public void onSuccess(int code, BaseBean data) {
262 | log("onSuccess(code=" + code + ",data=" + data.toFormatJson() + ")");
263 | }
264 |
265 | @Override
266 | public void onFailed(int code, String msg) {
267 | log("onFailed(code=" + code + ",msg=" + msg + ")");
268 | }
269 | }));
270 | }
271 |
272 | private void getHttps() {
273 | mRxLife.add(RxRequest.create(FreeApi.api().https("哈哈")).listener(new RxRequest.RequestListener() {
274 | private long timeStart = 0;
275 |
276 | @Override
277 | public void onStart() {
278 | log(null);
279 | log("onStart()");
280 | timeStart = System.currentTimeMillis();
281 | }
282 |
283 | @Override
284 | public void onError(@NonNull ExceptionHandle handle) {
285 | log("onError(" + handle.getMsg() + ")");
286 | }
287 |
288 | @Override
289 | public void onFinish() {
290 | long cast = System.currentTimeMillis() - timeStart;
291 | log("onFinish(cast=" + cast + ")");
292 | }
293 | }).request(new RxRequest.ResultCallback() {
294 | @Override
295 | public void onSuccess(int code, BaseBean data) {
296 | log("onSuccess(code=" + code + ",data=" + data.toFormatJson() + ")");
297 | }
298 |
299 | @Override
300 | public void onFailed(int code, String msg) {
301 | log("onFailed(code=" + code + ",msg=" + msg + ")");
302 | }
303 | }));
304 | }
305 |
306 | private void log(String text){
307 | if (text == null) {
308 | tvLog.setText("");
309 | } else {
310 | Log.d(TAG, text);
311 | String textOld = tvLog.getText().toString();
312 | if (TextUtils.isEmpty(textOld)) {
313 | tvLog.setText(text);
314 | } else {
315 | tvLog.setText(tvLog.getText().toString() + "\n" + text);
316 | }
317 | }
318 | }
319 | }
320 |
--------------------------------------------------------------------------------
/app/src/main/java/per/goweii/android/rxhttp/bean/RecommendPoetryBean.java:
--------------------------------------------------------------------------------
1 | package per.goweii.android.rxhttp.bean;
2 |
3 | import per.goweii.rxhttp.request.base.BaseBean;
4 |
5 | /**
6 | * 描述:
7 | *
8 | * @author Cuizhen
9 | * @date 2018/10/13
10 | */
11 | public class RecommendPoetryBean extends BaseBean {
12 | /**
13 | * title : 饯郑安阳入蜀
14 | * content : 彭山折坂外,井络少城隈。|地是三巴俗,人非百里材。|畏途君怅望,岐路我裴徊。|心赏风烟隔,容华岁月催。|遥遥分凤野,去去转龙媒。|遗锦非前邑,鸣琴即旧台。|剑门千仞起,石路五丁开。|海客乘槎渡,仙童驭竹回。|魂将离鹤远,思逐断猿哀。|唯有双凫舄,飞去复飞来。
15 | * authors : 骆宾王
16 | */
17 |
18 | private String title;
19 | private String content;
20 | private String authors;
21 |
22 | public String getTitle() {
23 | return title;
24 | }
25 |
26 | public void setTitle(String title) {
27 | this.title = title;
28 | }
29 |
30 | public String getContent() {
31 | return content;
32 | }
33 |
34 | public void setContent(String content) {
35 | this.content = content;
36 | }
37 |
38 | public String getAuthors() {
39 | return authors;
40 | }
41 |
42 | public void setAuthors(String authors) {
43 | this.authors = authors;
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/app/src/main/java/per/goweii/android/rxhttp/bean/SinglePoetryBean.java:
--------------------------------------------------------------------------------
1 | package per.goweii.android.rxhttp.bean;
2 |
3 | import per.goweii.rxhttp.request.base.BaseBean;
4 |
5 | /**
6 | * 描述:
7 | *
8 | * @author Cuizhen
9 | * @date 2018/10/13
10 | */
11 | public class SinglePoetryBean extends BaseBean {
12 | /**
13 | * author : 苏辙
14 | * origin : 水调歌头·徐州中秋
15 | * category : 古诗文-节日-中秋节
16 | * content : 离别一何久,七度过中秋。
17 | */
18 |
19 | private String author;
20 | private String origin;
21 | private String category;
22 | private String content;
23 |
24 | public String getAuthor() {
25 | return author;
26 | }
27 |
28 | public void setAuthor(String author) {
29 | this.author = author;
30 | }
31 |
32 | public String getOrigin() {
33 | return origin;
34 | }
35 |
36 | public void setOrigin(String origin) {
37 | this.origin = origin;
38 | }
39 |
40 | public String getCategory() {
41 | return category;
42 | }
43 |
44 | public void setCategory(String category) {
45 | this.category = category;
46 | }
47 |
48 | public String getContent() {
49 | return content;
50 | }
51 |
52 | public void setContent(String content) {
53 | this.content = content;
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/app/src/main/java/per/goweii/android/rxhttp/bean/WeatherBean.java:
--------------------------------------------------------------------------------
1 | package per.goweii.android.rxhttp.bean;
2 |
3 | import java.util.List;
4 |
5 | import per.goweii.rxhttp.request.base.BaseBean;
6 |
7 | /**
8 | * 描述:
9 | *
10 | * @author Cuizhen
11 | * @date 2018/10/13
12 | */
13 | public class WeatherBean extends BaseBean {
14 |
15 | /**
16 | * yesterday : {"date":"12日星期五","high":"高温 22℃","fx":"东北风","low":"低温 13℃","fl":"","type":"多云"}
17 | * city : 西安
18 | * aqi : 81
19 | * forecast : [{"date":"13日星期六","high":"高温 19℃","fengli":"","low":"低温 11℃","fengxiang":"东风","type":"小雨"},{"date":"14日星期天","high":"高温 20℃","fengli":"","low":"低温 12℃","fengxiang":"东北风","type":"阴"},{"date":"15日星期一","high":"高温 21℃","fengli":"","low":"低温 10℃","fengxiang":"北风","type":"阴"},{"date":"16日星期二","high":"高温 20℃","fengli":"","low":"低温 11℃","fengxiang":"东北风","type":"阴"},{"date":"17日星期三","high":"高温 22℃","fengli":"","low":"低温 14℃","fengxiang":"东北风","type":"多云"}]
20 | * ganmao : 昼夜温差较大,较易发生感冒,请适当增减衣服。体质较弱的朋友请注意防护。
21 | * wendu : 15
22 | */
23 |
24 | private YesterdayBean yesterday;
25 | private String city;
26 | private String aqi;
27 | private String ganmao;
28 | private String wendu;
29 | private List forecast;
30 |
31 | public YesterdayBean getYesterday() {
32 | return yesterday;
33 | }
34 |
35 | public void setYesterday(YesterdayBean yesterday) {
36 | this.yesterday = yesterday;
37 | }
38 |
39 | public String getCity() {
40 | return city;
41 | }
42 |
43 | public void setCity(String city) {
44 | this.city = city;
45 | }
46 |
47 | public String getAqi() {
48 | return aqi;
49 | }
50 |
51 | public void setAqi(String aqi) {
52 | this.aqi = aqi;
53 | }
54 |
55 | public String getGanmao() {
56 | return ganmao;
57 | }
58 |
59 | public void setGanmao(String ganmao) {
60 | this.ganmao = ganmao;
61 | }
62 |
63 | public String getWendu() {
64 | return wendu;
65 | }
66 |
67 | public void setWendu(String wendu) {
68 | this.wendu = wendu;
69 | }
70 |
71 | public List getForecast() {
72 | return forecast;
73 | }
74 |
75 | public void setForecast(List forecast) {
76 | this.forecast = forecast;
77 | }
78 |
79 | public static class YesterdayBean {
80 | /**
81 | * date : 12日星期五
82 | * high : 高温 22℃
83 | * fx : 东北风
84 | * low : 低温 13℃
85 | * fl :
86 | * type : 多云
87 | */
88 |
89 | private String date;
90 | private String high;
91 | private String fx;
92 | private String low;
93 | private String fl;
94 | private String type;
95 |
96 | public String getDate() {
97 | return date;
98 | }
99 |
100 | public void setDate(String date) {
101 | this.date = date;
102 | }
103 |
104 | public String getHigh() {
105 | return high;
106 | }
107 |
108 | public void setHigh(String high) {
109 | this.high = high;
110 | }
111 |
112 | public String getFx() {
113 | return fx;
114 | }
115 |
116 | public void setFx(String fx) {
117 | this.fx = fx;
118 | }
119 |
120 | public String getLow() {
121 | return low;
122 | }
123 |
124 | public void setLow(String low) {
125 | this.low = low;
126 | }
127 |
128 | public String getFl() {
129 | return fl;
130 | }
131 |
132 | public void setFl(String fl) {
133 | this.fl = fl;
134 | }
135 |
136 | public String getType() {
137 | return type;
138 | }
139 |
140 | public void setType(String type) {
141 | this.type = type;
142 | }
143 | }
144 |
145 | public static class ForecastBean {
146 | /**
147 | * date : 13日星期六
148 | * high : 高温 19℃
149 | * fengli :
150 | * low : 低温 11℃
151 | * fengxiang : 东风
152 | * type : 小雨
153 | */
154 |
155 | private String date;
156 | private String high;
157 | private String fengli;
158 | private String low;
159 | private String fengxiang;
160 | private String type;
161 |
162 | public String getDate() {
163 | return date;
164 | }
165 |
166 | public void setDate(String date) {
167 | this.date = date;
168 | }
169 |
170 | public String getHigh() {
171 | return high;
172 | }
173 |
174 | public void setHigh(String high) {
175 | this.high = high;
176 | }
177 |
178 | public String getFengli() {
179 | return fengli;
180 | }
181 |
182 | public void setFengli(String fengli) {
183 | this.fengli = fengli;
184 | }
185 |
186 | public String getLow() {
187 | return low;
188 | }
189 |
190 | public void setLow(String low) {
191 | this.low = low;
192 | }
193 |
194 | public String getFengxiang() {
195 | return fengxiang;
196 | }
197 |
198 | public void setFengxiang(String fengxiang) {
199 | this.fengxiang = fengxiang;
200 | }
201 |
202 | public String getType() {
203 | return type;
204 | }
205 |
206 | public void setType(String type) {
207 | this.type = type;
208 | }
209 | }
210 | }
211 |
--------------------------------------------------------------------------------
/app/src/main/java/per/goweii/android/rxhttp/http/FreeApi.java:
--------------------------------------------------------------------------------
1 | package per.goweii.android.rxhttp.http;
2 |
3 | import io.reactivex.Observable;
4 | import per.goweii.android.rxhttp.bean.RecommendPoetryBean;
5 | import per.goweii.android.rxhttp.bean.SinglePoetryBean;
6 | import per.goweii.android.rxhttp.bean.WeatherBean;
7 | import per.goweii.rxhttp.request.Api;
8 | import per.goweii.rxhttp.request.base.BaseBean;
9 | import retrofit2.http.GET;
10 | import retrofit2.http.Headers;
11 | import retrofit2.http.Query;
12 |
13 | /**
14 | * @author Cuizhen
15 | * @date 2018/10/13
16 | */
17 | public class FreeApi extends Api {
18 |
19 | public static Service api() {
20 | return Api.api(Service.class);
21 | }
22 |
23 | public interface Code{
24 | int SUCCESS = 200;
25 | }
26 |
27 | public interface Config {
28 | String BASE_URL = "http://api.apiopen.top/";
29 |
30 | String BASE_URL_OTHER_NAME = "other";
31 | String BASE_URL_OTHER = "https://www.apiopen.top/";
32 |
33 | String BASE_URL_ERROR_NAME = "error";
34 | String BASE_URL_ERROR = "https://www.apiopen1.top/";
35 |
36 | String BASE_URL_HTTPS_NAME = "https";
37 | String BASE_URL_HTTPS = "https://www.baidu.com/";
38 | }
39 |
40 | public interface Service {
41 | /**
42 | * 随机单句诗词推荐
43 | */
44 | @Headers({Header.CACHE_ALIVE_SECOND + ":" + 10})
45 | @GET("singlePoetry")
46 | Observable> singlePoetry();
47 |
48 | /**
49 | * 随机一首诗词推荐
50 | */
51 | @Headers({Header.CACHE_ALIVE_SECOND + ":" + 0})
52 | @GET("recommendPoetry")
53 | Observable> recommendPoetry();
54 |
55 | /**
56 | * 获取天气
57 | */
58 | @Headers({Header.BASE_URL_REDIRECT + ":" + Config.BASE_URL_OTHER_NAME})
59 | @GET("weatherApi?")
60 | Observable> weather(@Query("city") String city);
61 |
62 | /**
63 | * 错误地址
64 | */
65 | @Headers({Header.BASE_URL_REDIRECT + ":" + Config.BASE_URL_ERROR_NAME})
66 | @GET("weatherApi")
67 | Observable> errorHost();
68 |
69 | /**
70 | * https
71 | */
72 | @Headers({Header.BASE_URL_REDIRECT + ":" + Config.BASE_URL_HTTPS_NAME})
73 | @GET("s")
74 | Observable> https(@Query("wd") String wd);
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/app/src/main/java/per/goweii/android/rxhttp/http/ResponseBean.java:
--------------------------------------------------------------------------------
1 | package per.goweii.android.rxhttp.http;
2 |
3 | import com.google.gson.annotations.SerializedName;
4 |
5 | import per.goweii.rxhttp.request.base.BaseResponse;
6 |
7 | /**
8 | * 描述:
9 | *
10 | * @author Cuizhen
11 | * @date 2018/10/13
12 | */
13 | public class ResponseBean implements BaseResponse {
14 |
15 | @SerializedName(value = "code", alternate = {"status"})
16 | private int code;
17 | @SerializedName(value = "data", alternate = {"result"})
18 | private E data;
19 | @SerializedName(value = "msg", alternate = {"message"})
20 | private String message;
21 |
22 | @Override
23 | public int getCode() {
24 | return code;
25 | }
26 |
27 | @Override
28 | public void setCode(int code) {
29 | this.code = code;
30 | }
31 |
32 | @Override
33 | public E getData() {
34 | return data;
35 | }
36 |
37 | @Override
38 | public void setData(E data) {
39 | this.data = data;
40 | }
41 |
42 | @Override
43 | public String getMsg() {
44 | return message;
45 | }
46 |
47 | @Override
48 | public void setMsg(String msg) {
49 | this.message = msg;
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
16 |
17 |
24 |
25 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_test_download.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
15 |
16 |
26 |
27 |
33 |
34 |
39 |
40 |
46 |
47 |
54 |
55 |
56 |
57 |
62 |
63 |
69 |
70 |
77 |
78 |
79 |
80 |
85 |
86 |
92 |
93 |
100 |
101 |
102 |
103 |
104 |
105 |
113 |
114 |
122 |
123 |
131 |
132 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_test_request.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
16 |
17 |
21 |
22 |
23 |
24 |
25 |
29 |
30 |
34 |
35 |
39 |
40 |
47 |
48 |
56 |
57 |
58 |
59 |
63 |
64 |
71 |
72 |
80 |
81 |
82 |
83 |
87 |
88 |
95 |
96 |
104 |
105 |
112 |
113 |
114 |
115 |
119 |
120 |
127 |
128 |
136 |
137 |
138 |
139 |
143 |
144 |
151 |
152 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/goweii/RxHttp/e50b8110a88cc4b3b38b47d7daaabf213ceea877/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/goweii/RxHttp/e50b8110a88cc4b3b38b47d7daaabf213ceea877/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/goweii/RxHttp/e50b8110a88cc4b3b38b47d7daaabf213ceea877/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/goweii/RxHttp/e50b8110a88cc4b3b38b47d7daaabf213ceea877/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/goweii/RxHttp/e50b8110a88cc4b3b38b47d7daaabf213ceea877/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/goweii/RxHttp/e50b8110a88cc4b3b38b47d7daaabf213ceea877/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/goweii/RxHttp/e50b8110a88cc4b3b38b47d7daaabf213ceea877/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/goweii/RxHttp/e50b8110a88cc4b3b38b47d7daaabf213ceea877/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/goweii/RxHttp/e50b8110a88cc4b3b38b47d7daaabf213ceea877/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/goweii/RxHttp/e50b8110a88cc4b3b38b47d7daaabf213ceea877/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #008577
4 | #00574B
5 | #D81B60
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | RxHttp
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 | repositories {
3 | google()
4 | jcenter()
5 | }
6 | dependencies {
7 | classpath "com.android.tools.build:gradle:4.1.1"
8 | }
9 | }
10 |
11 | allprojects {
12 | repositories {
13 | google()
14 | jcenter()
15 | }
16 | }
17 |
18 | task clean(type: Delete) {
19 | delete rootProject.buildDir
20 | }
21 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 |
15 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/goweii/RxHttp/e50b8110a88cc4b3b38b47d7daaabf213ceea877/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sun Feb 28 00:04:10 CST 2021
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/rxhttp/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/rxhttp/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 28
5 | buildToolsVersion '28.0.3'
6 | defaultConfig {
7 | minSdkVersion 14
8 | targetSdkVersion 28
9 | versionCode 1
10 | versionName "1.0"
11 |
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | compileOptions {
21 | sourceCompatibility JavaVersion.VERSION_1_7
22 | targetCompatibility JavaVersion.VERSION_1_7
23 | }
24 | }
25 |
26 | dependencies {
27 | implementation fileTree(include: ['*.jar'], dir: 'libs')
28 | implementation 'com.android.support:appcompat-v7:28.0.0'
29 |
30 | // Retrofit2
31 | api 'com.squareup.retrofit2:retrofit:2.4.0'
32 | api 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
33 | api 'com.squareup.retrofit2:converter-gson:2.4.0'
34 |
35 | // OkHttp
36 | api 'com.squareup.okhttp3:logging-interceptor:3.9.0'
37 | api 'com.squareup.okhttp3:okhttp:3.11.0'
38 | api 'com.squareup.okio:okio:1.14.0'
39 |
40 | // RxJava2
41 | api 'io.reactivex.rxjava2:rxjava:2.1.16'
42 | api 'io.reactivex.rxjava2:rxandroid:2.0.2'
43 | }
44 |
--------------------------------------------------------------------------------
/rxhttp/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/rxhttp/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/rxhttp/src/main/java/per/goweii/rxhttp/core/RxHttp.java:
--------------------------------------------------------------------------------
1 | package per.goweii.rxhttp.core;
2 |
3 | import android.annotation.SuppressLint;
4 | import android.content.Context;
5 | import android.support.annotation.NonNull;
6 |
7 | import io.reactivex.Observable;
8 | import per.goweii.rxhttp.core.exception.RxHttpUninitializedException;
9 | import per.goweii.rxhttp.download.DownloadInfo;
10 | import per.goweii.rxhttp.download.RxDownload;
11 | import per.goweii.rxhttp.download.setting.DefaultDownloadSetting;
12 | import per.goweii.rxhttp.download.setting.DownloadSetting;
13 | import per.goweii.rxhttp.request.RxRequest;
14 | import per.goweii.rxhttp.request.base.BaseResponse;
15 | import per.goweii.rxhttp.request.exception.NullRequestSettingException;
16 | import per.goweii.rxhttp.request.setting.RequestSetting;
17 |
18 | /**
19 | * 描述:
20 | *
21 | * @author Cuizhen
22 | * @date 2018/10/12
23 | */
24 | @SuppressLint("StaticFieldLeak")
25 | public class RxHttp {
26 |
27 | private static RxHttp INSTANCE = null;
28 |
29 | private final Context mAppContext;
30 | private RequestSetting mRequestSetting = null;
31 | private DownloadSetting mDownloadSetting = null;
32 |
33 | private RxHttp(@NonNull Context context) {
34 | mAppContext = context;
35 | }
36 |
37 | public static void init(@NonNull Context context) {
38 | INSTANCE = new RxHttp(context.getApplicationContext());
39 | }
40 |
41 | @NonNull
42 | public static RxHttp getInstance() {
43 | if (INSTANCE == null) {
44 | throw new RxHttpUninitializedException();
45 | }
46 | return INSTANCE;
47 | }
48 |
49 | public static void initRequest(@NonNull RequestSetting setting) {
50 | getInstance().mRequestSetting = setting;
51 | }
52 |
53 | public static void initDownload(@NonNull DownloadSetting setting) {
54 | getInstance().mDownloadSetting = setting;
55 | }
56 |
57 | @NonNull
58 | public static Context getAppContext() {
59 | return getInstance().mAppContext;
60 | }
61 |
62 | @NonNull
63 | public static RequestSetting getRequestSetting() {
64 | RequestSetting setting = getInstance().mRequestSetting;
65 | if (setting == null) {
66 | throw new NullRequestSettingException();
67 | }
68 | return setting;
69 | }
70 |
71 | @NonNull
72 | public static DownloadSetting getDownloadSetting() {
73 | DownloadSetting setting = getInstance().mDownloadSetting;
74 | if (setting == null) {
75 | setting = new DefaultDownloadSetting();
76 | }
77 | return setting;
78 | }
79 |
80 | public static > RxRequest request(@NonNull Observable observable) {
81 | return RxRequest.create(observable);
82 | }
83 |
84 | public static RxDownload download(@NonNull DownloadInfo info) {
85 | return RxDownload.create(info);
86 | }
87 | }
88 |
--------------------------------------------------------------------------------
/rxhttp/src/main/java/per/goweii/rxhttp/core/RxLife.java:
--------------------------------------------------------------------------------
1 | package per.goweii.rxhttp.core;
2 |
3 | import android.support.annotation.NonNull;
4 |
5 | import io.reactivex.disposables.CompositeDisposable;
6 | import io.reactivex.disposables.Disposable;
7 |
8 | /**
9 | * 描述:
10 | *
11 | * @author Cuizhen
12 | * @date 2018/10/13
13 | */
14 | public class RxLife {
15 |
16 | private CompositeDisposable mCompositeDisposable = null;
17 |
18 | private RxLife() {
19 | mCompositeDisposable = new CompositeDisposable();
20 | }
21 |
22 | @NonNull
23 | public static RxLife create() {
24 | return new RxLife();
25 | }
26 |
27 | public void destroy() {
28 | if (mCompositeDisposable == null || mCompositeDisposable.isDisposed()) {
29 | return;
30 | }
31 | mCompositeDisposable.dispose();
32 | }
33 |
34 | public void add(Disposable d) {
35 | if (mCompositeDisposable == null || mCompositeDisposable.isDisposed()) {
36 | mCompositeDisposable = new CompositeDisposable();
37 | }
38 | mCompositeDisposable.add(d);
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/rxhttp/src/main/java/per/goweii/rxhttp/core/exception/RxHttpUninitializedException.java:
--------------------------------------------------------------------------------
1 | package per.goweii.rxhttp.core.exception;
2 |
3 | import per.goweii.rxhttp.core.RxHttp;
4 |
5 | /**
6 | * 描述:在调用网络请求之前应该先进行初始化,建议在Application中初始化
7 | * {@link RxHttp#init(android.content.Context)}
8 | *
9 | * @author Cuizhen
10 | * @date 2018/10/12
11 | */
12 | public class RxHttpUninitializedException extends RuntimeException {
13 | public RxHttpUninitializedException() {
14 | super("RxHttp未初始化");
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/rxhttp/src/main/java/per/goweii/rxhttp/core/manager/BaseClientManager.java:
--------------------------------------------------------------------------------
1 | package per.goweii.rxhttp.core.manager;
2 |
3 | import android.support.annotation.NonNull;
4 |
5 | import retrofit2.Retrofit;
6 |
7 | /**
8 | * 用于管理Retrofit实例
9 | * 子类继承后自行判断是否采用单例模式
10 | *
11 | * @author Cuizhen
12 | * @date 2018/9/4
13 | */
14 | public abstract class BaseClientManager {
15 | @NonNull
16 | protected abstract Retrofit create();
17 | }
18 |
--------------------------------------------------------------------------------
/rxhttp/src/main/java/per/goweii/rxhttp/core/utils/BaseUrlUtils.java:
--------------------------------------------------------------------------------
1 | package per.goweii.rxhttp.core.utils;
2 |
3 | import android.support.annotation.NonNull;
4 |
5 | /**
6 | * 描述:检查BaseUrl是否以"/"结尾
7 | *
8 | * @author Cuizhen
9 | * @date 2018/10/13
10 | */
11 | public class BaseUrlUtils {
12 |
13 | public static String checkBaseUrl(@NonNull String url) {
14 | if (url.endsWith("/")) {
15 | return url;
16 | } else {
17 | return url + "/";
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/rxhttp/src/main/java/per/goweii/rxhttp/core/utils/SDCardUtils.java:
--------------------------------------------------------------------------------
1 | package per.goweii.rxhttp.core.utils;
2 |
3 | import android.os.Environment;
4 | import android.support.annotation.NonNull;
5 |
6 | import java.io.File;
7 |
8 | import per.goweii.rxhttp.core.RxHttp;
9 |
10 | /**
11 | * @author Cuizhen
12 | * @date 18/4/23
13 | */
14 | public class SDCardUtils {
15 |
16 | @NonNull
17 | public static String getCacheDir() {
18 | File cacheFile = null;
19 | if (isSDCardAlive()) {
20 | cacheFile = RxHttp.getAppContext().getExternalCacheDir();
21 | }
22 | if (cacheFile == null) {
23 | cacheFile = RxHttp.getAppContext().getCacheDir();
24 | }
25 | return cacheFile.getAbsolutePath();
26 | }
27 |
28 | @NonNull
29 | public static String getDownloadCacheDir() {
30 | File dir = null;
31 | if (isSDCardAlive()) {
32 | dir = RxHttp.getAppContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
33 | }
34 | if (dir == null) {
35 | dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
36 | }
37 | return dir.getAbsolutePath();
38 | }
39 |
40 | private static boolean isSDCardAlive() {
41 | return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
42 | }
43 |
44 | }
--------------------------------------------------------------------------------
/rxhttp/src/main/java/per/goweii/rxhttp/download/DownloadApi.java:
--------------------------------------------------------------------------------
1 | package per.goweii.rxhttp.download;
2 |
3 | import io.reactivex.Observable;
4 | import okhttp3.ResponseBody;
5 | import retrofit2.http.GET;
6 | import retrofit2.http.Header;
7 | import retrofit2.http.Streaming;
8 | import retrofit2.http.Url;
9 |
10 | /**
11 | * @author CuiZhen
12 | * @date 2018/10/14
13 | * QQ: 302833254
14 | * E-mail: goweii@163.com
15 | * GitHub: https://github.com/goweii
16 | */
17 | public interface DownloadApi{
18 | @Streaming
19 | @GET
20 | Observable download(@Header("RANGE") String range, @Url String url);
21 | }
22 |
--------------------------------------------------------------------------------
/rxhttp/src/main/java/per/goweii/rxhttp/download/DownloadClientManager.java:
--------------------------------------------------------------------------------
1 | package per.goweii.rxhttp.download;
2 |
3 | import android.support.annotation.NonNull;
4 |
5 | import java.util.concurrent.TimeUnit;
6 |
7 | import okhttp3.OkHttpClient;
8 | import per.goweii.rxhttp.core.RxHttp;
9 | import per.goweii.rxhttp.core.manager.BaseClientManager;
10 | import per.goweii.rxhttp.core.utils.BaseUrlUtils;
11 | import per.goweii.rxhttp.download.interceptor.RealNameInterceptor;
12 | import retrofit2.Retrofit;
13 | import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
14 |
15 | /**
16 | * 描述:
17 | *
18 | * @author Cuizhen
19 | * @date 2018/10/15
20 | */
21 | class DownloadClientManager extends BaseClientManager {
22 |
23 | private static DownloadClientManager INSTANCE = null;
24 | private final Retrofit mRetrofit;
25 |
26 | private DownloadClientManager() {
27 | mRetrofit = create();
28 | }
29 |
30 | /**
31 | * 采用单例模式
32 | *
33 | * @return RequestClientManager
34 | */
35 | @NonNull
36 | private static DownloadClientManager getInstance() {
37 | if (INSTANCE == null) {
38 | synchronized (DownloadClientManager.class) {
39 | if (INSTANCE == null) {
40 | INSTANCE = new DownloadClientManager();
41 | }
42 | }
43 | }
44 | return INSTANCE;
45 | }
46 |
47 | static DownloadApi getService() {
48 | return getInstance().mRetrofit.create(DownloadApi.class);
49 | }
50 |
51 | @NonNull
52 | @Override
53 | protected Retrofit create() {
54 | return new Retrofit.Builder()
55 | .client(createOkHttpClient())
56 | .baseUrl(BaseUrlUtils.checkBaseUrl(RxHttp.getDownloadSetting().getBaseUrl()))
57 | .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
58 | .build();
59 | }
60 |
61 | @NonNull
62 | private OkHttpClient createOkHttpClient(){
63 | OkHttpClient.Builder builder = new OkHttpClient.Builder();
64 | long timeout = RxHttp.getDownloadSetting().getTimeout();
65 | long connectTimeout = RxHttp.getDownloadSetting().getConnectTimeout();
66 | long readTimeout = RxHttp.getDownloadSetting().getReadTimeout();
67 | long writeTimeout = RxHttp.getDownloadSetting().getWriteTimeout();
68 | builder.connectTimeout(connectTimeout > 0 ? connectTimeout : timeout, TimeUnit.MILLISECONDS);
69 | builder.readTimeout(readTimeout > 0 ? readTimeout : timeout, TimeUnit.MILLISECONDS);
70 | builder.writeTimeout(writeTimeout > 0 ? writeTimeout : timeout, TimeUnit.MILLISECONDS);
71 | RealNameInterceptor.addTo(builder);
72 | return builder.build();
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/rxhttp/src/main/java/per/goweii/rxhttp/download/DownloadInfo.java:
--------------------------------------------------------------------------------
1 | package per.goweii.rxhttp.download;
2 |
3 | import android.support.annotation.IntRange;
4 | import android.support.annotation.NonNull;
5 | import android.support.annotation.Nullable;
6 |
7 | import per.goweii.rxhttp.core.RxHttp;
8 |
9 | /**
10 | * 描述:
11 | *
12 | * @author Cuizhen
13 | * @date 2018/10/15
14 | */
15 | public class DownloadInfo {
16 |
17 | @NonNull
18 | public String url;
19 | @Nullable
20 | public String saveDirPath;
21 | @Nullable
22 | public String saveFileName;
23 | @IntRange(from = 0)
24 | public long downloadLength;
25 | @IntRange(from = 0)
26 | public long contentLength;
27 | @NonNull
28 | public State state;
29 | @NonNull
30 | public Mode mode;
31 |
32 | private DownloadInfo(@NonNull String url, @Nullable String saveDirPath, @Nullable String saveFileName,
33 | @IntRange(from = 0) long downloadLength, @IntRange(from = 0) long contentLength) {
34 | this.url = url;
35 | this.saveDirPath = saveDirPath;
36 | this.saveFileName = saveFileName;
37 | this.downloadLength = downloadLength;
38 | this.contentLength = contentLength;
39 | this.state = State.STOPPED;
40 | this.mode = RxHttp.getDownloadSetting().getDefaultDownloadMode();
41 | }
42 |
43 | @NonNull
44 | public static DownloadInfo create(@NonNull String url){
45 | return create(url, null, null);
46 | }
47 |
48 | @NonNull
49 | public static DownloadInfo create(@NonNull String url,
50 | @Nullable String saveDirPath,
51 | @Nullable String saveFileName){
52 | return create(url, saveDirPath, saveFileName, 0, 0);
53 | }
54 |
55 | @NonNull
56 | public static DownloadInfo create(@NonNull String url,
57 | @Nullable String saveDirPath,
58 | @Nullable String saveFileName,
59 | @IntRange(from = 0) long downloadLength,
60 | @IntRange(from = 0) long contentLength){
61 | return new DownloadInfo(url, saveDirPath, saveFileName, downloadLength, contentLength);
62 | }
63 |
64 | /**
65 | * 如果路径文件存在,但是断点续传时未传入已下载长度信息,此时的写入模式
66 | */
67 | public enum Mode{
68 | /**
69 | * 追加
70 | */
71 | APPEND,
72 | /**
73 | * 替换
74 | */
75 | REPLACE,
76 | /**
77 | * 重命名
78 | */
79 | RENAME
80 | }
81 |
82 | public enum State{
83 | /**
84 | * 正在开始
85 | */
86 | STARTING,
87 | /**
88 | * 正在下载
89 | */
90 | DOWNLOADING,
91 | /**
92 | * 已停止
93 | */
94 | STOPPED,
95 | /**
96 | * 下载出错
97 | */
98 | ERROR,
99 | /**
100 | * 下载完成
101 | */
102 | COMPLETION
103 | }
104 | }
105 |
--------------------------------------------------------------------------------
/rxhttp/src/main/java/per/goweii/rxhttp/download/RxDownload.java:
--------------------------------------------------------------------------------
1 | package per.goweii.rxhttp.download;
2 |
3 | import android.net.Uri;
4 | import android.support.annotation.NonNull;
5 | import android.text.TextUtils;
6 |
7 | import java.io.File;
8 | import java.io.FileOutputStream;
9 | import java.io.IOException;
10 | import java.io.InputStream;
11 | import java.lang.reflect.Field;
12 | import java.util.concurrent.TimeUnit;
13 |
14 | import io.reactivex.Observable;
15 | import io.reactivex.ObservableEmitter;
16 | import io.reactivex.ObservableOnSubscribe;
17 | import io.reactivex.ObservableSource;
18 | import io.reactivex.Observer;
19 | import io.reactivex.android.schedulers.AndroidSchedulers;
20 | import io.reactivex.disposables.Disposable;
21 | import io.reactivex.functions.Action;
22 | import io.reactivex.functions.Consumer;
23 | import io.reactivex.functions.Function;
24 | import io.reactivex.schedulers.Schedulers;
25 | import okhttp3.ResponseBody;
26 | import per.goweii.rxhttp.download.exception.RangeLengthIsZeroException;
27 | import per.goweii.rxhttp.download.exception.SaveFileBrokenPointException;
28 | import per.goweii.rxhttp.download.exception.SaveFileDirMakeException;
29 | import per.goweii.rxhttp.download.exception.SaveFileWriteException;
30 | import per.goweii.rxhttp.download.interceptor.DownloadResponseBody;
31 | import per.goweii.rxhttp.download.utils.DownloadInfoChecker;
32 | import per.goweii.rxhttp.download.utils.RxNotify;
33 | import per.goweii.rxhttp.download.utils.UnitFormatUtils;
34 |
35 | /**
36 | * 描述:网络请求
37 | *
38 | * @author Cuizhen
39 | * @date 2018/9/9
40 | */
41 | public class RxDownload {
42 |
43 | private final DownloadInfo mInfo;
44 | private DownloadListener mDownloadListener = null;
45 | private ProgressListener mProgressListener = null;
46 | private SpeedListener mSpeedListener = null;
47 | private Disposable mDisposableDownload = null;
48 | private Disposable mDisposableSpeed = null;
49 |
50 | private RxDownload(@NonNull DownloadInfo info) {
51 | mInfo = info;
52 | }
53 |
54 | @NonNull
55 | public static RxDownload create(@NonNull DownloadInfo info) {
56 | return new RxDownload(info);
57 | }
58 |
59 | @NonNull
60 | public RxDownload setDownloadListener(@NonNull DownloadListener listener) {
61 | mDownloadListener = listener;
62 | return this;
63 | }
64 |
65 | @NonNull
66 | public RxDownload setProgressListener(@NonNull ProgressListener listener) {
67 | mProgressListener = listener;
68 | return this;
69 | }
70 |
71 | @NonNull
72 | public RxDownload setSpeedListener(@NonNull SpeedListener listener) {
73 | mSpeedListener = listener;
74 | return this;
75 | }
76 |
77 | @NonNull
78 | public DownloadInfo getDownloadInfo() {
79 | return mInfo;
80 | }
81 |
82 | public void start() {
83 | if (mDisposableDownload != null && !mDisposableDownload.isDisposed()) {
84 | return;
85 | }
86 | Observable.create(new ObservableOnSubscribe() {
87 | @Override
88 | public void subscribe(@NonNull ObservableEmitter emitter) throws Exception {
89 | DownloadInfoChecker.checkDownloadLength(mInfo);
90 | DownloadInfoChecker.checkContentLength(mInfo);
91 | emitter.onNext("bytes=" + mInfo.downloadLength + "-" + (mInfo.contentLength == 0 ? "" : mInfo.contentLength));
92 | }
93 | }).subscribeOn(Schedulers.io()).observeOn(Schedulers.io()).flatMap(new Function>() {
94 | @Override
95 | public ObservableSource apply(@NonNull String range) throws Exception {
96 | return DownloadClientManager.getService().download(range, mInfo.url);
97 | }
98 | }).doOnNext(new Consumer() {
99 | @Override
100 | public void accept(ResponseBody responseBody) throws Exception {
101 | if (mInfo.contentLength == 0) {
102 | mInfo.contentLength = mInfo.downloadLength + responseBody.contentLength();
103 | } else if (mInfo.downloadLength + responseBody.contentLength() != mInfo.contentLength) {
104 | throw new SaveFileBrokenPointException();
105 | }
106 | DownloadInfoChecker.checkDirPath(mInfo);
107 | if (TextUtils.isEmpty(mInfo.saveFileName)) {
108 | try {
109 | Class> clazz = responseBody.getClass();
110 | Field field = clazz.getDeclaredField("delegate");
111 | field.setAccessible(true);
112 | DownloadResponseBody body = (DownloadResponseBody) field.get(responseBody);
113 | mInfo.saveFileName = body.getRealName();
114 | } catch (Throwable ignore) {
115 | }
116 | }
117 | if (TextUtils.isEmpty(mInfo.saveFileName)) {
118 | try {
119 | String filename = null;
120 | Uri uri = Uri.parse(mInfo.url);
121 | for (String name : uri.getQueryParameterNames()) {
122 | if (name.contains("name")) {
123 | filename = uri.getQueryParameter(name);
124 | }
125 | }
126 | if (TextUtils.isEmpty(filename)) {
127 | filename = uri.getLastPathSegment();
128 | }
129 | mInfo.saveFileName = filename;
130 | } catch (Throwable ignore) {
131 | }
132 | }
133 | DownloadInfoChecker.checkFileName(mInfo);
134 | mInfo.state = DownloadInfo.State.DOWNLOADING;
135 | notifyDownloading();
136 | write(responseBody.byteStream(), createSaveFile(mInfo));
137 | }
138 | }).observeOn(AndroidSchedulers.mainThread()).doOnDispose(new Action() {
139 | @Override
140 | public void run() throws Exception {
141 | cancelSpeedObserver();
142 | }
143 | }).subscribe(new Observer() {
144 | @Override
145 | public void onSubscribe(@NonNull Disposable d) {
146 | mDisposableDownload = d;
147 | mInfo.state = DownloadInfo.State.STARTING;
148 | if (mDownloadListener != null) {
149 | mDownloadListener.onStarting(mInfo);
150 | }
151 | }
152 |
153 | @Override
154 | public void onNext(@NonNull ResponseBody responseBody) {
155 | mInfo.state = DownloadInfo.State.COMPLETION;
156 | if (mDownloadListener != null) {
157 | mDownloadListener.onCompletion(mInfo);
158 | }
159 | }
160 |
161 | @Override
162 | public void onError(@NonNull Throwable e) {
163 | if (e instanceof RangeLengthIsZeroException) {
164 | mInfo.state = DownloadInfo.State.COMPLETION;
165 | if (mDownloadListener != null) {
166 | mDownloadListener.onCompletion(mInfo);
167 | }
168 | } else {
169 | mInfo.state = DownloadInfo.State.ERROR;
170 | if (mDownloadListener != null) {
171 | mDownloadListener.onError(mInfo, e);
172 | }
173 | }
174 | }
175 |
176 | @Override
177 | public void onComplete() {
178 | }
179 | });
180 | }
181 |
182 | public void stop() {
183 | if (mDisposableDownload != null && !mDisposableDownload.isDisposed()) {
184 | mDisposableDownload.dispose();
185 | mDisposableDownload = null;
186 | }
187 | mInfo.state = DownloadInfo.State.STOPPED;
188 | if (mDownloadListener != null) {
189 | mDownloadListener.onStopped(mInfo);
190 | }
191 | }
192 |
193 | public void cancel() {
194 | if (mDisposableDownload != null && !mDisposableDownload.isDisposed()) {
195 | mDisposableDownload.dispose();
196 | mDisposableDownload = null;
197 | }
198 | deleteSaveFile(mInfo);
199 | mInfo.state = DownloadInfo.State.STOPPED;
200 | if (mDownloadListener != null) {
201 | mDownloadListener.onCanceled(mInfo);
202 | }
203 | }
204 |
205 | private File createSaveFile(DownloadInfo info) throws SaveFileDirMakeException {
206 | File file = new File(info.saveDirPath, info.saveFileName);
207 | if (!file.getParentFile().exists()) {
208 | if (!file.getParentFile().mkdirs()) {
209 | throw new SaveFileDirMakeException();
210 | }
211 | }
212 | return file;
213 | }
214 |
215 | private void deleteSaveFile(DownloadInfo info) {
216 | try {
217 | if (new File(info.saveDirPath, info.saveFileName).delete()) {
218 | mInfo.downloadLength = 0;
219 | }
220 | } catch (Exception ignore) {
221 | }
222 | }
223 |
224 | private void write(InputStream is, File file) throws SaveFileWriteException {
225 | createSpeedObserver();
226 | FileOutputStream fos = null;
227 | try {
228 | fos = new FileOutputStream(file, true);
229 | byte[] buffer = new byte[2048];
230 | int len;
231 | while ((len = is.read(buffer)) != -1) {
232 | fos.write(buffer, 0, len);
233 | mInfo.downloadLength += len;
234 | notifyProgress();
235 | }
236 | fos.flush();
237 | } catch (IOException e) {
238 | throw new SaveFileWriteException();
239 | } finally {
240 | if (is != null) {
241 | try {
242 | is.close();
243 | } catch (IOException e) {
244 | e.printStackTrace();
245 | }
246 | }
247 | if (fos != null) {
248 | try {
249 | fos.close();
250 | } catch (IOException e) {
251 | e.printStackTrace();
252 | }
253 | }
254 | }
255 | }
256 |
257 | private void notifyDownloading() {
258 | if (mDownloadListener != null) {
259 | RxNotify.runOnUiThread(new RxNotify.Action() {
260 | @Override
261 | public void run() {
262 | mDownloadListener.onDownloading(mInfo);
263 | }
264 | });
265 | }
266 | }
267 |
268 | private void notifyProgress() {
269 | if (mProgressListener != null) {
270 | RxNotify.runOnUiThread(new RxNotify.Action() {
271 | @Override
272 | public void run() {
273 | float progress = (float) mInfo.downloadLength / (float) mInfo.contentLength;
274 | mProgressListener.onProgress(progress, mInfo.downloadLength, mInfo.contentLength);
275 | }
276 | });
277 | }
278 | }
279 |
280 | private void createSpeedObserver() {
281 | if (mDisposableSpeed != null && !mDisposableSpeed.isDisposed()) {
282 | return;
283 | }
284 | mDisposableSpeed = Observable.interval(1, 1, TimeUnit.SECONDS)
285 | .subscribeOn(Schedulers.computation())
286 | .map(new Function() {
287 | private long lastDownloadLength = 0;
288 |
289 | @Override
290 | public Float apply(@NonNull Long ms) throws Exception {
291 | float bytesPerSecond = UnitFormatUtils.calculateSpeed(mInfo.downloadLength - lastDownloadLength, 1);
292 | lastDownloadLength = mInfo.downloadLength;
293 | return bytesPerSecond;
294 | }
295 | })
296 | .observeOn(AndroidSchedulers.mainThread())
297 | .subscribe(new Consumer() {
298 | @Override
299 | public void accept(Float speedPerSecond) throws Exception {
300 | if (mSpeedListener != null) {
301 | mSpeedListener.onSpeedChange(speedPerSecond, UnitFormatUtils.formatSpeedPerSecond(speedPerSecond));
302 | }
303 | }
304 | });
305 | }
306 |
307 | private void cancelSpeedObserver() {
308 | if (mDisposableSpeed != null && !mDisposableSpeed.isDisposed()) {
309 | mDisposableSpeed.dispose();
310 | }
311 | mDisposableSpeed = null;
312 | }
313 |
314 | public interface DownloadListener {
315 | void onStarting(DownloadInfo info);
316 |
317 | void onDownloading(DownloadInfo info);
318 |
319 | void onStopped(DownloadInfo info);
320 |
321 | void onCanceled(DownloadInfo info);
322 |
323 | void onCompletion(DownloadInfo info);
324 |
325 | void onError(DownloadInfo info, Throwable e);
326 | }
327 |
328 | public interface ProgressListener {
329 | void onProgress(float progress, long downloadLength, long contentLength);
330 | }
331 |
332 | public interface SpeedListener {
333 | void onSpeedChange(float bytesPerSecond, String speedFormat);
334 | }
335 | }
336 |
--------------------------------------------------------------------------------
/rxhttp/src/main/java/per/goweii/rxhttp/download/exception/RangeLengthIsZeroException.java:
--------------------------------------------------------------------------------
1 | package per.goweii.rxhttp.download.exception;
2 |
3 | /**
4 | * @author Cuizhen
5 | * @date 2018/10/12
6 | */
7 | public class RangeLengthIsZeroException extends RuntimeException {
8 | public RangeLengthIsZeroException() {
9 | super("断点处请求长度为0");
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/rxhttp/src/main/java/per/goweii/rxhttp/download/exception/SaveFileBrokenPointException.java:
--------------------------------------------------------------------------------
1 | package per.goweii.rxhttp.download.exception;
2 |
3 | /**
4 | * @author Cuizhen
5 | * @date 2018/10/12
6 | */
7 | public class SaveFileBrokenPointException extends RuntimeException {
8 | public SaveFileBrokenPointException() {
9 | super("文件已下载部分与断点续传不符");
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/rxhttp/src/main/java/per/goweii/rxhttp/download/exception/SaveFileDirMakeException.java:
--------------------------------------------------------------------------------
1 | package per.goweii.rxhttp.download.exception;
2 |
3 | /**
4 | * @author Cuizhen
5 | * @date 2018/10/12
6 | */
7 | public class SaveFileDirMakeException extends RuntimeException {
8 | public SaveFileDirMakeException() {
9 | super("下载保存的文件父文件夹创建失败");
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/rxhttp/src/main/java/per/goweii/rxhttp/download/exception/SaveFileWriteException.java:
--------------------------------------------------------------------------------
1 | package per.goweii.rxhttp.download.exception;
2 |
3 | /**
4 | * @author Cuizhen
5 | * @date 2018/10/12
6 | */
7 | public class SaveFileWriteException extends RuntimeException {
8 | public SaveFileWriteException() {
9 | super("下载保存的文件写入失败");
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/rxhttp/src/main/java/per/goweii/rxhttp/download/interceptor/DownloadResponseBody.java:
--------------------------------------------------------------------------------
1 | package per.goweii.rxhttp.download.interceptor;
2 |
3 | import android.support.annotation.NonNull;
4 | import android.support.annotation.Nullable;
5 |
6 | import java.io.IOException;
7 |
8 | import okhttp3.MediaType;
9 | import okhttp3.ResponseBody;
10 | import okio.Buffer;
11 | import okio.BufferedSource;
12 | import okio.ForwardingSource;
13 | import okio.Okio;
14 | import okio.Source;
15 |
16 | /**
17 | * 描述:
18 | *
19 | * @author Cuizhen
20 | * @date 2018/10/21
21 | */
22 | public class DownloadResponseBody extends ResponseBody {
23 |
24 | private final ResponseBody responseBody;
25 | private BufferedSource source = null;
26 | private String realName = null;
27 |
28 | public DownloadResponseBody(ResponseBody responseBody) {
29 | this.responseBody = responseBody;
30 | }
31 |
32 | @Nullable
33 | public String getRealName() {
34 | return realName;
35 | }
36 |
37 | public void setRealName(@Nullable String realName) {
38 | this.realName = realName;
39 | }
40 |
41 | @Nullable
42 | @Override
43 | public MediaType contentType() {
44 | return responseBody.contentType();
45 | }
46 |
47 | @Override
48 | public long contentLength() {
49 | return responseBody.contentLength();
50 | }
51 |
52 | @NonNull
53 | @Override
54 | public BufferedSource source() {
55 | if (source == null) {
56 | source = Okio.buffer(source(responseBody.source()));
57 | }
58 | return source;
59 | }
60 |
61 | /**
62 | * 读取,回调进度接口
63 | */
64 | private Source source(Source source) {
65 | return new ForwardingSource(source) {
66 | @Override
67 | public long read(Buffer sink, long byteCount) throws IOException {
68 | return super.read(sink, byteCount);
69 | }
70 | };
71 | }
72 | }
--------------------------------------------------------------------------------
/rxhttp/src/main/java/per/goweii/rxhttp/download/interceptor/RealNameInterceptor.java:
--------------------------------------------------------------------------------
1 | package per.goweii.rxhttp.download.interceptor;
2 |
3 | import android.support.annotation.NonNull;
4 | import android.support.annotation.Nullable;
5 | import android.text.TextUtils;
6 |
7 | import java.io.IOException;
8 | import java.util.HashMap;
9 | import java.util.Map;
10 |
11 | import okhttp3.Interceptor;
12 | import okhttp3.OkHttpClient;
13 | import okhttp3.Response;
14 |
15 | /**
16 | * @author Cuizhen
17 | * @date 2018/10/18
18 | */
19 | public class RealNameInterceptor implements Interceptor {
20 |
21 | public static void addTo(@NonNull OkHttpClient.Builder builder) {
22 | builder.addInterceptor(new RealNameInterceptor());
23 | }
24 |
25 | @Override
26 | public Response intercept(Chain chain) throws IOException {
27 | Response response = chain.proceed(chain.request());
28 | String disposition = response.header("Content-Disposition");
29 | String realName = parseRealName(disposition);
30 | DownloadResponseBody responseBody = new DownloadResponseBody(response.body());
31 | responseBody.setRealName(realName);
32 | return response.newBuilder()
33 | .body(responseBody)
34 | .build();
35 | }
36 |
37 | private String parseRealName(@Nullable String disposition) {
38 | if (disposition == null || disposition.isEmpty()) {
39 | return null;
40 | }
41 | String[] parts = disposition.split(";");
42 | if (parts.length == 0) {
43 | return null;
44 | }
45 | Map pairs = new HashMap<>(parts.length);
46 | for (String part : parts) {
47 | String s = part.trim();
48 | int i = s.indexOf("=");
49 | if (i == -1) {
50 | continue;
51 | }
52 | String k = s.substring(0, i);
53 | if (TextUtils.isEmpty(k)) {
54 | continue;
55 | }
56 | String v = s.substring(i + 1);
57 | if (TextUtils.isEmpty(v)) {
58 | continue;
59 | }
60 | pairs.put(k.trim(), v.trim());
61 | }
62 | if (pairs.isEmpty()) {
63 | return null;
64 | }
65 | String filename = pairs.get("filename*");
66 | if (filename == null || filename.isEmpty()) {
67 | filename = pairs.get("filename");
68 | }
69 | if (filename == null || filename.isEmpty()) {
70 | filename = pairs.get("name");
71 | }
72 | if (filename == null || filename.isEmpty()) {
73 | return null;
74 | }
75 | if (filename.startsWith("\"") && filename.endsWith("\"")) {
76 | filename = filename.substring(1, filename.length() - 1);
77 | }
78 | filename = filename.replace("UTF-8", "");
79 | return filename;
80 | }
81 | }
--------------------------------------------------------------------------------
/rxhttp/src/main/java/per/goweii/rxhttp/download/setting/DefaultDownloadSetting.java:
--------------------------------------------------------------------------------
1 | package per.goweii.rxhttp.download.setting;
2 |
3 | import android.support.annotation.NonNull;
4 | import android.support.annotation.Nullable;
5 |
6 | import per.goweii.rxhttp.download.DownloadInfo;
7 |
8 | /**
9 | * 描述:
10 | *
11 | * @author Cuizhen
12 | * @date 2018/10/16
13 | */
14 | public class DefaultDownloadSetting implements DownloadSetting {
15 |
16 | @NonNull
17 | @Override
18 | public String getBaseUrl() {
19 | return "http://api.rxhttp.download/";
20 | }
21 |
22 | @Override
23 | public long getTimeout() {
24 | return 60000;
25 | }
26 |
27 | @Override
28 | public long getConnectTimeout() {
29 | return 0;
30 | }
31 |
32 | @Override
33 | public long getReadTimeout() {
34 | return 0;
35 | }
36 |
37 | @Override
38 | public long getWriteTimeout() {
39 | return 0;
40 | }
41 |
42 | @Nullable
43 | @Override
44 | public String getSaveDirPath() {
45 | return null;
46 | }
47 |
48 | @NonNull
49 | @Override
50 | public DownloadInfo.Mode getDefaultDownloadMode() {
51 | return DownloadInfo.Mode.APPEND;
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/rxhttp/src/main/java/per/goweii/rxhttp/download/setting/DownloadSetting.java:
--------------------------------------------------------------------------------
1 | package per.goweii.rxhttp.download.setting;
2 |
3 | import android.support.annotation.IntRange;
4 | import android.support.annotation.NonNull;
5 | import android.support.annotation.Nullable;
6 |
7 | import per.goweii.rxhttp.download.DownloadInfo;
8 |
9 | /**
10 | * 描述:
11 | *
12 | * @author Cuizhen
13 | * @date 2018/10/15
14 | */
15 | public interface DownloadSetting {
16 |
17 | @NonNull
18 | String getBaseUrl();
19 |
20 | /**
21 | * 获取默认超时时长,单位为毫秒数
22 | */
23 | @IntRange(from = 1)
24 | long getTimeout();
25 |
26 | /**
27 | * 获取Connect超时时长,单位为毫秒数
28 | * 返回0则去getTimeout
29 | */
30 | @IntRange(from = 0)
31 | long getConnectTimeout();
32 |
33 | /**
34 | * 获取Read超时时长,单位为毫秒数
35 | * 返回0则去getTimeout
36 | */
37 | @IntRange(from = 0)
38 | long getReadTimeout();
39 |
40 | /**
41 | * 获取Write超时时长,单位为毫秒数
42 | * 返回0则去getTimeout
43 | */
44 | @IntRange(from = 0)
45 | long getWriteTimeout();
46 |
47 | @Nullable
48 | String getSaveDirPath();
49 |
50 | @NonNull
51 | DownloadInfo.Mode getDefaultDownloadMode();
52 | }
53 |
--------------------------------------------------------------------------------
/rxhttp/src/main/java/per/goweii/rxhttp/download/utils/DownloadInfoChecker.java:
--------------------------------------------------------------------------------
1 | package per.goweii.rxhttp.download.utils;
2 |
3 | import android.text.TextUtils;
4 |
5 | import java.io.File;
6 |
7 | import per.goweii.rxhttp.core.RxHttp;
8 | import per.goweii.rxhttp.core.utils.SDCardUtils;
9 | import per.goweii.rxhttp.download.DownloadInfo;
10 | import per.goweii.rxhttp.download.exception.RangeLengthIsZeroException;
11 | import per.goweii.rxhttp.download.exception.SaveFileBrokenPointException;
12 |
13 | /**
14 | * 描述:
15 | *
16 | * @author Cuizhen
17 | * @date 2018/10/19
18 | */
19 | public class DownloadInfoChecker {
20 |
21 | public static void checkDownloadLength(DownloadInfo info) throws SaveFileBrokenPointException{
22 | if (info.downloadLength == 0){
23 | File file = createFile(info.saveDirPath, info.saveFileName);
24 | if (file != null && file.exists()) {
25 | if (info.mode == DownloadInfo.Mode.APPEND) {
26 | info.downloadLength = file.length();
27 | } else if (info.mode == DownloadInfo.Mode.REPLACE) {
28 | //noinspection ResultOfMethodCallIgnored
29 | file.delete();
30 | } else {
31 | assert info.saveFileName != null;
32 | info.saveFileName = renameFileName(info.saveFileName);
33 | }
34 | }
35 | } else {
36 | File file = createFile(info.saveDirPath, info.saveFileName);
37 | if (file != null && file.exists()) {
38 | if (info.downloadLength != file.length()) {
39 | throw new SaveFileBrokenPointException();
40 | }
41 | } else {
42 | info.downloadLength = 0;
43 | }
44 | }
45 | }
46 |
47 | public static void checkContentLength(DownloadInfo info) throws RangeLengthIsZeroException{
48 | //noinspection ConditionCoveredByFurtherCondition
49 | if (info.downloadLength > 0 && info.contentLength > 0 && info.contentLength <= info.downloadLength) {
50 | throw new RangeLengthIsZeroException();
51 | }
52 | }
53 |
54 | private static String renameFileName(String fileName){
55 | String nameLeft;
56 | String nameDivide;
57 | String nameRight;
58 | int index = fileName.lastIndexOf(".");
59 | if (index >= 0) {
60 | nameLeft = fileName.substring(0, index);
61 | nameDivide = ".";
62 | nameRight = fileName.substring(index + 1);
63 | } else {
64 | nameLeft = fileName;
65 | nameDivide = "";
66 | nameRight = "";
67 | }
68 | int k1 = nameLeft.lastIndexOf("(");
69 | int k2 = nameLeft.lastIndexOf(")");
70 | int i = 1;
71 | if (k2 + 1 == nameLeft.length() && k1 >= 0 && k2 >= 0 && k2 > k1) {
72 | String num = nameLeft.substring(k1 + 1, k2);
73 | nameLeft = nameLeft.substring(0, k1);
74 | try {
75 | i = Integer.parseInt(num);
76 | i += 1;
77 | } catch (NumberFormatException ignore){
78 | }
79 | }
80 | return nameLeft + "(" + i + ")" + nameDivide + nameRight;
81 | }
82 |
83 | private static File createFile(String dirPath, String fileName){
84 | if (TextUtils.isEmpty(dirPath) || TextUtils.isEmpty(fileName)) {
85 | return null;
86 | }
87 | return new File(dirPath, fileName);
88 | }
89 |
90 | public static void checkDirPath(DownloadInfo info){
91 | if (TextUtils.isEmpty(info.saveDirPath)) {
92 | info.saveDirPath = RxHttp.getDownloadSetting().getSaveDirPath();
93 | }
94 | if (TextUtils.isEmpty(info.saveDirPath)) {
95 | info.saveDirPath = SDCardUtils.getDownloadCacheDir();
96 | }
97 | }
98 |
99 | public static void checkFileName(DownloadInfo info){
100 | if (TextUtils.isEmpty(info.saveFileName)) {
101 | info.saveFileName = System.currentTimeMillis() + ".rxdownload";
102 | }
103 | }
104 |
105 | }
106 |
--------------------------------------------------------------------------------
/rxhttp/src/main/java/per/goweii/rxhttp/download/utils/RxNotify.java:
--------------------------------------------------------------------------------
1 | package per.goweii.rxhttp.download.utils;
2 |
3 | import android.support.annotation.NonNull;
4 |
5 | import io.reactivex.Observable;
6 | import io.reactivex.Observer;
7 | import io.reactivex.android.schedulers.AndroidSchedulers;
8 | import io.reactivex.disposables.Disposable;
9 | import io.reactivex.schedulers.Schedulers;
10 |
11 | /**
12 | * 描述:
13 | *
14 | * @author Cuizhen
15 | * @date 2018/10/19
16 | */
17 | public class RxNotify {
18 |
19 | public static void runOnUiThread(@NonNull final Action action){
20 | Observable.empty()
21 | .subscribeOn(AndroidSchedulers.mainThread())
22 | .observeOn(AndroidSchedulers.mainThread())
23 | .subscribe(new Observer