Skip to content

Commit

Permalink
Support Redirected
Browse files Browse the repository at this point in the history
  • Loading branch information
JessYanCoding committed Jul 14, 2017
1 parent bef478b commit 14a8458
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* 低耦合,实际请求端和进度接收端并不存在直接或间接的关联关系,即可以在 **App** 任何地方接收进度信息.
* 侵入性低,使用本框架你并不需要更改之前进行上传或下载的代码,即使用或不使用本框架并不会影响到原有的代码.
* 多端同步,同一个数据源的上传或下载进度可以指定多个不同的接收端,少去了使用 **EventBus** 实现多个端口同步更新进度.
* 支持多文件上传
* 自动管理监听器,少去了手动注销监听器的烦恼.
* 默认运行在主线层,少去了切换线程的烦恼.
* 轻量级框架,不包含任何三方库,体积极小.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* 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.
* 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.
* 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.
* Support multi-file upload
* Automatic management of the listener, less to manually cancel the trouble of the listener.
* The default run in the main line layer, less to switch the thread of trouble.
* Lightweight framework, does not contain any three-party library, very small size.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;

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

if (haveRedirect(response)) {
resolveRedirect(mRequestListeners, response);
resolveRedirect(mResponseListeners, response);
return response;
}

String key = response.request().url().toString();
if (mResponseListeners.containsKey(key)) {
List<ProgressListener> listeners = mResponseListeners.get(key);
Expand All @@ -192,6 +198,37 @@ public Response wrapResponseBody(Response response) {
return response;
}

/**
* 是否需要重定向
*
* @param response
* @return
*/
private boolean haveRedirect(Response response) {
String status = response.header("Status");
if (status.contains("301") || status.contains("302") || status.contains("303") || status.contains("307")) {
return true;
}
return false;
}

/**
* 使重定向后,也可以监听进度
*
* @param map
* @param response
*/
private void resolveRedirect(Map<String, List<ProgressListener>> map, Response response) {
String url = response.request().url().toString();
List<ProgressListener> progressListeners = map.get(url); //查看此重定向 url ,是否已经注册过监听器
if (progressListeners != null) {
String location = response.header("Location");// 重定向地址
if (!TextUtils.isEmpty(location) && !map.containsKey(location)) {
map.put(location, progressListeners); //将需要重定向地址的监听器,提供给重定向地址,保证重定向后也可以监听进度
}
}
}


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

0 comments on commit 14a8458

Please sign in to comment.