在安卓中将HTML加载到Webview中?

3
我正在将以下HTML内容加载到Webview中。但是我遇到了以下错误:

网页无法访问 text/html;utf8%3C!DOCTYPE%20HTML%20PUBLIC%20//W3C// ........

还有更多内容。
这是我的HTML内容:
<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 

Transitional//EN'><html><head><title>Personality ABC</title></head><body><table 

border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td 

align="center"><strong>[Red-Blue] Sachin</strong><br /><img 

src="r7y3b6.png" alt="[Red-Blue] 

Sachin" title="[Red-Blue] Sachin" /><br />Sachin</td><td align="center"><strong>

[Yellow-Red-Blue] Test1</strong><br /><img src="5y5b5.png" alt="[Yellow-Red-Blue] Test1" title="[Yellow-

Red-Blue] Test1" /><br />Test1</td></tr></table><br/><p><strong>Harmony:</strong></p>
<p><strong>Test1</strong> and <strong>Sachin</strong> experience lots of similarities 

together. Both personalities have strong red (dominant, authoritative and impatient) 

and blue (rational, reserved and analytic) sides and understand each other&rsquo;s 

nature; both being rational, thoughtful but dominant, authoritative and a strong 

leader at the same time. Both love to be in control and love to be superior and have a 

need for progress and a love for challenges.</p>
<p><strong>Conflict potential:</strong></p>
<p><strong>Test1</strong> has all 3 personalities in one and<strong> </strong>gets 

irritated easily. It is like a mood switch from one to the other and back again: 

yellow (friendly, kind) to red (dominant, need to lead) to blue (rational, thinking 

through, being objective). This irritates <strong>Sachin</strong> because he/she finds 

it frustrating when someone cannot make up their mind. <strong>Test1</strong> 

sometimes feels confused and expresses it with: &ldquo;I am confused!&rdquo; This is 

difficult for <strong>Sachin</strong> to deal with.</p></body></html>

我不知道问题出在哪里?

你怎么加载它?请提供代码。 - fazo
你尝试过移除标签<!DOCTYPE ...>吗? - Ahmad Kayyali
@fazo 我是这样使用的 webView.loadData(str,"text/html", "utf-8"); - David
  1. 确保您在str中有任何内容
  2. 根据Ahmad的建议删除doctype
  3. 最后尝试非常简单的<div>text</div>作为str
- fazo
2个回答

10
WebView wv = (WebView)findViewById(R.id.webView);

wv.loadDataWithBaseURL("notreal/", data_html, "text/html", "utf-8",null);


2
我有这段代码来打开一个HTML文件。
WebView web = new WebView(this);
web.loadUrl("file:///sdcard/file.html");//The file:// is the protocol and it's nedded for internal files

嗨,我认为他可以在不保存到SD卡等文件中的情况下加载HTML数据。 - Houcine

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接