Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

android - How to load html string in a webview?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...