Android WebView with garbled UTF-8 characters.

拥有回忆 提交于 2019-11-26 06:18:06

问题


I\'m using some webviews in my android app, but are unable to make them display in utf-8 encoding.

If use this one I won\'t see my scandinavian charcters:

mWebView.loadUrl(\"file:///android_asset/om.html\")

And if try this one, I won\'t get anything displayed at all

mWebView.loadDataWithBaseURL(\"file:///android_asset/om.html\", null, \"text/html\", \"utf-8\",null);

Regards


回答1:


You can try to edit the settings of your webview before you load the data:

WebSettings settings = mWebView.getSettings();
settings.setDefaultTextEncodingName("utf-8");

Also, as provided in the comment below, be sure to add "charset=utf-8" to the loadData call:

mWebView.loadData(getString(R.string.info_texto), "text/html; charset=utf-8", "utf-8");



回答2:


This seems to have been broken in some form or fashion forever. Issue 1733

Use loadDataWithBaseURL instead of loadData.

// Pretend this is an html document with those three characters
String scandinavianCharacters = "øæå";

// Won't render correctly
webView.loadData(scandinavianCharacters, "text/html", "UTF-8");

// Will render correctly
webView.loadDataWithBaseURL(null, scandinavianCharacters, "text/html", "UTF-8", null);

Now the part that is truly annoying is that on the Samsung Galaxy S II (4.0.3) loadData() works just fine, but testing on the Galaxy Nexus (4.0.2) the multi-byte characters are garbled unless you use loadDataWithBaseURL(). WebView Documentation

Recent versions of Android

Some are reporting a change in the behavior of the loadData calls requiring the mimeType to include charset=utf-8.

webView.loadData(scandinavianCharacters, "text/html; charset=utf-8", "UTF-8");

You can also use this formulation with WebSettings

WebView webView = (WebView) findViewById(R.id.DemoWebView);
WebSettings webSettings = webView.getSettings();
webSettings.setDefaultTextEncodingName("utf-8");  
webView.loadData(scandinavianCharacters, "text/html; charset=utf-8", null);

It is amazing that Android still hasn't resolved this basic issue.




回答3:


Derzu's bit is very helpful above:

webview.loadData(getString(R.string.info_texto), "text/html; charset=utf-8", "utf-8"); 

I had utf-8 on Adroid 2.x and garbled ansi on 4.x until I put in the

 charset=utf-8

in the wv.loadUrlWhatever() call. Excellent attention to detail, Derzu




回答4:


There are two ways that a HTML page delivered by a HTTP server can specify the content encoding. Usually, the server will specify the content encoding in the HTTP headers, but since this page is being loaded from a file, there is no HTTP transaction and therefore no headers. As a result, WebView assumes a default encoding of Latin-1.

However, you can specify a content encoding using the <meta> tag. Construct your html file thus:

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>Title</title>
</head>
Your content following

And then load it into WebView using mWebView.loadUrl("file:///android_asset/om.html");. It should display the non-latin characters as you expect.




回答5:


WebView wv = (WebView) findViewById(R.id.rowWebview);
WebSettings settings = wv.getSettings();
settings.setDefaultTextEncodingName("utf-8");                   
wv.loadData(topHtml, "text/html; charset=utf-8",null);

A combination of the two seems to work for me. For some reason it likes null on the encoding and the charset in the mime type :/ weird. this has solved months of aggravation for me.




回答6:


You need to swap your first two arguments. See this thread: Android WebView UTF-8 not showing

So your code should look like this:

mWebView.loadDataWithBaseURL(null, "file:///android_asset/om.html", "text/html", "utf-8",null);



回答7:


I'm not sure what you are doing prior to loading that page. Could this security change have anything to do with it? Are you loading page from web before?

Note for post 1.0. Due to the change in the WebKit, the access to asset files through "file:///android_asset/" for the sub resources is more restricted. If you provide null or empty string as baseUrl, you won't be able to access asset files. If the baseUrl is anything other than http(s)/ftp(s)/about/javascript as scheme, you can access asset files for sub resources.

Taken from here: http://developer.android.com/reference/android/webkit/WebView.html In the section on method "loadDataWithBaseURL".

Can you use "loadData" instead for a quick test? Specify "utf-8" for the encoding and pasting a scandinavian character into the data parmeter. Simple test to remove the security issue.




回答8:


You should keep 3 things in mind to show the right content always:

  1. Using loadDataWithBaseUrl instead of loadData funciton.
  2. Setting the correct encoding in html file as a meta tag
  3. Setting defaultTextEncodingName in WebSettings

The examples have been provided via other answers so I don't repeat!




回答9:


mwebView.loadData(URLEncoder.encode(data, "utf-8").replaceAll("\\+"," "), "text/html", "utf-8");


来源:https://stackoverflow.com/questions/4933069/android-webview-with-garbled-utf-8-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!