BasicAuthentication in android for webview not working

webview.setWebViewClient(new MyWebViewClient ());

private class MyWebViewClient extends WebViewClient {
    @Override
    public void onReceivedHttpAuthRequest(WebView view,
            HttpAuthHandler handler, String host, String realm) {
        handler.proceed("[email protected]", "mypassword");
    }
}

This is most voted solution there I am not sure where to set the URL to open.Please suggest.


Not sure if this will help someone, but I implemented the onReceivedHttpAuthRequest function(like the people above) with username and password and in every device it worked normally except on Android Oreo. Then, I realized that the Oreo phone received http requests not from the main frame and it gave me 401 error.

In the onReceivedHttpError function I checked, if the http error is from the main frame with:

         `override fun onReceivedHttpError(
                view: WebView?,
                request: WebResourceRequest?,
                errorResponse: WebResourceResponse?
            ) {
                super.onReceivedHttpError(view, request, errorResponse)
                if(request.isForMainFrame){ //do smt. with the error}
               }`

Then it started to work normally.