本文介绍了如何为滑行设置 OkHttpClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Glide 加载图像,我面临的问题是当我在慢速互联网连接上运行应用程序时,我收到 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 中不再存在.所以我想知道如何为 Glide 设置 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 13:19