如何加载HTML字符串中的WebView?字符串、加载、HTML、WebView

2023-09-13 01:10:33 作者:一辈子☆守护沵

我有一个包含这样的HTML字符串:

i have a html string containing this:

    <!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的一个web视图。

I need to show that website into a webview in android.

我trye​​d这一切:

I tryed with all this:

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);

另外我trye​​d删除DOCTYPE标签:

Also i tryed removing DOCTYPE tag:

TXT = txt.replace(!&LT; D​​OCTYPE HTML PUBLIC \ - // W3C // DTD HTML 4.01过渡// EN \&gt;中,);

那些没有人有工作。我只是实现显示的字符串到web视图(html的code),但不包含必须与HTML code创建的网站。

No one of those have work. I just achieved to show the string into the webview (the html code), but not the website that must be created with that html code.

什么是错的?

推荐答案

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

To load your data in WebView. Call loadData() method of WebView

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

您可以检查这个例子

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

您应该添加 - \ - 前 - - 例如 - > 名= \西班牙preSS \

You should add -- \ -- before -- " -- for example --> 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>";