429_WebView加载静态资源





WebView加载静态资源


    private void fetchOfflineResources () {
        AssetManager am = getAssets();
        try {
            String[] res = am.list("offline");
            if (res != null) {
                Collections.addAll(offlineResources, res);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }




            @Override
            public WebResourceResponse shouldInterceptRequest (WebView view, String url) {
                int lastSlash = url.lastIndexOf("/");
                if (lastSlash != -1) {
                    String suffix = url.substring(lastSlash + 1);
                    if (offlineResources.contains(suffix)) {
                        String mimeType;
                        if (suffix.endsWith(".js")) {
                            mimeType = "application/x-javascript";
                        } else if (suffix.endsWith(".css")) {
                            mimeType = "text/css";
                        } else {
                            mimeType = "text/html";
                        }
                        try {
                            InputStream is = getAssets().open("offline/" + suffix);
                            return new WebResourceResponse(mimeType, "UTF-8", is);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                return super.shouldInterceptRequest(view, url);
            }


            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public WebResourceResponse shouldInterceptRequest (WebView view, WebResourceRequest request) {
                String url = request.getUrl().toString();
                int lastSlash = url.lastIndexOf("/");
                if (lastSlash != -1) {
                    String suffix = url.substring(lastSlash + 1);
                    if (offlineResources.contains(suffix)) {
                        String mimeType;
                        if (suffix.endsWith(".js")) {
                            mimeType = "application/x-javascript";
                        } else if (suffix.endsWith(".css")) {
                            mimeType = "text/css";
                        } else {
                            mimeType = "text/html";
                        }
                        try {
                            InputStream is = getAssets().open("offline/" + suffix);
                            return new WebResourceResponse(mimeType, "UTF-8", is);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                WebResourceResponse response = null;
                response =  super.shouldInterceptRequest(view, request);
                return response;
            }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
加载 WebView 时,有一些注意事项可以帮助提高用户体验和保证应用的安全性。以下是一些需要注意的事项: 1. 加载进度条:在 WebView 加载页面的过程中,显示一个加载进度条可以让用户知道页面正在加载,并提供一种视觉反馈。你可以使用 Android 的 ProgressBar 或者自定义视图来实现。 2. 错误处理:WebView 可能会遇到网络错误、页面加载错误等问题,你应该处理这些错误,并向用户提供相应的提示信息,例如显示一个错误页面或者弹出一个对话框。 3. 缓存:WebView 默认会缓存已加载的页面,这样可以提高页面加载速度并减少网络流量。你可以根据需要设置缓存策略,例如只缓存静态资源或者禁用缓存。 4. 安全性:WebView 是一个容易受到攻击的组件,你需要采取一些安全措施来保护应用和用户的数据。例如,禁用 JavaScript 或者限制 JavaScript 的执行权限,防止跨域脚本攻击;验证加载的网页是否来自可信任的源;避免使用明文传输敏感数据等。 5. 用户交互:当 WebView 加载外部网页时,用户可能会与网页进行交互,例如点击链接或者填写表单。你可以通过 WebViewClient 和 WebChromeClient 来拦截网页的跳转、表单提交等操作,并根据需要进行处理。 以上是一些常见的注意事项,根据具体的需求和场景,可能还有其他需要注意的地方。在开发过程中,你可以参考 Android 官方文档和相关教程来获取更多关于 WebView 的使用技巧和最佳实践。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值