2012-12-11 21 views
26

我有一个包含这样的HTML字符串:如何在webview中加载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的网页视图。

我tryed这一切:

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

而且我tryed除去DOCTYPE标签:

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

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

出了什么问题?

+0

读你尝试'loadData'? – njzk2

+0

你可以把这个html字符串放在res/values/strings.xml文件中,然后用例如'你的html这里创建的项目'然后通过'Html.fromHtml(getString(R.string .website))',请检查它是否有一些时间,因为我想知道它是否也可以工作;) – deadfish

+0

(无论如何,你需要基础url?) – njzk2

回答

70

在WebView中加载数据。调用loadData的WebView

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

()方法可以检查这个例子

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

[编辑1]

您应该添加 - \ - 之前 - “ - 例如 - >name = \“西班牙新闻\”

下面的字符串为我工作

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

不起作用,只是显示一个没有内容的空白视图。 – NullPointerException

+1

请看编辑的答案 – skaya129

+0

对于正确的UTF-8,你应该使用'webView.loadData(data,“text/html; charset = UTF-8;”,null);'按照http:// stackoverflow。com/a/9312031 –

0

我有同样的要求,我在下面way.You也可以尝试这样做的..

使用loadData方法

web.loadData("<p style='text-align:center'><img class='aligncenter size-full wp-image-1607' title='' src="+movImage+" alt='' width='240px' height='180px' /></p><p><center><U><H2>"+movName+"("+movYear+")</H2></U></center></p><p><strong>Director : </strong>"+movDirector+"</p><p><strong>Producer : </strong>"+movProducer+"</p><p><strong>Character : </strong>"+movActedAs+"</p><p><strong>Summary : </strong>"+movAnecdotes+"</p><p><strong>Synopsis : </strong>"+movSynopsis+"</p>\n","text/html", "UTF-8");

movDirector movProducer像所有的都是我的字符串变量。

总之我保留我的网址自定义样式。

3

您也可以尝试这个

final WebView webView = new WebView(this); 
      webView.loadDataWithBaseURL(null, content, "text/html", "UTF-8", null); 
3

从资产HTML文件

ViewGroup webGroup; 
    String content = readContent("content/ganji.html"); 

     final WebView webView = new WebView(this); 
     webView.loadDataWithBaseURL(null, content, "text/html", "UTF-8", null); 

     webGroup.addView(webView); 
相关问题