Turn from: http://blog.csdn.net/crazy_zihao/article/details/51557425 Preface
When using WebView to load an HTTPS resource file, if the authentication certificate is not recognized by Android, there will be a failure to load the corresponding resource problem successfully. So, we have to deal with this situation in response.
Resolve Step 1. Enable mixed content
In Android5.0, WebView has made some changes, if your system target API is more than 21:
- Mixed content and third-party cookies are prohibited by default. You can use Setmixedcontentmode () and setacceptthirdpartycookies () to enable them individually.
- The system can now intelligently select the portion of the HTML document to draw. This new feature can reduce memory footprint and improve performance. To render the entire HTML document at once, you can call this method Enableslowwholedocumentdraw ()
- If your app's target API is below 21: The system allows mixed content and third-party cookies, and always renders the entire HTML document at once.
Add the following code to the class that uses WebView:
- [HTML] view plain copy print?
- Android 5.0 above default does not support mixed Content
- if (Build.VERSION.SDK_INT >= build.version_codes. LOLLIPOP) {
- Webview.getsettings (). Setmixedcontentmode (
- Websettings.mixed_content_compatibility_mode);
- }
2. Set WebView to accept certificates for all websites
In the case that the certificate is not accepted by Android, we can fix it by setting the Onreceivedsslerror method of rewriting webviewclient to accept the certificate of all the websites, the code is as follows:
[HTML]View PlainCopyprint?
- Webview.setwebviewclient (New Webviewclient () {
- @Override
- public void Onreceivedsslerror (WebView view,
- Sslerrorhandler handler, sslerror error) {
- TODO auto-generated Method Stub
- Handler.cancel ();//Android Default processing mode
- Handler.proceed ();//Accept certificates for all websites
- Handlemessage (Message msg);//Other processing
- }
- });
Note: When overriding WebViewClient
the onReceivedSslError
method, be aware that the method must be removed onReceivedSslError
super.onReceivedSslError(view, handler, error);
, otherwise the setting is not valid.
Android WebView Mixed Content cannot display picture resolution