Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 500
NullPointerException Avatar asked Dec 11 '12 08:12

NullPointerException


People also ask

How do I load HTML code into WebView?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken web view to show html content.

How do I load a string in HTML?

you can put this html string to file res/values/strings. xml and create item for example <string name="website">your html here</string> and then load it by Html. fromHtml(getString(R. string.

Which method is used to load HTML content in WebView?

The loadUrl() and loadData() methods of Android WebView class are used to load and display web page.


1 Answers

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>"; 
like image 187
skaya129 Avatar answered Sep 22 '22 04:09

skaya129