安卓 WebView无法访问天猫 net::ERR_UNKNOWN_URL_SCHEME

本文介绍了解决Android WebView无法加载本地资源的问题方案。通过设置WebViewClient并自定义shouldInterceptRequest方法,实现了对特定URL的拦截并正确加载本地资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

或者出现:

Not allowed to load local resource: file:///android_asset/webkit/android-weberror.png

在中文网找了,各种加了以下代码

		WebSettings webSettings = mWebView.getSettings();
		webSettings.setJavaScriptCanOpenWindowsAutomatically(true);// 设置js可以直接打开窗口,如window.open(),默认为false
		webSettings.setJavaScriptEnabled(true);// 是否允许执行js,默认为false。设置true时,会提醒可能造成XSS漏洞
		webSettings.setSupportZoom(true);// 是否可以缩放,默认true
		webSettings.setBuiltInZoomControls(true);// 是否显示缩放按钮,默认false
		webSettings.setUseWideViewPort(true);// 设置此属性,可任意比例缩放。大视图模式
		webSettings.setLoadWithOverviewMode(true);// 和setUseWideViewPort(true)一起解决网页自适应问题
		webSettings.setAppCacheEnabled(true);// 是否使用缓存
		webSettings.setDomStorageEnabled(true);// DOM Storage

都不行


然后又找了一圈,终于在外文网发现了答案,加上去后,我是可以访问天猫了,各位也有这个问题的同鞋可以一试


// Injection token as specified in HTML source
private static final String INJECTION_TOKEN = "**injection**";

webView.setWebViewClient(new WebViewClient() {

    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
        WebResourceResponse response = super.shouldInterceptRequest(view, url);
        if(url != null && url.contains(INJECTION_TOKEN)) {
            String assetPath = url.substring(url.indexOf(INJECTION_TOKEN) + INJECTION_TOKEN.length(), url.length());
            try {
                response = new WebResourceResponse(
                        "application/javascript",
                        "UTF8",
                        getContext().getAssets().open(assetPath)
                );
            } catch (IOException e) {
                e.printStackTrace(); // Failed to load asset file
            }
        }
        return response;
    }
});

英文不怎么好,不过看代码感觉大体是解决了那个出错的读取资源问题,从而解决了整个问题,有兴趣的同学可以去看原帖

http://stackoverflow.com/questions/19997146/kitkat-kills-not-allowed-to-load-local-resource-file-android-asset-webkit-a/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值