简体   繁体   中英

ERR_INVALID_RESPONSE when loading webView in android 9

My code is working very good in android below 9, but in android 9 I have a problem with webView when loading a resource and it show me the error message:

"Web page not available The web page at data:text/html; charset=utf-8;charset=utf-8;base64, could not be loaded because: net::ERR_INVALID_RESPONSE"

I think the problem is from UTF8 in android 9. I find this:

In Android 9, the UTF-8 decoder for Java language is stricter and follows the Unicode standard.

in android-9.0-migration https://developer.android.com/about/versions/pie/android-9.0-migration

My code is:

public void loadResourcePage() {
    loadDataWithBaseURL(basePath, "<html><body><p> some text </p></body></html>", "text/html", "UTF-8", null); }
The documents at this page (https://developer.android.com/about/versions/pie/android-9.0-migration) mention that:

In Android 9, the UTF-8 decoder for Java language is stricter and follows the Unicode standard.

So try converting the UTF-8 into Base64 and use loadData()

try {
       String base64 = null;
       base64 = android.util.Base64.encodeToString(lecureHtmlData.getBytes("UTF-8"),
                    android.util.Base64.DEFAULT);
       wvLecture.loadData(base64, "text/html; charset=utf-8", "base64");
    } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
    }
Actually you should avoid using http, but if there is no way you can do this:

Add @xml/network_security_config into your resources:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">www.smedk.ru</domain>
    </domain-config>
</network-security-config>
Add this security config to your Manifest like this:

<application
    ...
    android:networkSecurityConfig="@xml/network_security_config"
    ...>

    ...
</application>
Now you allowed using HTTP connection on www.smedk.ru subdomains.

You can read more in https://developer.android.com/training/articles/security-config#CleartextTrafficPermitted

Note: The guidance in this section applies only to apps that target Android 8.1 (API level 27) or lower. Starting with Android 9 (API level 28), cleartext support is disabled by default.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM