在Android 9中加载webView时出现ERR_INVALID_RESPONSE

时间:2019-07-01 08:10:43

标签: java android webview

我的代码在9以下的android系统中运行良好,但是在android 9中,加载资源时webView出现问题,并且显示错误消息:

”网页不可用 网页位于data:text / html; charset = utf-8; charset = utf-8; base64,由于以下原因而无法加载: 净:: ERR_INVALID_RESPONSE”

我认为问题是来自Android 9中的UTF8。 我找到了:

  

在Android 9中,用于Java语言的UTF-8解码器更加严格,   遵循Unicode标准。

在android-9.0迁移https://developer.android.com/about/versions/pie/android-9.0-migration

我的代码是:

public void loadResourcePage() {
    loadDataWithBaseURL(basePath, "<html><body><p> some text </p></body></html>", "text/html", "UTF-8", null); }

2 个答案:

答案 0 :(得分:0)

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();
    }

答案 1 :(得分:-1)

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.