简体   繁体   English

Android WebView loadDataWithBaseURL shouldInterceptRequest

[英]Android WebView loadDataWithBaseURL shouldInterceptRequest

I am using a WebView to display an HTML string with img tags.我正在使用 WebView 来显示带有 img 标签的 HTML 字符串。 These tags must display pictures stored in a ZIP archive, so I need to intercept the images requests to return a ZipInputStream.这些标签必须显示存储在 ZIP 存档中的图片,因此我需要拦截图片请求以返回 ZipInputStream。

I'm thus using loadDataWithBaseURL and shouldInterceptRequest, but shouldInterceptRequest is never called for my pictures requests.因此,我使用 loadDataWithBaseURL 和 shouldInterceptRequest,但我的图片请求从未调用过 shouldInterceptRequest。 Here's my WebViewClient client:这是我的 WebViewClient 客户端:

webView.webViewClient = object : WebViewClient() {

    override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest?): WebResourceResponse? {
        println("shouldInterceptRequest1 url: ${request?.url}")
        return super.shouldInterceptRequest(view, request)
    }

    override fun shouldInterceptRequest(view: WebView?, url: String?): WebResourceResponse? {

        println("shouldInterceptRequest2 url: $url")

        @Suppress("DEPRECATION")
        return super.shouldInterceptRequest(view, url)
    }
}

And here's what I do to load the HTML:这就是我加载 HTML 的方法:

webView.loadDataWithBaseURL(null, "<div><img src='file://path/to/my/image.png'/></div>", "text/html", "UTF-8", null)

All I get in the logcat is this:我在 logcat 中得到的只是:

shouldInterceptRequest1 url: data:text/html;charset=utf-8;base64, shouldInterceptRequest1 url:数据:文本/html;字符集=utf-8;base64,

shouldInterceptRequest2 url: data:text/html;charset=utf-8;base64, shouldInterceptRequest2 url:数据:文本/html;字符集=utf-8;base64,

The img request does not trigger shouldInterceptRequest. img 请求不会触发 shouldInterceptRequest。

What's wrong?怎么了?

Despite the doc says this:尽管医生这样说:

This callback is invoked for a variety of URL schemes (eg, http(s):, data:, file:, etc.), not only those schemes which send requests over the network.此回调被各种 URL 方案(例如,http(s):、data:、file: 等)调用,而不仅仅是那些通过网络发送请求的方案。 This is not called for javascript: URLs, blob: URLs, or for assets accessed via file:///android_asset/ or file:///android_res/ URLs.对于 javascript: URLs、blob: URLs 或通过 file:///android_asset/ 或 file:///android_res/ URLs 访问的资产,不需要这样做。

In my very basic sample code shouldInterceptRequest isn't called for file:// URIs when using loadDataWithBaseURL... Using a http:// scheme works.在我非常基本的示例代码中,在使用 loadDataWithBaseURL 时不为 file:// URI 调用 shouldInterceptRequest ... 使用 http:// 方案有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM