我有一个适用于以前版本的Android操作系统的WebView,但是在加载Ice Cream Sandwich设备时遇到了问题。我正在尝试加载的页面需要BASIC AUTH,并且为了非安全目的而传入查询字符串参数。
问题在于WebViewClient.onReceivedHttpAuthRequest方法:
String[] up = view.getHttpAuthUsernamePassword(host, realm);
它返回一个null数组而不是用户的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;
}
}
答案 0 :(得分:3)
您是否检查了在#onReceivedHttpAuthRequest中收到的内容?
我刚刚修改了你说的话。似乎ICS不仅作为主机返回“主机”,而且还返回端口,例如:HOST_OF_WEBAPP:80。
因此,如果在#onReceivedHttpAuthRequest中收到“HOST_OF_WEBAPP:80”,则不会检索存储为“HOST_OF_WEBAPP”的内容。
试一试!