完整异常:
Fatal Exception: java.lang.IllegalArgumentException
Expected URL scheme 'http' or 'https' but no colon was found
okhttp3.HttpUrl$Builder.parse$okhttp (HttpUrl.kt:1260)
okhttp3.HttpUrl$Companion.get (HttpUrl.kt:1633)
okhttp3.Request$Builder.url (Request.kt:184)
okhttp3.Cache$Entry.response (Cache.kt:641)
okhttp3.Cache.get$okhttp (Cache.kt:183)
okhttp3.internal.cache.CacheInterceptor.intercept (CacheInterceptor.kt:47)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109)
okhttp3.internal.http.BridgeInterceptor.intercept (BridgeInterceptor.kt:83)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109)
okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept (RetryAndFollowUpInterceptor.kt:76)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109)
okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp (RealCall.kt:201)
okhttp3.internal.connection.RealCall$AsyncCall.run (RealCall.kt:517)
java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1167)
我在两个位置使用了Okhttp3,网络模块和Glide模块。
添加GLIDE模块时,碰撞开始发生。
@GlideModule
class CustomGlideModule : AppGlideModule() {
/**
* Registers a set of components to use
* when initializing Glide within the app when Glide's annotation processor is used.
*/
override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
val cache = Cache(
directory = File(context.cacheDir, GLIDE_CACHE_FILE_NAME),
maxSize = CACHE_SIZE
)
cache.clearMalformedUrls()
val client: OkHttpClient = Builder()
.cache(cache)
.build()
registry.register(
SVG::class.java,
PictureDrawable::class.java,
SvgDrawableTranscoder()
).append(InputStream::class.java, SVG::class.java, SvgDecoder())
.replace(
GlideUrl::class.java,
InputStream::class.java,
OkHttpUrlLoader.Factory(client)
)
}
}
我已经为每个OKHTTP客户端创建单独的文件,一个用于网络,另一个用于Glide Therefore, each of them has his own file, which he can only access
and create function called clearMalformedUrls() to clear the null url found in the cached files And inside it we use toHttpUrlOrNull() is an Okhttp3 utility function that returns null if it is not http:// or https://.
fun Cache.clearMalformedUrls() {
val urlIterator = urls()
while (urlIterator.hasNext()) {
if (urlIterator.next().toHttpUrlOrNull() == null) {
urlIterator.remove()
}
}
}
Note : Never be able to reproduce it, its only happened on production.
References :
Full Exception :
Fatal Exception: java.lang.IllegalArgumentException
Expected URL scheme 'http' or 'https' but no colon was found
okhttp3.HttpUrl$Builder.parse$okhttp (HttpUrl.kt:1260)
okhttp3.HttpUrl$Companion.get (HttpUrl.kt:1633)
okhttp3.Request$Builder.url (Request.kt:184)
okhttp3.Cache$Entry.response (Cache.kt:641)
okhttp3.Cache.get$okhttp (Cache.kt:183)
okhttp3.internal.cache.CacheInterceptor.intercept (CacheInterceptor.kt:47)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109)
okhttp3.internal.http.BridgeInterceptor.intercept (BridgeInterceptor.kt:83)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109)
okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept (RetryAndFollowUpInterceptor.kt:76)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109)
okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp (RealCall.kt:201)
okhttp3.internal.connection.RealCall$AsyncCall.run (RealCall.kt:517)
java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1167)
I used okhttp3 in two places, the network module and the glide module.
The crash start happening when adding the glide module .
@GlideModule
class CustomGlideModule : AppGlideModule() {
/**
* Registers a set of components to use
* when initializing Glide within the app when Glide's annotation processor is used.
*/
override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
val cache = Cache(
directory = File(context.cacheDir, GLIDE_CACHE_FILE_NAME),
maxSize = CACHE_SIZE
)
cache.clearMalformedUrls()
val client: OkHttpClient = Builder()
.cache(cache)
.build()
registry.register(
SVG::class.java,
PictureDrawable::class.java,
SvgDrawableTranscoder()
).append(InputStream::class.java, SVG::class.java, SvgDecoder())
.replace(
GlideUrl::class.java,
InputStream::class.java,
OkHttpUrlLoader.Factory(client)
)
}
}
I'm already create separate file for every okhttp client , one for the network and the other for glide
Therefore, each of them has his own file, which he can only access
and create function called clearMalformedUrls() to clear the null url found in the cached files
And inside it we use toHttpUrlOrNull() is an Okhttp3 utility function that returns null if it is not http:// or https://.
fun Cache.clearMalformedUrls() {
val urlIterator = urls()
while (urlIterator.hasNext()) {
if (urlIterator.next().toHttpUrlOrNull() == null) {
urlIterator.remove()
}
}
}
Note : Never be able to reproduce it, its only happened on production.
References :
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论