How to set OkHttpClient to glide

I use Glide to upload images, the problem I am facing is that when I start the application when I have a slow Internet connection, I get a SocketTimeOutException . Therefore, to solve this problem, I want to use a custom OkHttpClient so that I can change the HttpClient timeout, 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 no longer exists in the Glide API. So I was wondering how to install OkHttpClient for Glide

+19
android android-glide
source share
4 answers

To use OkHttpUrlLoader, you need to add dependencies as @darwin said, but there is a problem with the dependencies https://github.com/bumptech/glide/issues/941 . So you will add this to your dependencies

  compile ('com.github.bumptech.glide:okhttp3-integration:1.4.0'){ exclude group: 'glide-parent' } 
+17
source

since glide 4.0.0 has changed a bit.

First of all, GlideModule deprecated, and you need to use AppGlideModule if you are developing an application and LibraryGlideModule to develop a library. you need to use @GlideModule over your custom AppGlideModule class.

Secondly, there is no register() method in Glide .

and finally, okhttp3 will use the builder.

for applications, it will be as follows:

  @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 will need to have all this dependency with the exact versions in your gradle application:

  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' } 
+38
source

You need to add the okhttp3 integration dependency to your application image file

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

Send the official link Worm Integration Module

After that you can add GlideModule with okhttp ...

+2
source

Add or update okhttp3-integration:4.4.0 version okhttp3-integration:4.4.0

 implementation ('com.github.bumptech.glide:okhttp3-integration:4.4.0'){ exclude group: 'glide-parent' } 
0
source

All Articles