WebView.loadUrl raising UnsupportedOperationException

Issue

This exception raising only in android 5.1 (Samsung galaxy J3 2016)

Log:

Caused by java.lang.UnsupportedOperationException
    java.util.Collections$SingletonMap$1$1.remove (Collections.java:355)
    java.util.AbstractMap.remove (AbstractMap.java:397)
    org.chromium.android_webview.AwContents.loadUrl (AwContents.java:1594)
    org.chromium.android_webview.AwContents.loadUrl (AwContents.java:1458)
    com.android.webview.chromium.WebViewChromium.loadUrl (WebViewChromium.java:496)
    android.webkit.WebView.loadUrl (WebView.java:862)

I don’t have this device, I receive a ton of such exceptions from my crashlytics report. My WebView settings is below:

settings.loadWithOverviewMode  true
settings.useWideViewPort  true
@SuppressLint("SetJavaScriptEnabled")
settings.javaScriptEnabled  true
settings.domStorageEnabled  true
webViewClient  WebViewClient()

And I am loading URL with extra headers provided:

val extraHeaders  mapOf("Referer" to "https://my.site.url")
webView.loadUrl(state.url, extraHeaders)

Solution

Ok, I found a problem: In source code of AwContents in loadUrl method you can find such block:

    final String REFERER  "referer";
    Map<String, String> extraHeaders  params.getExtraHeaders();
    if (extraHeaders ! null) {
        for (String header : extraHeaders.keySet()) {
            if (REFERER.equals(header.toLowerCase(Locale.US))) {
                params.setReferrer(new Referrer(extraHeaders.remove(header), 1));
                params.setExtraHeaders(extraHeaders);
                break;
            }
        }
    }

interesting thing is extraHeaders.remove(header) because I’m creating immutable map, this causing throwing UnsupportedOperationException.

Made this change:

val extraHeaders  mutableMapOf("Referer" to "https://my.site.url")

Answered By – Axbor Axrorov

Leave a Comment