How to set OkHttpClient for glide

后端 未结 5 1083
不思量自难忘°
不思量自难忘° 2020-12-05 17:45

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

相关标签:
5条回答
  • 2020-12-05 18:09

    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.11.0'
    kapt 'com.github.bumptech.glide:compiler:4.11.0'
    implementation 'com.github.bumptech.glide:okhttp3-integration:4.11.0'
    
    0 讨论(0)
  • 2020-12-05 18:22

    Add or upgrade okhttp3-integration:4.4.0 version

    implementation ('com.github.bumptech.glide:okhttp3-integration:4.4.0'){
            exclude group: 'glide-parent'
        }
    
    0 讨论(0)
  • 2020-12-05 18:26

    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'
        }
    
    0 讨论(0)
  • 2020-12-05 18:29

    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'
        }
    
    0 讨论(0)
  • 2020-12-05 18:34

    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...

    0 讨论(0)
提交回复
热议问题