How to create / update / upload HTML file at runtime

I need to create and open an HTML file at runtime.

<html> <body> <form name="Home" method="POST" action='' > <input id='Title' type='hidden' value="Mr" name='Title'/> <input id='Name' type='hidden' value="bala" name='Name'/> . . . </form> <script language='javascript'> //java script </script> </body> </html> 

In the above format, I need to update the value field at runtime. After the update, I want to open this html file in my Android application.

Update: I know that we can store the HTML file as shown below.

 public static String createHTML(Study study) { String format = "<html>" + "<body>" + "<form name="Home" method="POST" action='' >" + "<input id='Title' type='hidden' value='%d' name='Title'/>" + . . . . " </body>" + "</html>"; return String.format(format, study.title, study.name...); } 

but I want to know if there is another way to create an HTML file, for example, create an XML file using XmlSerializer Thanks!

+2
java android html android-webview
source share
4 answers

How to create an html file.

  • Open file
  • Enter text characters containing HTML
  • Close file

If you need more information, the Oracle Java Tutorial contains tutorials on basic Java constructors and how to do simple file I / O.


There are other “elegant” ways to do these things in different contexts (like a web service), but if this is the total amount of what you need to do, then ... keep it simple.


... is there any other way to create an HTML file, for example, creating an XML file using XmlSerializer.

Not HTML. You can create XHTML this way.

Having said that, building HTML or XHTML sounds like a bad idea. The resulting code would be neither readable nor efficient ... compared to string concatenation or pattern-based approaches.

(Try and see ...)

+1
source share

If you want to run the html version of the file, try using this code.

  webview_data.loadDataWithBaseURL("file:///android_asset/", htmlString,"text/html", "UTF-8",null); 

And make sure you want to use any css or javascript file, and then put it in the android assault folder.

+4
source share

There are really better ways to generate HTML pages in Android than manually concatenating strings. There are a few sleek Java libraries that will do this for you (recall that you can easily use regular Java libraries in your Android project).

+1
source share
  • Create MyHtml File

    Myhtml.html

     <html> <body> <form name="Home" method="POST" action='' > <input id='Name' type='hidden' value="MyName" name='Name'/> <input id='FirstName' type='hidden' value="MyFName" name='FirstName'/> . . . </form> <script language='javascript'> //java script </script> </body> </html> 
  • stored in the Assets folder as MyHtml.html

  • Read and update this file in my application as shown below.

     InputStream is = getAssets().open("MyHtml.html"); int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); is.close(); String str = new String(buffer); str = str.replace("MyName", "James Bala"); str = str.replace("MyFName", "James"); . . . 
  • then i uploaded my html file to webView as below

     myWebView.loadData(str, "text/html; charset=utf-8", "UTF-8"); 

As Stephen’s advice, I finally finished this decision. Thanks @Stephen

0
source share

All Articles