42

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);
    }
}

but OkHttpUrlLoader is not there any more in Glide API. So i was wondering how can set the OkHttpClient for Glide

user2934930
  • 1,076
  • 2
  • 12
  • 26

5 Answers5

55

since glide 4.0.0 it has changed a little bit.

first of all GlideModule is deprecated and you need to use AppGlideModule if you are developing an application and LibraryGlideModule for library development. you need to use @GlideModule above your custom AppGlideModule class.

secondly there is no register() method in Glide object any more.

and finally okhttp3 will use a builder.

it'll be like below for apps:

    @GlideModule
    private class CustomGlideModule extends AppGlideModule {

       @Override
       public void registerComponents(Context context, Glide glide, Registry registry) {
           OkHttpClient client = new OkHttpClient.Builder()
                   .readTimeout(15, TimeUnit.SECONDS)
                   .connectTimeout(15, TimeUnit.SECONDS)
                   .build();

       OkHttpUrlLoader.Factory factory = new OkHttpUrlLoader.Factory(client);

           glide.getRegistry().replace(GlideUrl.class, InputStream.class, factory);
       }
   }

you'll need to have all these dependency with the exact versions in your app gradle file:

 compile "com.squareup.okhttp3:okhttp:3.8.1"
    compile 'com.github.bumptech.glide:glide:4.0.0'
    compile ('com.github.bumptech.glide:okhttp3-integration:4.0.0'){
        exclude group: 'glide-parent'
    }
Amir Ziarati
  • 14,248
  • 11
  • 47
  • 52
  • 3
    It works for me except the missing .build() at the end of the connecttimeout line. – Louis Tsai Sep 08 '17 at 12:08
  • If you add `exclude group: 'glide-parent'`, it will write: 'Could not find method com.github.bumptech.glide:okhttp3-integration:4.11.0() for arguments [build_88p4z8p3v5idok4cebd6iktio$_run_closure3$_closure15@24e35b27] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.' – CoolMind Sep 09 '20 at 11:33
23

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'
    }
umerk44
  • 2,797
  • 4
  • 23
  • 43
14

Based on the docs, this is the proper up-to-date way to do it:

@GlideModule
@Excludes(OkHttpLibraryGlideModule::class)
class MyGlideModule : AppGlideModule() {

  override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
    registry.replace(
      GlideUrl::class.java,
      InputStream::class.java,
      OkHttpUrlLoader.Factory(myOkHttpClient)
    )
  }
}

And you'll need all these dependencies:

implementation 'com.github.bumptech.glide:glide:4.12.0'
kapt 'com.github.bumptech.glide:compiler:4.12.0'
implementation 'com.github.bumptech.glide:okhttp3-integration:4.12.0'
Fred Porciúncula
  • 8,533
  • 3
  • 40
  • 57
3

Add or upgrade okhttp3-integration:4.4.0 version

implementation ('com.github.bumptech.glide:okhttp3-integration:4.4.0'){
        exclude group: 'glide-parent'
    }
Mansukh Ahir
  • 3,373
  • 5
  • 40
  • 66
2

You need to add okhttp3-integration dependency to your app gradile file

dependencies {
compile 'com.github.bumptech.glide:okhttp3-integration:1.4.0@aar'
//compile 'com.squareup.okhttp3:okhttp:3.2.0'}

Reffer the official link glide integration module

After that u can add GlideModule with okhttp...

darwin
  • 1,524
  • 1
  • 22
  • 32
  • If you're using gradle, you can include this module simply by depending on the aar, the module will be merged in by manifest merger. There is no need to add a GlideModule by yourself. – Murciegalo84 Jan 15 '18 at 14:33