Skip to content

Commit 14a8458

Browse files
committed
Support Redirected
1 parent bef478b commit 14a8458

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

README-zh.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* 低耦合,实际请求端和进度接收端并不存在直接或间接的关联关系,即可以在 **App** 任何地方接收进度信息.
2525
* 侵入性低,使用本框架你并不需要更改之前进行上传或下载的代码,即使用或不使用本框架并不会影响到原有的代码.
2626
* 多端同步,同一个数据源的上传或下载进度可以指定多个不同的接收端,少去了使用 **EventBus** 实现多个端口同步更新进度.
27+
* 支持多文件上传
2728
* 自动管理监听器,少去了手动注销监听器的烦恼.
2829
* 默认运行在主线层,少去了切换线程的烦恼.
2930
* 轻量级框架,不包含任何三方库,体积极小.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* Low coupling, the actual request and the progress of the receiver does not exist directly or indirectly, that can be anywhere in **App** to receive progress information.
2424
* Low intrusion, use this framework you do not need to change the code before uploading or downloading, ie using or not using this framework does not affect the original code.
2525
* Multi-end synchronization, the same data source upload or download progress can specify a number of different receivers, less to use **EventBus** achieve multiple port synchronization update progress.
26+
* Support multi-file upload
2627
* Automatic management of the listener, less to manually cancel the trouble of the listener.
2728
* The default run in the main line layer, less to switch the thread of trouble.
2829
* Lightweight framework, does not contain any three-party library, very small size.

progress/src/main/java/me/jessyan/progressmanager/ProgressManager.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.os.Handler;
44
import android.os.Looper;
5+
import android.text.TextUtils;
56

67
import java.io.IOException;
78
import java.util.LinkedList;
@@ -22,7 +23,6 @@
2223
* 基于 Okhttp Interceptor,所以使用前请确保你使用 Okhttp 或 Retrofit 进行网络请求
2324
* 实现原理类似 EventBus,你可在 App 中的任何地方,将多个监听器,以 Url 地址作为标识符,注册到本管理器
2425
* 当此 Url 地址存在下载或者上传的动作时,管理器会主动调用所有使用此 Url 地址注册过的监听器,达到多个模块的同步更新
25-
* 因为是通过 Url 作为唯一标识符,所以如果出现请求被重定向其他页面进行上传或者下载,那么就会出现获取不到进度的情况
2626
* Created by jess on 02/06/2017 18:37
2727
* Contact with jess.yan.effort@gmail.com
2828
*/
@@ -182,6 +182,12 @@ public Response wrapResponseBody(Response response) {
182182
if (response == null || response.body() == null)
183183
return response;
184184

185+
if (haveRedirect(response)) {
186+
resolveRedirect(mRequestListeners, response);
187+
resolveRedirect(mResponseListeners, response);
188+
return response;
189+
}
190+
185191
String key = response.request().url().toString();
186192
if (mResponseListeners.containsKey(key)) {
187193
List<ProgressListener> listeners = mResponseListeners.get(key);
@@ -192,6 +198,37 @@ public Response wrapResponseBody(Response response) {
192198
return response;
193199
}
194200

201+
/**
202+
* 是否需要重定向
203+
*
204+
* @param response
205+
* @return
206+
*/
207+
private boolean haveRedirect(Response response) {
208+
String status = response.header("Status");
209+
if (status.contains("301") || status.contains("302") || status.contains("303") || status.contains("307")) {
210+
return true;
211+
}
212+
return false;
213+
}
214+
215+
/**
216+
* 使重定向后,也可以监听进度
217+
*
218+
* @param map
219+
* @param response
220+
*/
221+
private void resolveRedirect(Map<String, List<ProgressListener>> map, Response response) {
222+
String url = response.request().url().toString();
223+
List<ProgressListener> progressListeners = map.get(url); //查看此重定向 url ,是否已经注册过监听器
224+
if (progressListeners != null) {
225+
String location = response.header("Location");// 重定向地址
226+
if (!TextUtils.isEmpty(location) && !map.containsKey(location)) {
227+
map.put(location, progressListeners); //将需要重定向地址的监听器,提供给重定向地址,保证重定向后也可以监听进度
228+
}
229+
}
230+
}
231+
195232

196233
private void forEachListenersOnError(Map<String, List<ProgressListener>> map, String url, Exception e) {
197234
if (map.containsKey(url)) {

0 commit comments

Comments
 (0)