簡體   English   中英

Android WebView loadDataWithBaseURL shouldInterceptRequest

[英]Android WebView loadDataWithBaseURL shouldInterceptRequest

我正在使用 WebView 來顯示帶有 img 標簽的 HTML 字符串。 這些標簽必須顯示存儲在 ZIP 存檔中的圖片,因此我需要攔截圖片請求以返回 ZipInputStream。

因此,我使用 loadDataWithBaseURL 和 shouldInterceptRequest,但我的圖片請求從未調用過 shouldInterceptRequest。 這是我的 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)
    }
}

這就是我加載 HTML 的方法:

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

我在 logcat 中得到的只是:

shouldInterceptRequest1 url:數據:文本/html;字符集=utf-8;base64,

shouldInterceptRequest2 url:數據:文本/html;字符集=utf-8;base64,

img 請求不會觸發 shouldInterceptRequest。

怎么了?

盡管醫生這樣說:

此回調被各種 URL 方案(例如,http(s):、data:、file: 等)調用,而不僅僅是那些通過網絡發送請求的方案。 對於 javascript: URLs、blob: URLs 或通過 file:///android_asset/ 或 file:///android_res/ URLs 訪問的資產,不需要這樣做。

在我非常基本的示例代碼中,在使用 loadDataWithBaseURL 時不為 file:// URI 調用 shouldInterceptRequest ... 使用 http:// 方案有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM