简体   繁体   English

Android WebViewClient / WebViewAssetLoader 实现错误

[英]Android WebViewClient / WebViewAssetLoader implementation error

I am trying to load a webpage in asset folder and app's local storage using the webasset loader using the code provided by google.我正在尝试使用 google 提供的代码使用 webasset 加载器在资产文件夹和应用程序的本地存储中加载网页。

    final WebViewAssetLoader assetLoader = new WebViewAssetLoader.Builder()
                .addPathHandler("/assets/", new WebViewAssetLoader.AssetsPathHandler(this))
                .build();

        myWebView = (WebView) findViewById(R.id.webview);
        myWebView.setWebViewClient(new WebViewClient() {
            @Override
            @RequiresApi(21)
            public WebResourceResponse shouldInterceptRequest(WebView view,
                                                              WebResourceRequest request) {
                return assetLoader.shouldInterceptRequest(request.getUrl());
            }
//This gives the error: shouldInterceptRequest(WebView, WebResourceRequest)' is already
   defined in 'Anonymous class derived from android.webkit.WebViewClient

            @Override
            @SuppressWarnings("deprecation") // for API < 21
            public WebResourceResponse shouldInterceptRequest(WebView view,
                                                              WebResourceRequest request) {
                return assetLoader.shouldInterceptRequest(Uri.parse(request));
//This gives an error for the request variable. It says : Required type: String and
   Provided: WebResourceRequest. When I cast it as a string type, it
   gives an error similar to first error.
            }
        });

Can someone please guide me here?有人可以在这里指导我吗? What am i doing wrong here, i am using the exact code given by google.我在这里做错了什么,我使用的是谷歌给出的确切代码。 Any tutorial on using this class?有关使用此 class 的任何教程?

The snippet you found in the documentation is wrong, and I have already opened an issue for it.您在文档中找到的片段是错误的,我已经为它打开了一个问题

The problem is that the second method's signature, which refers to a deprecated implementation, should instead be:问题是第二种方法的签名,它引用了一个不推荐的实现,应该是:

public WebResourceResponse shouldInterceptRequest(WebView view, String url) {

Use this and it'll compile (and work) as expected.使用它,它会按预期编译(和工作)。

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

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