I am using a webview based application where i am rendering a url in the webview. The Url has a HTTP auth .
When i launch the url very first time,its onReceivedHttpAuthRequest() is called and I display a dialog for user to enter the authentication credentials that is auth username and password.
@Override
public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
final WebView mView = view;
final HttpAuthHandler mHandler = handler;
final EditText usernameInput = new EditText(mActivity);
usernameInput.setHint("Username");
final EditText passwordInput = new EditText(mActivity);
passwordInput.setHint("Password");
passwordInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
LinearLayout ll = new LinearLayout(mActivity);
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(usernameInput);
ll.addView(passwordInput);
Builder authDialog = new AlertDialog
.Builder(mActivity)
.setTitle("Authentication")
.setView(ll)
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
mHandler.proceed(usernameInput.getText().toString(), passwordInput.getText().toString());
dialog.dismiss();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
mView.stopLoading();
onLoadListener.onAuthCancel((MyWebView)mView, mTitleTextView);
}
});
if(view!=null)
authDialog.show();
}
On Submitting the request proceed well and the url is loaded. But After I exit the app using back button(not sending in background), if i launch it again and tru to load the same url it directly load the url without asking for credentials that is onReceivedHttpAuthRequest() is never called again.
I am also clearing the credentials on app exit using following code:
WebViewDatabase webDB = WebViewDatabase.getInstance(BrowserActivity.this);
if(webDB!=null){
if(webDB.hasFormData())
webDB.clearFormData();
if(webDB.hasUsernamePassword())
webDB.clearUsernamePassword();
if(webDB.hasHttpAuthUsernamePassword())
webDB.clearHttpAuthUsernamePassword();
}
webView.clearCache(true);
Also i am clearing all the webview cache, cookies, and application's cache directory and the webview databases:
BrowserActivity.this.deleteDatabase("webview.db");
BrowserActivity.this.deleteDatabase("webviewCache.db");
I don't know why this is happening. Is there anybody who can help me on this. At least on the issue that why onReceivedHttpAuthRequest() is not called?
shouldOverrideUrlLoading(WebView view, WebResourceRequest request) Give the host application a chance to take control when a URL is about to be loaded in the current WebView. boolean. shouldOverrideUrlLoading(WebView view, String url) This method was deprecated in API level 24.
Android WebView is a system component for the Android operating system (OS) that allows Android apps to display content from the web directly inside an application.
WebViewClient is the object responsible for most the actions inside a WebView. JavaScript enabled, security, routing, etc. You can make a custom one, as well as use a Chrome one.
before load the url,set Authorization information in http headers
Map extraHeaders = new HashMap<>();
extraHeaders.put("Authorization", "TlRM");//the value is Casually set.
webView.loadUrl(url, extraHeaders);
in WebViewClient
@Override
public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler,
String host, String realm) {
view.loadUrl(url);//reload the url.
handler.proceed(account, password);
}
I resolve this issue,but I can not speak English very well. Hope it can help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With