如何在 webview 中加载 html 字符串? [英] How to load html string in a webview?

查看:97
本文介绍了如何在 webview 中加载 html 字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 <头><meta http-equiv="content-type" content="text/html; charset=windows-1250"><meta name="spanish press" content="西班牙、西班牙报纸、新闻、经济、政治、体育"><title></title><body id="body"><!-- 以下代码将在页面中呈现可点击的图片广告--><script src="http://www.myscript.com/a"></script>

我需要将该网站显示到 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

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

下面的字符串对我有用

String webData = " "+"<meta name="spanish press" content="西班牙、西班牙报纸、新闻、经济、政治、体育"><title></title></head><body id="正文">"+"<script src="http://www.myscript.com/a"></script>şlkasşldkasşdksaşdkaşskdşk</body></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>

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

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

Also i tryed removing DOCTYPE tag:

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

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.

What is wrong?

解决方案

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

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

You can check this example

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

[Edit 1]

You should add -- -- before -- " -- for example --> name="spanish press"

below string worked for me

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

这篇关于如何在 webview 中加载 html 字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆