【问题标题】:cacheing of webpage for offline use (android)缓存网页以供离线使用 (android)
【发布时间】:2016-06-07 19:56:47
【问题描述】:

我正在关注来自堆栈溢出的this 问题以进行我的查询。该代码工作正常,但在离线模式下它不会加载任何页面,而是显示没有互联网连接。

我怀疑网页是否真的被写入缓存。

我的代码如下-

private void openURL() {
    webView.getSettings().setAppCacheMaxSize(5 * 1024 * 1024); // 5MB
    webView.getSettings().setAppCachePath(getApplicationContext().getCacheDir().getAbsolutePath());
    webView.getSettings().setAllowFileAccess(true);
    webView.getSettings().setAppCacheEnabled(true);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);

    if (!isNetworkAvailable()) {
        webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        Toast.makeText(MainActivity.this, "server offline. loading the cached website", Toast.LENGTH_LONG).show();
    }

    webView.loadUrl("http://www.google.com");
    webView.requestFocus();
}

我正在显示的 toast 在没有互联网的情况下显示。所以代码工作正常,但页面没有被加载。

logcat

W/chromium: [WARNING:aw_network_delegate.cc(75)]    http://192.168.1.210/trackme/user/sendpagestest#-102#1
[INFO:browser_view_renderer.cc(185)] [CalculateDesiredMemoryPolicy]   [58982400][180]
register, handle(0xb8d834b0) (w:720 h:1280 s:720 f:0x1 u:0x000f02)
cache file failed CRC check
[INFO:CONSOLE(12)] "Not allowed to load local resource:    file:///android_asset/webkit/android-weberror.png", source:   data:text/html,chromewebdata (12)
[getaddrinfo]: hostname=192.168.1.210; servname=(null); cache_mode=(null), netid=0; mark=0
[getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=4; ai_family=0
[INFO:SkUtilsArm.cpp(179)] Device supports ARM NEON instructions!
E/SelfBrailleClient: Failed to bind to service
E/SelfBrailleClient: Failed to bind to service
D/OpenGLRenderer: Flushing caches (mode 0)
D/OpenGLRenderer: Flushing caches (mode 0)
D/GraphicBuffer: unregister, handle(0xb8d63a28) (w:583 h:88 s:592  f:0x1 u:0x000f02)
E/SelfBrailleClient: Failed to bind to service
E/SelfBrailleClient: Failed to bind to service
E/SelfBrailleClient: Failed to bind to service
D/WebView: loadUrl=http://192.168.1.210/trackme/user/sendpagestest
W/chromium: [WARNING:aw_network_delegate.cc(75)] http://192.168.1.210/trackme/user/sendpagestest#-102#1
I/chromium: [INFO:CONSOLE(12)] "Not allowed to load local resource: file:///android_asset/webkit/android-weberror.png", source: data:text/html,chromewebdata (12)
D/libc-netbsd: [getaddrinfo]: hostname=192.168.1.210; servname=(null); cache_mode=(null), netid=0; mark=0
D/libc-netbsd: [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=4; ai_family=0

请帮帮我。

谢谢

更新

我刚刚检查了 google.com,它工作正常。但是当我使用 facebook.com 进行操作时,就会出现同样的问题。

【问题讨论】:

  • 您的 LogCat 显示 webview 加载 http://192.168.1.210/trackme/user/sendpagestest 并说“缓存文件未通过 CRC 检查”。你能显示它的 HTML 源代码吗?也许页面中有一些东西阻止了 webview 缓存它。
  • 感谢您的回复。但我不能理解你。你要看什么。网页html还是什么?
  • WebView 无法读取/写入缓存文件,可能是因为权限不正确,也可能是您的 Web 服务器无法使用 HTTP 304 回复。
  • 我不能明确地将响应设为 304。

标签: android caching webpage


【解决方案1】:

据我所知,如果 Web 服务器回复 HTTP 304(未修改),WebView 将从缓存加载。它仍然需要将 HTTP GET 发送到 Web 服务器,并且需要网络连接。

【讨论】:

  • 那么我现在该怎么办。请提出建议。
  • 有什么方法可以明确提供http响应代码为304。请回复。
  • 如果你使用 Apache,你可以使用 mod_expires。见httpd.apache.org/docs/2.2/mod/mod_expires.html
  • 不,当然。我的意思是托管此 URL http://192.168.1.210/trackme/user/sendpagestest 的 Web 服务器
【解决方案2】:

在做了一些研究之后,我找到了我的问题的答案。

我明确将网页下载到设备中并保存。当设备处于离线模式时,我会显示保存在设备中的网页。当它在线时,再次下载页面并覆盖前一个页面。

我使用了以下代码-

下载

InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
       URL url = new URL("http://192.168.1.210/bibhutest.html");
       connection = (HttpURLConnection) url.openConnection();
       connection.connect();
            int fileLength = connection.getContentLength();
            input = connection.getInputStream();
            output = new FileOutputStream("address at which you want to download file");
            byte data[] = new byte[4096];
            long total = 0;
            int count;
            while ((count = input.read(data)) != -1) {
                if (isCancelled()) {
                    input.close();
                    return null;
                }
                total += count;
                output.write(data, 0, count);
            }
                if (output != null)
                    output.close();
                if (input != null)
                    input.close();
            if (connection != null)
                connection.disconnect();

显示下载的文件

if (!isNetworkAvailable()) {
// from the device when dont have internet
webView.loadUrl("file:////sdcard/android/data/downloads.html");
webView.requestFocus();
}

if (!isNetworkAvailable()) {
//load directly from the internet
webView.loadUrl(url);
webView.requestFocus();
}

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

根据自己的需要使用上面的代码,并调整它何时下载文件和保存文件以及如何显示。在您的代码中应用适当的逻辑。

谢谢

【讨论】:

  • 如何保存网页图片。通过这种方式我们可以保存文件但图像不可见。
猜你喜欢
  • 2013-05-25
  • 2019-06-13
  • 2012-12-07
  • 2013-07-03
  • 2018-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多