在项目中记录或打印OkHttp网络请求,我们一般会通过添加HttpLoggingInterceptor到我们的OkHttpClient来实现。但在一个较大的APP中,有些业务模块是通过AAR依赖到项目中。对于这些模块的网络请求监控,我们可以直接将监控逻辑代码插桩到OkHttp源码中来实现。
通过查看OKhttp3源码,定位到需要将代码织入的位置为RealCall类的getResponseWithInterceptorChain()函数
Response getResponseWithInterceptorChain() throws IOException {
// Build a full stack of interceptors.
List<Interceptor> interceptors = new ArrayList<>();
interceptors.addAll(client.interceptors());
interceptors.add(retryAndFollowUpInterceptor);
interceptors.add(new BridgeInterceptor(client.cookieJar()));
interceptors.add(new CacheInterceptor(client.internalCache()));
interceptors.add(new ConnectInterceptor(client));
if (!forWebSocket) {
interceptors.addAll(client.networkInterceptors());
}
interceptors.add(new CallServerInterceptor(forWebSocket));
Interceptor.Chain chain = new RealInterceptorChain(interceptors, null, null, null, 0,
origi