我有一个 WebView 可以在以前版本的 Android 操作系统上正常工作,但在加载 Ice Cream Sandwich 设备时出现问题。我尝试加载的页面需要 BASIC AUTH 并且出于非安全目的传入查询字符串参数。
问题在于 WebViewClient.onReceivedHttpAuthRequest 方法:
String[] up = view.getHttpAuthUsernamePassword(host, realm);
它返回一个空数组而不是用户的 ID 和密码。任何人都知道为什么这在 Android ICS 中的作用不同?我得到了其他版本的操作系统的用户 ID 和密码。
完整代码:
public class DestinationWebViewScreen extends Activity{
WebView mWebView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.destination_webview);
mWebView = (WebView) findViewById(R.id.social_destination_webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setLoadsImagesAutomatically(true);
mWebView.clearCache(true);
mWebView.clearFormData();
mWebView.clearHistory();
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.setHttpAuthUsernamePassword("HOST_OF_WEBAPP","Spring Security Application", "USERID", "PASSWORD");
mWebView.setWebViewClient( new WebViewClient() {
@Override
public void onReceivedHttpAuthRequest (WebView view,
HttpAuthHandler handler, String host,String realm){
String[] up = view.getHttpAuthUsernamePassword(host, realm);
if( up != null && up.length == 2 ) {
handler.proceed(up[0], up[1]);
}
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if(progressDialog != null){
progressDialog.dismiss();
}
showProgressbar();
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
dismissProgressDialog();
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
dismissProgressDialog();
}
});
mWebView.loadUrl("https://webapphost/webapp?customerId=0-222293");
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(networkStateReceiver, filter);
}
public void showProgressbar() {
progressDialog = new CustomProgressDialog(SocialDestinationWebViewScreen.this);
progressDialog.getWindow().setBackgroundDrawable(getResources().getDrawable(android.R.color.transparent));
progressDialog.setMessage(SocialDestinationWebViewScreen.this.getResources().getString(R.string.loading));
progressDialog.show();
}
void dismissProgressDialog(){
if(progressDialog != null){
progressDialog.dismiss();
}
progressDialog = null;
}
}