如何在Webview中隐藏或禁止身份验证弹出窗口?

时间:2013-09-30 20:57:00

标签: android android-webview

我做了一个经过身份验证的搜索webview,它在登录时通过用户名和密码进行身份验证。每次我通过搜索进入webview时,都会弹出已登录用户的身份验证消息(我相信错误代码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);
}

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

1 个答案:

答案 0 :(得分:0)

将以下已实现的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);
    }

}

请参阅 documentation page for onReceivedHttpAuthRequest