开发者

How can I load an HTML template into a WebView?

开发者 https://www.devze.com 2023-03-31 18:01 出处:网络
Imagine an HTML template that looks like this: <html> <head> <style type=\"text/css\">

Imagine an HTML template that looks like this:

<html>
<head>
    <style type="text/css">
        body {
            color: white;
        }
    </style>
</head>
<body>
The changes include:
<br/><br/>
%1$s
<br/><br/>
Would you like to update now?
</body>
</html>

I know I can load the contents into a WebView with webView.loadUrl("file:///android_asset/html/upgrade.html"). But I also want to replace the $1 at runtime, just as if I were loading it from an XML file using String.format().

Is it pos开发者_如何转开发sible to load the file contents into a String first so I can run String.format() on it and then use webView.loadData() instead?


I came up with a solution based on this answer to a similar question:

String prompt = "";
AssetManager assetManager = getApplicationContext().getResources().getAssets();
try {
    InputStream inputStream = assetManager.open("html/upgrade-alert.html");
    byte[] b = new byte[inputStream.available()];
    inputStream.read(b);
    prompt = String.format(new String(b),changes);
    inputStream.close();
} catch (IOException e) {
    Log.e(LOGTAG, "Couldn't open upgrade-alert.html", e);
}

WebView html = new WebView(this);
html.loadData(prompt,"text/html","utf-8");


just write whole html in some string replace one string that you want to replace and then load using loadData.

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/WebView1.html

http://sriram-iyengar.blogspot.com/2011/05/android-webview-loaddata-and-encoding.html

something like this


Well you could use HTMLCleaner to get the HTML from your WebView as a DOM and change it via TagNodes. An example how to use HTMLCLeaner to get a node an give out a certain attribute of it. You should do this in an AsyncTask of course, so that it doesn't block the ui.

public static String snapFromCleanedHTMLWithXPath(String stringURL, String xPath, String attrToStrip) {
    String snap = "";

    // create an instance of HtmlCleaner
    HtmlCleaner cleaner = new HtmlCleaner();

    // take default cleaner properties
    CleanerProperties props = cleaner.getProperties();

    props.setAllowHtmlInsideAttributes(true);
    props.setAllowMultiWordAttributes(true);
    props.setRecognizeUnicodeChars(true);
    props.setOmitComments(true);

    // open a connection to the desired URL
    URL url;
    try {
        url = new URL(stringURL);
        URLConnection conn = url.openConnection();

        // use the cleaner to "clean" the HTML and return it as a TagNode object
        TagNode root = cleaner.clean(new InputStreamReader(conn.getInputStream()));

        Object[] foundNodes = root.evaluateXPath(xPath);

        if (foundNodes.length > 0) {
            // casted to a TagNode
            TagNode foundNode = (TagNode) foundNodes[0];
            snap = foundNode.getAttributeByName(attrToStrip);
        }
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (XPatherException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    cleaner = null;
    props = null;
    url = null;
    return snap;
}

HTMLCLeaner-Website

0

精彩评论

暂无评论...
验证码 换一张
取 消