通过webview android中的代理加载所有资源

时间:2013-06-24 08:51:07

标签: java android webview

如何使用来自webviewClient的onLoadResource()回调从android webview动态加载资源?

以下是我到目前为止编写的代码。当我获得新资源时,它会加载一个单一想象的整个页面,而不是更新并在网页的同一个原始网址上显示图像。

如果网页上有5个图片和文字,那么每次onLoadResource尝试加载图片时,我当前的代码都会加载5页。

我想要它做的是将图像加载到同一页面和任何其他资源,如JS,jquery等。

@Override
    public void onLoadResource(WebView view, String url) {
   addRequestToProxy(url);

}


public void addRequestToProxy(String url){
//pass url to proxy and wait for respoonse
String response;
//handle response

if(mime-type == IMAGE){
String urlStr = "http://example.com/my.jpg";
        String pageData = "<img src=\"data:" + contentResponse.getMimeType()
                + ";base64," + contentResponse.getContent() + "\" />";

        mWebView.loadDataWithBaseURL(urlStr, pageData, "text/html", null,
                urlStr);
}else{

mWebView.loadDataWithBaseURL(null, response, "text/html", null,
                        null);
}

1 个答案:

答案 0 :(得分:1)

而不是覆盖onLoadResource(),最好这样做 - shouldInterceptRequest()

webview shouldInterceptRequest example