How to create/update/load a HTML file at runtime

蓝咒 提交于 2020-01-20 08:29:03

问题


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 above format, i need to update value field at runtime. After updating I want to open this html file within my android application.

Update: I know we can crate HTML file like 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 wank to know is there any other way to create HTML file like creating XML file using XmlSerializer Thanks!


回答1:


How to create html file.

  1. Open file
  2. Write text characters comprising the HTML
  3. Close file

If you need more details, the Oracle Java Tutorial has lessons on basic Java constructs, and on how to do simple file I/O.


There are other more "elegant" ways of doing this kind of thing in different contexts (e.g. in a web service) but if this the sum total of what you need to do, then ... keep it simple.


... is there any other way to create HTML file like creating XML file using XmlSerializer.

Not HTML. You could create XHTML that way through.

Having said that, constructing HTML or XHTML that way sounds like a poor idea. The resulting code would be neither readable or efficient ... compared with string concatenation or template-based approaches.

(Try it and see ...)




回答2:


If you want to run html file runtime then try using this code.

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

And make sure if you want to use any css or javascript file then put it into android assest folder..




回答3:


There are indeed better ways of generating HTML pages in Android than manual string concatenation. There are a few slick Java libraries that will do it for you (recall that you can use regular Java libraries within your Android project pretty easily).

  • RenderSnake: http://rendersnake.org/index.html
  • FreeMarker Java: http://freemarker.org/



回答4:


  1. 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>
    
  2. stored in Assets folder as MyHtml.html

  3. Read and update this file in my app like 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");
        .
        .
        .
    
  4. then i loaded my html file in webView like below

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

As Stephen's advice finally I ended with this solutions. Thanks @Stephen



来源:https://stackoverflow.com/questions/18416182/how-to-create-update-load-a-html-file-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!