android webview 打开远程页面时,怎样把某些资源文件的request url 替换为本地assets资源。
怪我咯
怪我咯 2017-04-17 12:03:58
0
2
587

目的是为了提高加载速度。比如jquery可以预先放置在app assets。把远程页面中的/sss/jquery.js文件在发起请求前映射到本地android_asset/jquery.js文件。

在ios中我可以用 LocalSubstitutionCache.mappingDict 做到。在android中一直找不到解决方案。

前提是不能修改远程页面的代码,不能在远程页面中使用“file:///android_asset”之类的资源路径。

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(2)
刘奇

WebView provides a callback from API 11 to handle similar requirements. i.e. shouldInterceptRequest

 public WebResourceResponse shouldInterceptRequest(WebView view,  String url) {
    Log.i(LOGTAG, "shouldInterceptRequest url=" + url + ";threadInfo" + Thread.currentThread());
    WebResourceResponse response = null;
    if (url.contains("logo")) {
        try {
            InputStream localCopy = getAssets().open("droidyue.png");
            response = new WebResourceResponse("image/png", "UTF-8", localCopy);
        } catch (IOException e) {
            e.printStackTrace();
        }        
    }
    return response;
}    

For details, please refer to this article. WebView in Android intercepts and replaces network request data

伊谢尔伦

How do you load the files in the assets directory in your webview page? ? ?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template