jkndmiththinda
jkndmiththinda

Reputation: 271

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.

    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?

Upvotes: 0

Views: 1606

Answers (1)

Sebastiano
Sebastiano

Reputation: 12339

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.

Upvotes: 0

Related Questions