gpt4 book ai didi

android - 如何在 webview 中加载 html 字符串?

转载 作者:IT王子 更新时间:2023-10-28 23:51:29 25 4
gpt4 key购买 nike

我有一个包含以下内容的 html 字符串:

    <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1250">
<meta name="spanish press" content="spain, spanish newspaper, news,economy,politics,sports">
<title></title>
</head>
<body id="body">
<!-- The following code will render a clickable image ad in the page -->
<script src="http://www.myscript.com/a"></script>
</body>
</html>

我需要将该网站显示到 android 中的 webview 中。

我尝试了所有这些:

webView.loadDataWithBaseURL(null, txt, "text/html", "UTF-8", null);
webView.loadDataWithBaseURL("x-data://base", txt, "text/html", "UTF-8", null);
webView.loadDataWithBaseURL("notreal/", txt, "text/htm", "utf-8",null);

我也尝试删除 DOCTYPE 标签:

txt=txt.replace("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">", "");

没有一个人有工作。我刚刚实现了将字符串显示到 webview(html 代码)中,而不是必须使用该 html 代码创建的网站。

怎么了?

最佳答案

在 WebView 中加载数据。调用WebView的loadData()方法

wv.loadData(yourData, "text/html", "UTF-8");

你可以看看这个例子

http://developer.android.com/reference/android/webkit/WebView.html

[编辑 1]

您应该在 -- "之前添加 --\-- -- 例如 --> name=\"spanish press\"

下面的字符串对我有用

String webData =  "<!DOCTYPE html><head> <meta http-equiv=\"Content-Type\" " +
"content=\"text/html; charset=utf-8\"> <html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=windows-1250\">"+
"<meta name=\"spanish press\" content=\"spain, spanish newspaper, news,economy,politics,sports\"><title></title></head><body id=\"body\">"+
"<script src=\"http://www.myscript.com/a\"></script>şlkasşldkasşdksaşdkaşskdşk</body></html>";

关于android - 如何在 webview 中加载 html 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13816464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com