如何为滑行设置OkHttpClient
How to set OkHttpClient for glide
我正在使用 Glide 加载图像,我面临的问题是当我 运行 应用程序在慢速互联网连接上时,我得到 SocketTimeOutException
。所以为了解决这个问题,我想使用自定义 OkHttpClient
以便我可以更改 HttpClient 的超时,这是我的代码。
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);
}
}
但是 Glide API 中不再存在 OkHttpUrlLoader
。所以我想知道如何为 Glide
设置 OkHttpClient
您需要将 okhttp3-integration 依赖添加到您的应用程序 gradile 文件中
dependencies {
compile 'com.github.bumptech.glide:okhttp3-integration:1.4.0@aar'
//compile 'com.squareup.okhttp3:okhttp:3.2.0'}
参考官方linkglide integration module
之后你可以用 okhttp 添加 GlideModule...
要使用 OkHttpUrlLoader,您需要像 @darwin 所说的那样添加依赖项,但存在依赖项问题 https://github.com/bumptech/glide/issues/941。所以你将在你的依赖项中添加它
compile ('com.github.bumptech.glide:okhttp3-integration:1.4.0'){
exclude group: 'glide-parent'
}
自从glide 4.0.0开始有了一些变化
首先,GlideModule
已弃用,如果您正在开发应用程序,则需要使用 AppGlideModule
,而对于库开发,则需要使用 LibraryGlideModule
。您需要在自定义 AppGlideModule
class.
之上使用 @GlideModule
其次,Glide
对象中不再有 register()
方法。
最后 okhttp3 将使用构建器。
应用程序如下所示:
@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);
}
}
您需要将所有这些依赖项与您的应用 gradle 文件中的确切版本相关联:
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'
}
添加或升级okhttp3-integration:4.4.0
版本
implementation ('com.github.bumptech.glide:okhttp3-integration:4.4.0'){
exclude group: 'glide-parent'
}
基于 docs,这是正确的最新方法:
@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)
)
}
}
并且您将需要所有这些依赖项:
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'
我正在使用 Glide 加载图像,我面临的问题是当我 运行 应用程序在慢速互联网连接上时,我得到 SocketTimeOutException
。所以为了解决这个问题,我想使用自定义 OkHttpClient
以便我可以更改 HttpClient 的超时,这是我的代码。
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);
}
}
但是 Glide API 中不再存在 OkHttpUrlLoader
。所以我想知道如何为 Glide
您需要将 okhttp3-integration 依赖添加到您的应用程序 gradile 文件中
dependencies {
compile 'com.github.bumptech.glide:okhttp3-integration:1.4.0@aar'
//compile 'com.squareup.okhttp3:okhttp:3.2.0'}
参考官方linkglide integration module
之后你可以用 okhttp 添加 GlideModule...
要使用 OkHttpUrlLoader,您需要像 @darwin 所说的那样添加依赖项,但存在依赖项问题 https://github.com/bumptech/glide/issues/941。所以你将在你的依赖项中添加它
compile ('com.github.bumptech.glide:okhttp3-integration:1.4.0'){
exclude group: 'glide-parent'
}
自从glide 4.0.0开始有了一些变化
首先,GlideModule
已弃用,如果您正在开发应用程序,则需要使用 AppGlideModule
,而对于库开发,则需要使用 LibraryGlideModule
。您需要在自定义 AppGlideModule
class.
@GlideModule
其次,Glide
对象中不再有 register()
方法。
最后 okhttp3 将使用构建器。
应用程序如下所示:
@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);
}
}
您需要将所有这些依赖项与您的应用 gradle 文件中的确切版本相关联:
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'
}
添加或升级okhttp3-integration:4.4.0
版本
implementation ('com.github.bumptech.glide:okhttp3-integration:4.4.0'){
exclude group: 'glide-parent'
}
基于 docs,这是正确的最新方法:
@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)
)
}
}
并且您将需要所有这些依赖项:
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'