我使用的Psiphon VPN在我的应用程序捆绑在一起,因此所有REST调用经过本地端口。
我添加了一个代理OkHttp
客户inRetrofit
是这样的:
OkHttpClient.Builder client =
new OkHttpClient.Builder().proxy(new Proxy(Proxy.Type.HTTP,
new InetSocketAddress("localhost", randomPort )));
builder.client(client.build());
retrofit = builder.build();
一切工作正常,到目前为止,我可以成功加载通过本地代理的所有文本内容,但图像没有加载,因为滑翔有一个自定义的连接。
现在,我想设置类似上面的代码代理到滑翔,或任何其他库图像加载
我使用滑翔“4.8.0”只是这样的:
Glide.with(this)
.load(image_url)
.into(miniImageView);
我这样做是使用自定义@GlideModule
添加以下行到应用程序build.gradle
implementation 'com.github.bumptech.glide:glide:4.8.0'
implementation('com.github.bumptech.glide:okhttp3-integration:4.8.0') {
exclude group: 'glide-parent'
}
implementation 'com.github.bumptech.glide:annotations:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
这里是我的自定义模块:
@GlideModule
public class CustomGlideModule extends AppGlideModule {
@Override
public void registerComponents(Context context, Glide glide, Registry registry) {
Builder builder = new Builder()
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS);
if (VpnService.connected) {
builder.proxy(VpnService.proxy);
}
OkHttpClient client = builder.build();
OkHttpUrlLoader.Factory factory = new OkHttpUrlLoader.Factory(client);
glide.getRegistry().replace(GlideUrl.class, InputStream.class, factory);
}
}
滑翔会检测到它自动使用@GlideModule
在类的顶部,将它注入上运行。现在滑翔的每一个图像加载会经过我的自定义模块和自定义OkHttp连接