Search code examples
androidandroid-webviewwebchromeclient

Get loaded resource files list from android webview


Is there any way to get the currently successfully loaded and not loaded files from URL into android webview. That is, if I am loading the following,

  • 1.js, 2.js, 3.js
  • 1.css, 2.css, 3.css

in html page and load the html file into android webview.

I need to list the loaded files in webview and also which are unable to load into android webview. I tried using the onConsoleMessage() from WebchromeClient which detects only the console error alone. This method not getting the error messages for css image.

Is this possible to list the files from URL?


Solution

  • You can get the list of resources the WebView is trying to load by overriding WebViewClient.shouldInterceptRequest like so:

    class MyWebViewClient extends WebViewClient {
    
        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
            android.util.Log.i("MyWebViewClient", "attempting to load resource: " + url);
            return null;
        }
    
        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
            android.util.Log.i("MyWebViewClient", "attempting to load resource: " + request.getUrl());
            return null;
        }
    

    }

    Remember that shouldInterceptRequest is called on a background thread and so you need synchronize access to any shared data structures.

    There is no Java API to find out whether the WebView has successfully loaded a given resource though.