问题描述
我正在使用Glide加载图像,我面临的问题是,当我在缓慢的Internet连接上运行应用程序时,我得到了SocketTimeOutException
.因此,要解决此问题,我想使用自定义OkHttpClient
,以便我可以更改HttpClient的超时,这是我拥有的代码.
I am using Glide to load images, the issue I'm facing is that when i run app on slow internet connection I'm getting SocketTimeOutException
. So to solve this issue i want to use a custom OkHttpClient
so that I can change the timeout of HttpClient this is the code i have.
public class MyGlideModule implements GlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
}
@Override
public void registerComponents(Context context, Glide glide) {
OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(15, TimeUnit.SECONDS);
client.setReadTimeout(15,TimeUnit.SECONDS);
OkHttpUrlLoader.Factory factory = new OkHttpUrlLoader.Factory(client);
glide.register(GlideUrl.class, InputStream.class, factory);
}
}
但OkHttpUrlLoader
在Glide API中不再存在.所以我想知道如何为Oklide设置OkHttpClient
but OkHttpUrlLoader
is not there any more in Glide API. So i was wondering how can set the OkHttpClient for Glide
推荐答案
要使用OkHttpUrlLoader,您需要像@darwin所说的那样添加依赖项,但是存在依赖项问题 https://github.com/bumptech/glide/issues/941 .因此,您将在您的依赖项中添加
To use OkHttpUrlLoader you need to add dependencies as the @darwin said but there is dependency issue https://github.com/bumptech/glide/issues/941. So you will be adding this in your dependencies
compile ('com.github.bumptech.glide:okhttp3-integration:1.4.0'){
exclude group: 'glide-parent'
}
这篇关于如何设置OkHttpClient进行滑行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!