如何在 Web 视图中隐藏或禁止身份验证弹出窗口



我做了一个经过身份验证的搜索网络视图,它在登录时通过用户名和密码进行身份验证。每次我通过搜索进入网络视图时,它都会弹出已登录用户的身份验证消息(我相信错误代码 410)。如何抑制或隐藏相同内容?

我应该使用以下代码吗?

public void onReceivedError( WebView view, int errorCode, String description, String failingUrl ) {
    Toast.makeText(view.getContext(), "Authentication Error", Toast.LENGTH_LONG).show();
    if (errorCode == 410) {
        //webview.setHttpAuthUsernamePassword(host, realm, username, password);
        // Show alert to enter username and password.
        // Then, when those are entered in the alert,
        // set it through the setHttpAuthUsernamePassword(...)
        // shown below and then reload the site.
    }
    super.onReceivedError(view, errorCode, description, failingUrl);
}

如果是这样,我该如何使用代码抑制或隐藏弹出窗口?

以下实现的onReceivedHttpAuthRequest方法添加到 WebViewClient 类中:

....
    public void onReceivedError( WebView view, int errorCode, String description, String failingUrl ) {
        ....
    }
    @Override
    public void onReceivedHttpAuthRequest( WebView view, final HttpAuthHandler handler, final String host, final String realm) {
            // Replace SAVED_USERNAME and SAVED_PASSWORD with your authentication details.
            handler.proceed(SAVED_USERNAME,SAVED_PASSWORD);
    }
}

请参阅 onReceivedHttpAuthRequest 的文档页面

最新更新