问题
Hi I want to open this url http://3864.cloud-matic.net/ from my android webview and I have tried many ways but the app even not opens mainActivity. What I have tried is below.
public void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.main);
WebView webView = (WebView) findViewById(R.id.webView1);
webView.setHttpAuthUsernamePassword("cloud-matic.net/", "realm", username, password);
webView.loadUrl("http://3864.cloud-matic.net/");
}
Please give me idea where I am wrong. Ali
回答1:
webview.setWebViewClient(new MyWebViewClient ());
private class MyWebViewClient extends WebViewClient {
@Override
public void onReceivedHttpAuthRequest(WebView view,
HttpAuthHandler handler, String host, String realm) {
handler.proceed("me@test.com", "mypassword");
}
}
This is most voted solution there I am not sure where to set the URL to open.Please suggest.
回答2:
This should be perfect code snippet.
webView.getSettings().setJavaScriptEnabled(true);
webView.setFocusable(true);
webView.loadUrl("http://3864.cloud-matic.net/");
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedHttpAuthRequest(WebView view,
HttpAuthHandler handler, String host, String realm) {
handler.proceed("me@test.com", "mypassword");
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
prDialog.setMessage("Please wait ...");
prDialog.show();
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (prDialog != null) {
prDialog.dismiss();
}
}
});
回答3:
Based on answer in another topic I made it in a little different way.
This solution is working also in case, that you have auto login form displayed, when authentication failed (in such case, solution with overriding 'onReceivedHttpAuthRequest' is not working, because you are getting a normal html page without error).
String up = "yourusername"+":"+"yoursuperstrongpassword";
String authEncoded = new String(Base64.encodeBase64(up.getBytes()));
String authHeader = "Basic "+authEncoded;
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", authHeader);
myWebView.loadUrl("https://192.168.137.1:8443/tmedserver/doctor/doctorConsultations/consultationCall.mvc?consultationId=23", headers);
来源:https://stackoverflow.com/questions/21185400/basicauthentication-in-android-for-webview-not-working