码迷,mamicode.com
首页 > 移动开发 > 详细

Android webview Mixed Content无法显示图片解决

时间:2016-12-25 20:40:30      阅读:1130      评论:0      收藏:0      [点我收藏+]

标签:情况   系统默认   oar   source   ase   word   ide   blog   tps   

转自:http://blog.csdn.net/crazy_zihao/article/details/51557425

前言

  在使用WebView加载https资源文件时,如果认证证书不被Android认可,那么会出现无法成功加载对应资源问题。那么,我们就要针对这一状况作出对应的处理。

解决步骤

1. 启用mixed content

在Android5.0中,WebView方面做了些修改,如果你的系统target api为21以上:

  • 系统默认禁止了mixed content和第三方cookie。可以使用setMixedContentMode() 和 setAcceptThirdPartyCookies()以分别启用。
  • 系统现在可以智能选择HTML文档的portion来绘制。这种新特性可以减少内存footprint并改进性能。若要一次性渲染整个HTML文档,可以调用这个方法enableSlowWholeDocumentDraw()
  • 如果你的app的target api低于21:系统允许mixed content和第三方cookie,并且总是一次性渲染整个HTML文档。 
    在使用WebView的类中添加如下代码:
  • [html] view plain copy
     
     print?
    1. // android 5.0以上默认不支持Mixed Content  
    2. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {  
    3.     webView.getSettings().setMixedContentMode(  
    4.         WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);  
    5. }  

2. 设置WebView接受所有网站的证书

在认证证书不被Android所接受的情况下,我们可以通过设置重写WebViewClient的onReceivedSslError方法在其中设置接受所有网站的证书来解决,具体代码如下:

 

[html] view plain copy
 
 print?
  1. webView.setWebViewClient(new WebViewClient() {  
  2.             @Override  
  3.             public void onReceivedSslError(WebView view,  
  4.                     SslErrorHandler handler, SslError error) {  
  5.                 // TODO Auto-generated method stub  
  6.                 // handler.cancel();// Android默认的处理方式  
  7.                 handler.proceed();// 接受所有网站的证书  
  8.                 // handleMessage(Message msg);// 进行其他处理  
  9.             }  
  10. });  

 

注:在重写WebViewClientonReceivedSslError方法时,注意一定要去除onReceivedSslError方法的super.onReceivedSslError(view, handler, error);,否则设置无效。

Android webview Mixed Content无法显示图片解决

标签:情况   系统默认   oar   source   ase   word   ide   blog   tps   

原文地址:http://www.cnblogs.com/zhengshiqiang47/p/6220295.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!