WebView shows source html with loadDataWithBaseURL, not rendered view

前端 未结 2 974
一向
一向 2020-12-16 13:00

I\'m developing an application witch uses WebView to render custom html. But when I call
loadDAtaWithBaseURL(URL, \"

TEST

相关标签:
2条回答
  • 2020-12-16 13:31

    Regarding the info you have given, i can not have a clear debug for the issue, but this is how it should be done, just to check if you missed something

    1. First, add this line to your activity in the manifest file

    2. Load your data using

      public void loadDataWithBaseURL (String baseUrl, String data, String mimeType, String encoding, String historyUrl);

    And this is done this way

    loadDataWithBaseURL(Url, data, "text/html", "UTF-8", historyUrl)
    

    Note that

    If the base URL uses the data scheme, this method is equivalent to calling loadData() and the historyUrl is ignored, and the data will be treated as part of a data: URL. If the base URL uses any other scheme, then the data will be loaded into the WebView as a plain string (i.e. not part of a data URL) and any URL-encoded entities in the string will not be decoded.

    0 讨论(0)
  • 2020-12-16 13:33

    Don't enter mimeType below KitKat.

    fun getMimeType(): String? {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            "text/html; charset=utf-8"
        } else {
            null
        }
    }
    loadDAtaWithBaseURL(URL, "<html><h1>TEST</h1></html>", getMimeType(), "utf-8", null);
    

    Java:

    if(Build.VERSION.SDK_INT < 21)
        webView.loadDataWithBaseURL("about:blank","<html><h1>TEST</h1></html>","text/html", "UTF-8",null);
    else
        webView.loadDataWithBaseURL("about:blank","<html><h1>TEST</h1></html>","text/html; charset=utf-8", "UTF-8",null);
    
    0 讨论(0)
提交回复
热议问题