Fatal Exception: java.lang.IllegalArgumentException : Expected URL scheme 'http' or 'https' but no colon was found

Viewed 386

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 :

  1. https://bumptech.github.io/glide/int/okhttp3.html#okhttp3-1
  2. https://github.com/square/okhttp/issues/6453
  3. https://jizard.tistory.com/305
0 Answers
Related