我正在从一个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编码的。
有什么方法可以处理数据库内容中的特殊字符并正确显示它们吗?
非常感谢提前!
里克
最佳答案
成功!
解决这个问题是三件事的结合:
编码字符串的uri
在标识utf-8编码的html字符串中粘贴头
安装libreoffice,它允许将用于生成sqlite数据库的xls文件保存为具有utf-8编码的csv文件(据我从ms-office-excel所知,这是不可能的)。
是的。
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");
}
}
}