如何处理 API21 中的 shouldInterceptRequest 参数更改?

How to handle shouldInterceptRequest parameter change in API21?

在 API21 Google 中修改了 shouldInterceptRequest 方法以使用 WebResourceRequest request 而不是 String url。 有什么办法可以写一个通用的 class 扩展 WebViewClient 并处理这两种方法吗? 我的最低 API 版本是 18。

谢谢 克里斯蒂安

Google modified shouldInterceptRequest method to use WebResourceRequest request instead of String url

不,他们添加了一个secondshouldInterceptRequest()方法。两者都在 API 级别 21+ 中可用; String 变体可用于 API 11+ 级。虽然 String 标记为已弃用,但为了向后兼容,String 变体应该在相当长的一段时间内得到支持。

Is there any way I could write a generic class extending WebViewClient and handle both methods?

WebResourceRequest 版本 shouldInterceptRequest() 的内置实现只是调用 shouldInterceptRequest()String 实现:

public WebResourceResponse shouldInterceptRequest(WebView view,
        WebResourceRequest request) {
    return shouldInterceptRequest(view, request.getUrl().toString());
}

(截至目前 the source code

所以,你有两个选择:

  1. 如果您不需要 WebResourceRequest,只需覆盖 String 版本,它将用于所有相关的 API 级别。

  2. 覆盖两者,知道 WebResourceRequest 版本将用于 API 21+ 级,而 String 版本将用于 API 11-20 级。