17

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.

2
  • 18
    Sorry guys, I solve this by myself. "text/html; charset=utf-8;" is the problem and it should be only "text/html". Commented May 24, 2015 at 7:17
  • 1
    i want to buy you a beer man. You saved me. Commented Jul 18, 2016 at 19:40

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Great, seems to have helped me. How did you come up with this?
This problem occured at me when I added mimeType, so it didn't become so hard :)
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.