Android WebView -> Display WebArchive
博客专区 > simpower 的博客 > 博客详情
Android WebView -> Display WebArchive
simpower 发表于3年前
Android WebView -> Display WebArchive
  • 发表于 3年前
  • 阅读 480
  • 收藏 0
  • 点赞 0
  • 评论 0


Android's WebView has this saveWebArchive method since API level 11:

It can save entire websites as webarchives, which is great! But how do I get the downloaded contents back into a webview? I tried


But that only displays xml on the screen.

android webview webarchive

share|improve this question

asked Oct 13 '12 at 10:02

Jouke Waleson

add a comment

2 Answers


up vote16down voteaccepted

Update Feb. 21, 2014

My answer posted below does not apply to web archive files saved under Android 4.4 KitKat and newer. The saveWebArchive() method of WebView under Android 4.4 "KitKat" (and probably newer versions too) does not save the web archive in XML code that this reader code posted below. Instead it saves pages in MHT (MHTML) format. It is easy to read back the .mht files - just use:


That's all, much easier than the previous method, and compatible with other platforms.

Previously posted

I needed it myself, and everywhere I searched, there were unanswered questions like this. So I had to work it out myself. Below is my little WebArchiveReader class and sample code on how to use it. Please note that despite the Android docs declaring that shouldInterceptRequest() was added to WebViewClient in API11 (Honeycomb), this code works and was tested successfully in Android emulators down to API8 (Froyo). Below is all the code that's needed, I also uploaded the full project to GitHub repository at


package com.hyperionics.war_test;import android.util.Base64;import android.webkit.WebResourceResponse;import android.webkit.WebView;import android.webkit.WebViewClient;import org.w3c.dom.*;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import;import;import java.util.ArrayList;public abstract class WebArchiveReader {
    private Document myDoc = null;
    private static boolean myLoadingArchive = false;
    private WebView myWebView = null;
    private ArrayList<String> urlList = new ArrayList<String>();
    private ArrayList<Element> urlNodes = new ArrayList<Element>();

    abstract void onFinished(WebView webView);

    public boolean readWebArchive(InputStream is) {
        DocumentBuilderFactory builderFactory =
        DocumentBuilder builder = null;
        myDoc = null;
        try {
            builder = builderFactory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
        try {
            myDoc = builder.parse(is);
            NodeList nl = myDoc.getElementsByTagName("url");
            for (int i = 0; i < nl.getLength(); i++) {
                Node nd = nl.item(i);
                if(nd instanceof Element) {
                    Element el = (Element) nd;
                    // siblings of el (url) are: mimeType, textEncoding, frameName, data
                    NodeList nodes = el.getChildNodes();
                    for (int j = 0; j < nodes.getLength(); j++) {
                        Node node = nodes.item(j);
                        if (node instanceof Text) {
                            String dt = ((Text)node).getData();
                            byte[] b = Base64.decode(dt, Base64.DEFAULT);
                            dt = new String(b);
                            urlNodes.add((Element) el.getParentNode());
        } catch (Exception e) {
            myDoc = null;
        return myDoc != null;

    private byte [] getElBytes(Element el, String childName) {
        try {
            Node kid = el.getFirstChild();
            while (kid != null) {
                if (childName.equals(kid.getNodeName())) {
                    Node nn = kid.getFirstChild();
                    if (nn instanceof Text) {
                        String dt = ((Text)nn).getData();
                        return Base64.decode(dt, Base64.DEFAULT);
                kid = kid.getNextSibling();
        } catch (Exception e) {
        return null;

    public boolean loadToWebView(WebView v) {
        myWebView = v;
        v.setWebViewClient(new WebClient());
        WebSettings webSettings = v.getSettings();

        myLoadingArchive = true;
        try {
            // Find the first ArchiveResource in myDoc, should be <ArchiveResource>
            Element ar = (Element) myDoc.getDocumentElement().getFirstChild().getFirstChild();
            byte b[] = getElBytes(ar, "data");

            // Find out the web page charset encoding
            String charset = null;
            String topHtml = new String(b).toLowerCase();
            int n1 = topHtml.indexOf("<meta http-equiv=\"content-type\"");
            if (n1 > -1) {
                int n2 = topHtml.indexOf('>', n1);
                if (n2 > -1) {
                    String tag = topHtml.substring(n1, n2);
                    n1 = tag.indexOf("charset");
                    if (n1 > -1) {
                        tag = tag.substring(n1);
                        n1 = tag.indexOf('=');
                        if (n1 > -1) {
                            tag = tag.substring(n1+1);
                            tag = tag.trim();
                            n1 = tag.indexOf('\"');
                            if (n1 < 0)
                                n1 = tag.indexOf('\'');
                            if (n1 > -1) {
                                charset = tag.substring(0, n1).trim();

            if (charset != null)
                topHtml = new String(b, charset);
                topHtml = new String(b);
            String baseUrl = new String(getElBytes(ar, "url"));
            v.loadDataWithBaseURL(baseUrl, topHtml, "text/html", "UTF-8", null);
        } catch (Exception e) {
            return false;
        return true;

    private class WebClient extends WebViewClient {
        public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
            if (!myLoadingArchive)
                return null;
            int n = urlList.indexOf(url);
            if (n < 0)
                return null;
            Element parentEl = urlNodes.get(n);
            byte [] b = getElBytes(parentEl, "mimeType");
            String mimeType = b == null ? "text/html" : new String(b);
            b = getElBytes(parentEl, "textEncoding");
            String encoding = b == null ? "UTF-8" : new String(b);
            b = getElBytes(parentEl, "data");
            return new WebResourceResponse(mimeType, encoding, new ByteArrayInputStream(b));

        public void onPageFinished(WebView view, String url)
            // our WebClient is no longer needed in view
            myLoadingArchive = false;

Here is how to use this class, sample class:

package com.hyperionics.war_test;import;import android.os.Bundle;import android.webkit.WebView;import android.webkit.WebViewClient;import;import;public class MyActivity extends Activity {

    // Sample WebViewClient in case it was needed...
    // See continueWhenLoaded() sample function for the best place to set it on our webView
    private class MyWebClient extends WebViewClient {
        public void onPageFinished(WebView view, String url)
            Lt.d("Web page loaded: " + url);

    public void onCreate(Bundle savedInstanceState) {
        WebView webView = (WebView) findViewById(;
        try {
            InputStream is = getAssets().open("TestHtmlArchive.xml");
            WebArchiveReader wr = new WebArchiveReader() {
                void onFinished(WebView v) {
                    // we are notified here when the page is fully loaded.
            // To read from a file instead of an asset, use:
            // FileInputStream is = new FileInputStream(fileName);
            if (wr.readWebArchive(is)) {
        } catch (IOException e) {

    void continueWhenLoaded(WebView webView) {
        Lt.d("Page from WebArchive fully loaded.");
        // If you need to set your own WebViewClient, do it here,
        // after the WebArchive was fully loaded:
        webView.setWebViewClient(new MyWebClient());
        // Any other code we need to execute after loading a page from a WebArchive...

To make things complete, here is my little class for debug output:

package com.hyperionics.war_test;import android.util.Log;public class Lt {
    private static String myTag = "war_test";
    private Lt() {}
    static void setTag(String tag) { myTag = tag; }
    public static void d(String msg) {
        // Uncomment line below to turn on debug output
        Log.d(myTag, msg == null ? "(null)" : msg);
    public static void df(String msg) {
        // Forced output, do not comment out - for exceptions etc.
        Log.d(myTag, msg == null ? "(null)" : msg);

Hope this is helpful.

Update July 19, 2013

Some web pages don't have meta tag specifying text encoding, and then the code we show above does not display the characters correctly. In the GitHub version of this code I now added charset detection algorithm, which guesses the encoding in such cases. Again, see


share|improve this answer

edited Feb 21 at 22:27

answered Nov 18 '12 at 23:34



Fantastic! I hoped there would be a built in solution, but lacking that, this seems a solid alternative. Thanks!–  Jouke Waleson Nov 20 '12 at 7:12

Thank you, Jouke! I'm glad I was able to post something useful. I also uploaded the sample project to –  gregko Nov 25 '12 at 13:39

thanks for your answer but saveWebArchive is available from api 11, i want to support save webpage form api 8 and higher. Please help me if you have any solution regarding this. I saw your code works fine – Antarix May 14 '13 at 10:17

Actually my code posted above does not use saveWebArchive(), so it could be used even under API 8 to read archives saved elsewhere. I don't have a good solution for saveWebArchive() under older platforms, other than look at Android WebView source code to see if you could copy and adapt the code for this function for older platforms yourself. If you succeed, please post it for all of us as well! –  gregko May 14 '13 at 16:02

@gregko thanks your git project is really helpful,you saved my lot of time :) –  Tombeau Nov 28 '13 at 4:36 

show 6 more comments

up vote7down vote

I've found an undocumented way of reading saved webarchive. Just do: String raw_data = (read the mywebarchivefile as a string) and then call

webview.loadDataWithBaseURL(mywebarchivefile, raw_data, "application/x-webarchive-xml", "UTF-8", null);

The reference:

Available from Android 3.0, api level 11.

share|improve this answer

answered Nov 20 '13 at 0:06

Tomasz Jarosik

This worked like a charm for me. Thanks for this. –  Espen Riskedal Dec 14 '13 at 16:31

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 22
博文 356
码字总数 8015
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
* 支付类型