Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DiDi
DoraemonKit
提交
5054599b
D
DoraemonKit
项目概览
DiDi
/
DoraemonKit
12 个月 前同步成功
通知
166
Star
19623
Fork
3062
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
DoraemonKit
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
5054599b
编写于
2月 26, 2020
作者:
J
jackjintai
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
modify:
1、网络拦截器处理
上级
d158b42b
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
76 addition
and
25 deletion
+76
-25
Android/app/src/debug/java/com/didichuxing/doraemondemo/MainDebugActivity.java
.../java/com/didichuxing/doraemondemo/MainDebugActivity.java
+22
-18
Android/app/src/main/AndroidManifest.xml
Android/app/src/main/AndroidManifest.xml
+4
-1
Android/doraemonkit-plugin/src/main/groovy/com/didichuxing/doraemonkit/plugin/bytecode/method/OkHttpMethodAdapter.java
...raemonkit/plugin/bytecode/method/OkHttpMethodAdapter.java
+8
-2
Android/doraemonkit/src/main/java/com/didichuxing/doraemonkit/aop/OkHttpHook.java
...main/java/com/didichuxing/doraemonkit/aop/OkHttpHook.java
+19
-1
Android/doraemonkit/src/main/java/com/didichuxing/doraemonkit/aop/urlconnection/HttpUrlConnectionProxyUtil.java
...emonkit/aop/urlconnection/HttpUrlConnectionProxyUtil.java
+1
-1
Android/doraemonkit/src/main/java/com/didichuxing/doraemonkit/kit/network/okhttp/interceptor/DoraemonInterceptor.java
...t/kit/network/okhttp/interceptor/DoraemonInterceptor.java
+2
-0
Android/doraemonkit/src/main/java/com/didichuxing/doraemonkit/kit/network/okhttp/interceptor/DoraemonWeakNetworkInterceptor.java
...rk/okhttp/interceptor/DoraemonWeakNetworkInterceptor.java
+5
-0
Android/doraemonkit/src/main/java/com/didichuxing/doraemonkit/kit/network/okhttp/interceptor/LargePictureInterceptor.java
...t/network/okhttp/interceptor/LargePictureInterceptor.java
+1
-0
Android/doraemonkit/src/main/java/com/didichuxing/doraemonkit/kit/network/okhttp/interceptor/MockInterceptor.java
...onkit/kit/network/okhttp/interceptor/MockInterceptor.java
+14
-2
未找到文件。
Android/app/src/debug/java/com/didichuxing/doraemondemo/MainDebugActivity.java
浏览文件 @
5054599b
...
...
@@ -341,24 +341,28 @@ public class MainDebugActivity extends AppCompatActivity implements View.OnClick
case
R
.
id
.
btn_load_img
:
//Glide 加载
String
imgUrl
=
"http://b-ssl.duitang.com/uploads/item/201808/27/20180827043223_twunu.jpg"
;
Glide
.
with
(
MainDebugActivity
.
this
)
.
asBitmap
()
.
load
(
imgUrl
)
.
diskCacheStrategy
(
DiskCacheStrategy
.
NONE
)
.
skipMemoryCache
(
true
)
.
transform
(
new
LargeBitmapGlideTransformation
(
imgUrl
))
.
into
((
ImageView
)
findViewById
(
R
.
id
.
iv_glide
));
Picasso
.
get
().
load
(
imgUrl
)
.
transform
(
new
LargeBitmapPicassoTransformation
(
imgUrl
))
.
into
((
ImageView
)
findViewById
(
R
.
id
.
iv_picasso
));
// new Thread(new Runnable() {
// @Override
// public void run() {
// requestImage(imgUrl);
// }
// }).start();
FrescoUtil
.
loadImage
((
SimpleDraweeView
)
findViewById
(
R
.
id
.
iv_fresco
),
imgUrl
);
// Glide.with(MainDebugActivity.this)
// .asBitmap()
// .load(imgUrl)
// .diskCacheStrategy(DiskCacheStrategy.NONE)
// .skipMemoryCache(true)
// .transform(new LargeBitmapGlideTransformation(imgUrl))
// .into((ImageView) findViewById(R.id.iv_glide));
//
// Picasso.get().load(imgUrl)
// .transform(new LargeBitmapPicassoTransformation(imgUrl))
// .into((ImageView) findViewById(R.id.iv_picasso));
//
// FrescoUtil.loadImage((SimpleDraweeView) findViewById(R.id.iv_fresco), imgUrl);
new
Thread
(
new
Runnable
()
{
@Override
public
void
run
()
{
requestImage
(
imgUrl
);
}
}).
start
();
break
;
...
...
Android/app/src/main/AndroidManifest.xml
浏览文件 @
5054599b
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android=
"http://schemas.android.com/apk/res/android"
xmlns:tools=
"http://schemas.android.com/tools"
package=
"com.didichuxing.doraemondemo"
>
package=
"com.didichuxing.doraemondemo"
xmlns:dist=
"http://schemas.android.com/apk/distribution"
>
<dist:module
dist:instant=
"true"
/>
<uses-permission
android:name=
"android.permission.ACCESS_COARSE_LOCATION"
/>
<uses-permission
android:name=
"android.permission.ACCESS_FINE_LOCATION"
/>
...
...
Android/doraemonkit-plugin/src/main/groovy/com/didichuxing/doraemonkit/plugin/bytecode/method/OkHttpMethodAdapter.java
浏览文件 @
5054599b
...
...
@@ -13,7 +13,7 @@ public final class OkHttpMethodAdapter extends LocalVariablesSorter implements O
//private static final LoggerWrapper logger = LoggerWrapper.getLogger(OkHttpMethodAdapter.class);
public
OkHttpMethodAdapter
(
int
access
,
String
desc
,
MethodVisitor
mv
)
{
public
OkHttpMethodAdapter
(
int
access
,
String
desc
,
MethodVisitor
mv
)
{
super
(
Opcodes
.
ASM7
,
access
,
desc
,
mv
);
}
...
...
@@ -21,13 +21,19 @@ public final class OkHttpMethodAdapter extends LocalVariablesSorter implements O
public
void
visitInsn
(
int
opcode
)
{
//添加全局的 Dokit Interceptor
if
((
opcode
>=
IRETURN
&&
opcode
<=
RETURN
)
||
opcode
==
ATHROW
)
{
//
//
插入application 拦截器
mv
.
visitVarInsn
(
ALOAD
,
0
);
mv
.
visitFieldInsn
(
GETFIELD
,
"okhttp3/OkHttpClient$Builder"
,
"interceptors"
,
"Ljava/util/List;"
);
mv
.
visitFieldInsn
(
GETSTATIC
,
"com/didichuxing/doraemonkit/aop/OkHttpHook"
,
"globalInterceptors"
,
"Ljava/util/List;"
);
mv
.
visitMethodInsn
(
INVOKEINTERFACE
,
"java/util/List"
,
"addAll"
,
"(Ljava/util/Collection;)Z"
,
true
);
mv
.
visitInsn
(
POP
);
//插入application 网络拦截器
mv
.
visitVarInsn
(
ALOAD
,
0
);
mv
.
visitFieldInsn
(
GETFIELD
,
"okhttp3/OkHttpClient$Builder"
,
"networkInterceptors"
,
"Ljava/util/List;"
);
mv
.
visitFieldInsn
(
GETSTATIC
,
"com/didichuxing/doraemonkit/aop/OkHttpHook"
,
"globalNetworkInterceptors"
,
"Ljava/util/List;"
);
mv
.
visitMethodInsn
(
INVOKEINTERFACE
,
"java/util/List"
,
"addAll"
,
"(Ljava/util/Collection;)Z"
,
true
);
mv
.
visitInsn
(
POP
);
}
super
.
visitInsn
(
opcode
);
}
...
...
Android/doraemonkit/src/main/java/com/didichuxing/doraemonkit/aop/OkHttpHook.java
浏览文件 @
5054599b
...
...
@@ -20,7 +20,25 @@ import okhttp3.Interceptor;
* ================================================
*/
public
class
OkHttpHook
{
// 每个拦截器都有自己的相对优点。
// 应用拦截器
// 不需要担心中间响应,如重定向和重试。
// 总是调用一次,即使从缓存提供HTTP响应。
// 遵守应用程序的原始意图。
// 不注意OkHttp注入的头像If-None-Match。
// 允许短路和不通话Chain.proceed()。
// 允许重试并进行多次呼叫Chain.proceed()。
// 网络拦截器
// 能够对重定向和重试等中间响应进行操作。
// 不调用缓存的响应来短路网络。
// 观察数据,就像通过网络传输一样。
// 访问Connection该请求。
public
static
List
<
Interceptor
>
globalInterceptors
=
new
ArrayList
<>();
public
static
List
<
Interceptor
>
globalNetworkInterceptors
=
new
ArrayList
<>();
private
static
boolean
IS_INSTALL
=
false
;
public
static
void
installInterceptor
()
{
...
...
@@ -29,8 +47,8 @@ public class OkHttpHook {
}
globalInterceptors
.
add
(
new
MockInterceptor
());
globalInterceptors
.
add
(
new
LargePictureInterceptor
());
globalInterceptors
.
add
(
new
DoraemonWeakNetworkInterceptor
());
globalInterceptors
.
add
(
new
DoraemonInterceptor
());
globalNetworkInterceptors
.
add
(
new
DoraemonWeakNetworkInterceptor
());
IS_INSTALL
=
true
;
}
}
Android/doraemonkit/src/main/java/com/didichuxing/doraemonkit/aop/urlconnection/HttpUrlConnectionProxyUtil.java
浏览文件 @
5054599b
...
...
@@ -17,7 +17,7 @@ import okhttp3.OkHttpClient;
* 作 者:jint(金台)
* 版 本:1.0
* 创建日期:2019-12-16-14:54
* 描 述:
* 描 述:
ams 动态插入代码
* 修订历史:
* ================================================
*/
...
...
Android/doraemonkit/src/main/java/com/didichuxing/doraemonkit/kit/network/okhttp/interceptor/DoraemonInterceptor.java
浏览文件 @
5054599b
...
...
@@ -9,6 +9,7 @@ import com.didichuxing.doraemonkit.kit.network.core.RequestBodyHelper;
import
com.didichuxing.doraemonkit.kit.network.okhttp.ForwardingResponseBody
;
import
com.didichuxing.doraemonkit.kit.network.okhttp.OkHttpInspectorRequest
;
import
com.didichuxing.doraemonkit.kit.network.okhttp.OkHttpInspectorResponse
;
import
com.didichuxing.doraemonkit.util.LogHelper
;
import
java.io.IOException
;
import
java.io.InputStream
;
...
...
@@ -29,6 +30,7 @@ public class DoraemonInterceptor implements Interceptor {
@Override
public
Response
intercept
(
Chain
chain
)
throws
IOException
{
LogHelper
.
i
(
TAG
,
"=====DoraemonInterceptor===="
);
if
(!
NetworkManager
.
isActive
())
{
Request
request
=
chain
.
request
();
return
chain
.
proceed
(
request
);
...
...
Android/doraemonkit/src/main/java/com/didichuxing/doraemonkit/kit/network/okhttp/interceptor/DoraemonWeakNetworkInterceptor.java
浏览文件 @
5054599b
package
com.didichuxing.doraemonkit.kit.network.okhttp.interceptor
;
import
com.didichuxing.doraemonkit.kit.weaknetwork.WeakNetworkManager
;
import
com.didichuxing.doraemonkit.util.LogHelper
;
import
java.io.IOException
;
...
...
@@ -13,11 +14,15 @@ import okhttp3.Response;
* 用于模拟弱网的拦截器
* <p>
* Created by xiandanin on 2019-05-09 16:29
*
* @author didi
*/
public
class
DoraemonWeakNetworkInterceptor
implements
Interceptor
{
private
static
final
String
TAG
=
"DoraemonWeakNetworkInterceptor"
;
@Override
public
Response
intercept
(
Chain
chain
)
throws
IOException
{
LogHelper
.
i
(
TAG
,
"=====DoraemonWeakNetworkInterceptor===="
);
if
(!
WeakNetworkManager
.
get
().
isActive
())
{
Request
request
=
chain
.
request
();
return
chain
.
proceed
(
request
);
...
...
Android/doraemonkit/src/main/java/com/didichuxing/doraemonkit/kit/network/okhttp/interceptor/LargePictureInterceptor.java
浏览文件 @
5054599b
...
...
@@ -24,6 +24,7 @@ public class LargePictureInterceptor implements Interceptor {
@Override
public
Response
intercept
(
Chain
chain
)
throws
IOException
{
LogHelper
.
i
(
TAG
,
"=====LargePictureInterceptor===="
);
Request
request
=
chain
.
request
();
Response
response
=
chain
.
proceed
(
request
);
String
contentType
=
response
.
header
(
"Content-Type"
);
...
...
Android/doraemonkit/src/main/java/com/didichuxing/doraemonkit/kit/network/okhttp/interceptor/MockInterceptor.java
浏览文件 @
5054599b
...
...
@@ -10,6 +10,7 @@ import com.didichuxing.doraemonkit.constant.DokitConstant;
import
com.didichuxing.doraemonkit.kit.health.AppHealthInfoUtil
;
import
com.didichuxing.doraemonkit.kit.health.model.AppHealthInfo
;
import
com.didichuxing.doraemonkit.kit.network.NetworkManager
;
import
com.didichuxing.doraemonkit.kit.network.core.ResourceType
;
import
com.didichuxing.doraemonkit.kit.network.core.ResourceTypeHelper
;
import
com.didichuxing.doraemonkit.kit.network.room_db.DokitDbManager
;
import
com.didichuxing.doraemonkit.kit.network.room_db.MockInterceptApiBean
;
...
...
@@ -38,11 +39,22 @@ public class MockInterceptor implements Interceptor {
@Override
public
Response
intercept
(
Chain
chain
)
throws
IOException
{
LogHelper
.
i
(
TAG
,
"=====MockInterceptor===="
);
Request
oldRequest
=
chain
.
request
();
Response
oldResponse
=
chain
.
proceed
(
oldRequest
);
HttpUrl
url
=
oldRequest
.
url
();
String
host
=
url
.
host
();
String
contentType
=
oldResponse
.
header
(
"Content-Type"
);
//如果是图片则不进行拦截
ResourceType
resourceType
=
contentType
!=
null
?
getResourceTypeHelper
().
determineResourceType
(
contentType
)
:
null
;
if
(
resourceType
==
ResourceType
.
IMAGE
)
{
return
oldResponse
;
}
//如果是mock平台的接口则不进行拦截
if
(
host
.
equalsIgnoreCase
(
NetworkManager
.
MOCK_HOST
))
{
return
oldResponse
;
...
...
@@ -144,7 +156,7 @@ public class MockInterceptor implements Interceptor {
//判断是否需要重定向数据接口
//http https
String
scheme
=
url
.
scheme
();
MockInterceptApiBean
interceptApiBean
=
(
MockInterceptApiBean
)
DokitDbManager
.
getInstance
().
getInterceptApiByIdInMap
(
path
,
interceptMatchedId
,
DokitDbManager
.
FROM_SDK_OTHER
);
MockInterceptApiBean
interceptApiBean
=
(
MockInterceptApiBean
)
DokitDbManager
.
getInstance
().
getInterceptApiByIdInMap
(
path
,
interceptMatchedId
,
DokitDbManager
.
FROM_SDK_OTHER
);
if
(
interceptApiBean
==
null
)
{
matchedTemplateRule
(
oldResponse
,
path
,
templateMatchedId
);
return
oldResponse
;
...
...
@@ -200,7 +212,7 @@ public class MockInterceptor implements Interceptor {
if
(
TextUtils
.
isEmpty
(
templateMatchedId
))
{
return
;
}
MockTemplateApiBean
templateApiBean
=
(
MockTemplateApiBean
)
DokitDbManager
.
getInstance
().
getTemplateApiByIdInMap
(
path
,
templateMatchedId
,
DokitDbManager
.
FROM_SDK_OTHER
);
MockTemplateApiBean
templateApiBean
=
(
MockTemplateApiBean
)
DokitDbManager
.
getInstance
().
getTemplateApiByIdInMap
(
path
,
templateMatchedId
,
DokitDbManager
.
FROM_SDK_OTHER
);
if
(
templateApiBean
==
null
)
{
return
;
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录