user2779344
user2779344

Reputation: 220

WebView shows source html with loadDataWithBaseURL, not rendered view

I'm developing an application witch uses WebView to render custom html. But when I call
loadDAtaWithBaseURL(URL, "<html><h1>TEST</h1></html>", "text/html; charset=utf-8;", "utf-8", null);
it shows html itself (not rendered one) on Genymotion emulator. On my HTC-one, it works fine with rendered html. Each result is showed as attached.

Shown result on Genymotion emulator Shown result on HTC one

Does anyone have a same problem or solution? Thanks.

Upvotes: 17

Views: 1820

Answers (2)

Salih Balkan
Salih Balkan

Reputation: 31

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

Upvotes: 2

El Don
El Don

Reputation: 922

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.

Upvotes: 0

Related Questions