[英]Displaying iso-8859-1 encoded content in WebView
我正在從SQlite數據庫獲取HTML編碼的內容,該內容要顯示在WebView中。 我目前正在使用:
public class ShowAbstracts extends Activity {
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String body = extras.getString(DatabaseHelper.KEY_BODY);
WebView webview = new WebView(this);
setContentView(webview);
String summary = "<html><body> "+body+" </body></html>";
webview.loadData(summary, "text/html", "iso-8859-1");
}
}
}
它的工作方式是打開WebView並顯示內容,但是我得到了奇怪的字符,對於某些內容,它只是崩潰了。 數據庫是ISO-8859-1編碼的。 有什么方法可以照顧數據庫內容中的特殊字符並正確顯示它們?
首先十分感謝!
里克
成功!
解決這個問題是三件事的結合:
。
public class ShowAbstracts extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String body = extras.getString(DatabaseHelper.KEY_BODY);
WebView webview = new WebView(this);
setContentView(webview);
String summary = "<?xml version='1.0' encoding='utf-8'?><html><body>"+body+" </body></html>";
String uri = Uri.encode(summary);
webview.loadData(uri, "text/html", "utf-8");
}
}
}
WebView的api(http://developer.android.com/reference/android/webkit/WebView.html)表示data參數必須轉義URI(http://developer.android.com/reference/java /net/URLEncoder.html)。
另外,當您有字符串時,編碼已經完成。 您必須確保使用正確的編碼創建了字符串(對於Android,默認值為UTF-8)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.