我正在使用基于webview的应用程序,我在webview中呈现URL.Url具有HTTP身份验证.
当我第一次启动url时,会调用onReceivedHttpAuthRequest()并显示一个对话框,供用户输入身份验证凭据,即auth用户名和密码.
@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(); }
在提交请求时,请继续并加载URL.但是在我使用后退按钮(不在后台发送)退出应用程序后,如果我再次启动它并加载相同的URL,则直接加载URL而不要求提供onReceivedHttpAuthRequest()的凭据再也不会被调用.
我还使用以下代码清除app退出的凭据:
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);
此外,我正在清除所有webview缓存,cookie和应用程序的缓存目录和webview数据库:
BrowserActivity.this.deleteDatabase("webview.db"); BrowserActivity.this.deleteDatabase("webviewCache.db");
我不知道为什么会这样.有没有人可以帮助我.至少在没有调用onReceivedHttpAuthRequest()的问题上?