简体   繁体   English

如何将网页另存为网页存档,然后使用Android的WebView将网页存档显示为网页?

[英]How to save webpage as web archive and then display the web archive as a webpage using Android's WebView?

I am developing an Android application in Android Studio 3.0.1, Xubuntu 16.04 LTS, using a WebView as a wrapper for a responsive web app that contains HTML, CSS, JavaScript/jQuery and uses PHP/MySQL for the backend. 我正在Android Studio 3.0.1,Xubuntu 16.04 LTS中开发一个Android应用程序,使用WebView作为包含HTML,CSS,JavaScript / jQuery并使用PHP / MySQL作为后端的响应式Web应用程序的包装。 Almost everything is working as expected: the pages fully render, JavaScript runs and the sign-up/login/news-feed systems work as they should. 几乎所有内容都按预期工作:页面完全呈现,JavaScript运行,并且sign-up / login / news-feed系统按预期工作。

But now I would like to implement some kind of simple caching system, so that the user can still see the last available version of the pages when there's no Internet connection. 但是,现在我想实现某种简单的缓存系统,以便在没有Internet连接时用户仍可以看到页面的最新可用版本。

As of now, I've managed to detect whether there is Internet connection; 截至目前,我已经设法检测是否存在Internet连接。 In case there's no connection, the WebView displays a local "Error" page, stored in the app files - which is better than the default Android "Connection Error" screen, but still the user can't see the images and text posts from the news feed when offline like in the Facebook app. 如果没有连接,WebView将显示一个本地“错误”页面,该页面存储在应用程序文件中-比默认的Android“连接错误”屏幕要好,但用户仍然无法从该页面看到图像和文本。离线时的动态消息,例如在Facebook应用中。

What I have tried (Java/Android Studio): 我尝试过的方法(Java / Android Studio):

When the user opens the app (onCreate) : 当用户打开应用程序(onCreate)时

  • If there's no connection, check for a locally saved version of the page it's trying to retrieve; 如果没有连接,请检查要检索的页面的本地保存版本。 If there is connection, save the page as a .mht file for the next time: 如果存在连接,则下次将页面另存为.mht文件:

     if (!isConnected()) { try { File file = new File(getFilesDir(), FilenameUtils.getBaseName(url)); webView.loadUrl("file://" + file.getAbsolutePath() + ".mht"); } catch (Exception e) { Toast.makeText(getApplicationContext(), "Exception detected", Toast.LENGTH_LONG).show(); } } else { try { File file = new File(getFilesDir(), FilenameUtils.getBaseName(url)); webView.loadUrl(url); webView.saveWebArchive("file://" + file.getAbsolutePath() + ".mht"); } catch (Exception e) { Toast.makeText(getApplicationContext(), "Exception detected", Toast.LENGTH_LONG).show(); } } 

I have tried using webView.saveWebArchive() with different paths, including simple /path/to/file , file:///path/to/file , etc., to no avail. 我尝试将webView.saveWebArchive()用于不同的路径,包括简单的/path/to/filefile:///path/to/file等,但均无济于事。 The closest I got to displaying something on the WebView was a bunch of raw MHTML, but apparently it wasn't rendered. 我最接近在WebView上显示内容的是一堆原始MHTML,但显然它并未呈现。 I also got "File not found" screens and plain white screens without anything on them. 我还得到了“找不到文件”屏幕和纯白屏幕,上面没有任何内容。

How can I properly save a web page from a WebView into a web archive, and then retrieve from the local storage and display it as a web page again? 如何正确地将WebView中的网页保存到Web存档中,然后从本地存储中检索并再次显示为网页?

Edit: 编辑:

As suggested by greenapps in the comments, I overrode the "onPageFinished" method in the WebViewClient and called "saveWebArchive()" there: 正如greenapps在评论中所建议的那样,我覆盖了WebViewClient中的“ onPageFinished”方法,并在其中调用了“ saveWebArchive()”:

@Override
public void onPageFinished(WebView view, String link) {

    view.saveWebArchive(path);
    showMsg("Finished loading");

}

When the phone is connected to the Internet and the home page loads, it does show "Finished loading". 当电话连接到Internet并加载主页时,它确实显示“完成加载”。 After closing the app, disconnecting and opening it again, it now shows a blank background with the color of the original page, no more white pages or error messages. 关闭应用程序后,断开连接并再次打开它,它现在显示的背景为空白,具有原始页面的颜色,没有更多的白色页面或错误消息。 But there aren't any images, text or anything. 但是,没有任何图像,文字或任何东西。

The problem here is that you need to wait for the page to finish loading before attempting to save it to an archive. 这里的问题是,您需要等待页面完成加载,然后再尝试将其保存到存档中。

webView.loadUrl(url);
webView.saveWebArchive(path);

won't work since the page isn't fully loaded yet. 由于该页面尚未完全加载,因此无法使用。 So you need to override the "onPageFinished" method in your WebViewClient in order to save the archive only when the page is finished: 因此,您仅在页面完成时才需要覆盖WebViewClient中的“ onPageFinished”方法以保存存档:

webView.setWebViewClient(new WebViewClient() {

    @Override
    public void onPageFinished(WebView view, String url) {

        if (isConnected()) {

            try {

                Thread.sleep(2000);
                view.saveWebArchive(path_to_saveDir);

            } catch (InterruptedException e) {

                Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();

            }

        }

    }

});

Note that I've used Thread.sleep(2000) because there is a jQuery animation when the page loads, so it waits 2 seconds for the animation to finish, then saves the web archive. 请注意,我使用Thread.sleep(2000)是因为页面加载时有一个jQuery动画,因此它要等待2秒钟才能完成动画,然后保存Web存档。

webView.loadUrl(url); 
webView.saveWebArchive("file://" + file.getAbsolutePath() + ".mht");. 

You cannot save the page there. 您无法在此处保存页面。

Too early. 太早了。

The page is not yet loaded. 该页面尚未加载。

Do it in onPageLoaded() or in onPageFinished(). 在onPageLoaded()或onPageFinished()中进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM