【问题标题】:ERR_INVALID_RESPONSE when loading webView in android 9在android 9中加载webView时出现ERR_INVALID_RESPONSE
【发布时间】:2019-07-01 08:10:43
【问题描述】:

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

"网页不可用 网页位于 data:text/html; charset=utf-8;charset=utf-8;base64,无法加载,因为: net::ERR_INVALID_RESPONSE"

我认为问题来自 android 9 中的 UTF8。 我发现这个:

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

在android-9.0-migrationhttps://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); }

【问题讨论】:

    标签: java android webview


    【解决方案1】:
    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();
        }
    

    【讨论】:

    【解决方案2】:
    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.
    

    【讨论】:

    • 我的 html 中没有任何域名!我的问题只是在 android 9 中......在 android 8 和 8.1 或更低版本中一切都很好
    猜你喜欢
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    • 2016-01-18
    • 1970-01-01
    • 1970-01-01
    • 2013-03-13
    • 2018-03-01
    相关资源
    最近更新 更多