首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >脱机使用网页的缓存(android)

脱机使用网页的缓存(android)
EN

Stack Overflow用户
提问于 2016-02-25 07:08:06
回答 2查看 3.1K关注 0票数 1

我正在跟踪来自堆栈溢出的this问题以进行查询。代码工作正常,但在脱机模式下,它不加载任何页面,而是显示没有internet连接。

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

我的代码在-下面

代码语言:javascript
复制
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();
}

我正在展示的祝酒词是在没有互联网条件下显示的。因此,代码运行良好,但没有加载页面。

logcat

代码语言:javascript
复制
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时,也会出现同样的问题。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-02-29 07:20:56

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

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

我用了以下代码-

下载

代码语言:javascript
复制
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();

以显示下载的文件

代码语言:javascript
复制
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();
}

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

谢谢

票数 0
EN

Stack Overflow用户

发布于 2016-02-25 07:17:00

据我所知,如果web服务器使用HTTP304进行应答,WebView将从缓存加载(未修改)。它仍然需要将HTTP发送到web服务器,这需要网络连接。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35620588

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档