webview加载本地html文件里有外部网址链接,链接不对。

时间:2021-01-18 06:09:42
加载assets下的html我用的是  webView.loadUrl("file:///android_asset/index.html");  
当indel.html里含有  <img src=\"http://。。。网络地址。。。"/> 时,运行时地址就变成了  file://http://。。。网络地址。。。,怎么去掉“file://”呢?

4 个解决方案

#1


 <img src=\"http:// 这部分的\是干嘛用的?去掉看看

#2


引用 1 楼 jhg1204 的回复:
 <img src=\"http:// 这部分的\是干嘛用的?去掉看看
图片的网络地址

#3


没人吗???

#4


已解决,webview里有一个方法,shouldInterceptRequest ,
 webView.setWebViewClient(new WebViewClient(){

            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
                Log.i("LOGTAG", "shouldInterceptRequest url=" + url + ";threadInfo" + Thread.currentThread());
                WebResourceResponse response = null;
                if (url.contains("判断条件")) {
                    try {
                       URL u= new URL(url.replace("file:///","").replace("%22",""));//把file://替换掉
                        HttpURLConnection conn = (HttpURLConnection)u.openConnection();
                        InputStream inputStream = conn.getInputStream();
                        response = new WebResourceResponse("image/png", "UTF-8", inputStream);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return response;
            }
        });

webView.loadUrl("file:///android_asset/test.html");

#1


 <img src=\"http:// 这部分的\是干嘛用的?去掉看看

#2


引用 1 楼 jhg1204 的回复:
 <img src=\"http:// 这部分的\是干嘛用的?去掉看看
图片的网络地址

#3


没人吗???

#4


已解决,webview里有一个方法,shouldInterceptRequest ,
 webView.setWebViewClient(new WebViewClient(){

            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
                Log.i("LOGTAG", "shouldInterceptRequest url=" + url + ";threadInfo" + Thread.currentThread());
                WebResourceResponse response = null;
                if (url.contains("判断条件")) {
                    try {
                       URL u= new URL(url.replace("file:///","").replace("%22",""));//把file://替换掉
                        HttpURLConnection conn = (HttpURLConnection)u.openConnection();
                        InputStream inputStream = conn.getInputStream();
                        response = new WebResourceResponse("image/png", "UTF-8", inputStream);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return response;
            }
        });

webView.loadUrl("file:///android_asset/test.html");