android webview 访问https页面图片不显示(5.0+)或显示白页(6.0+)

总结一下今天的踩的坑
根据项目需要所有请求都换成了https,但是我们的图片资源是http的
在后台环境搭建完成后,我发现我的页面是白页!!!

Mixed Content: The page at 'https:xxxxx' was loaded over HTTPS, but requested an insecure image 'http://xxxxxxb95cc58806b3ce0a9.jpg'. This request has been blocked; the content must be served over HTTPS.", source: https:xxxxx 

好吧 报了警告.
但是警告也不应该出先白页啊..随后看WebSettings代码
发现

 /**
     * Used with {@link #setMixedContentMode}
     *
     * In this mode, the WebView will not allow a secure origin to load content from an insecure
     * origin. This is the preferred and most secure mode of operation for the WebView and apps are
     * strongly advised to use this mode.
     */
    public static final int MIXED_CONTENT_NEVER_ALLOW = 1;

看到了吧 官方强烈推荐使用这种方式
WebView将不允许一个安全的连接混合使用HTTPS +HTTP
这好像5.0以上才有的
因为这个才造成了图片不显示.
所以我们要设置一下
使用下面这个(允许HTTPS +HTTP)

 /**
     * Used with {@link #setMixedContentMode}
     *
     * In this mode, the WebView will allow a secure origin to load content from any other origin,
     * even if that origin is insecure. This is the least secure mode of operation for the WebView,
     * and where possible apps should not set this mode.
     */
    public static final int MIXED_CONTENT_ALWAYS_ALLOW = 0;

设置方式

   WebSettings wetSettings = webView.getSettings();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            wetSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }

但是 貌似还是白页....
这时我的内心是崩溃的,因为 android6.0版本以下正常 ,但是 android6.0版本以上的还是白页
继续寻找原因..然后在
WebViewClient中发现onReceivedSslError方法是这样描述的

 /**
     * Notify the host application that an SSL error occurred while loading a
     * resource. The host application must call either handler.cancel() or
     * handler.proceed(). Note that the decision may be retained for use in
     * response to future SSL errors. The default behavior is to cancel the
     * load.
     *
     * @param view The WebView that is initiating the callback.
     * @param handler An SslErrorHandler object that will handle the user's
     *            response.
     * @param error The SSL error object.
     */
    public void onReceivedSslError(WebView view, SslErrorHandler handler,
            SslError error) {
        handler.cancel();
    }

原本方法是当发现ssl加载出现问题时 默认会取消这个请求,就是白页的原因
那就重写WebViewClient的onReceivedSslError方法

  @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error){
            handler.proceed(); // 接受网站证书
        }

完事 .好坑
有问题请留言

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,687评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,026评论 19 139
  • 0. 前言 前面有被用户投诉 APP 流量消耗厉害: 于是乎考虑了流量方面的问题。暂时 APP 中涉及流量的几个方...
    zyl06阅读 24,305评论 5 63
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,261评论 4 61
  • 掬一捧岁月 握一份懂得 书一笔清远 盈一眸恬淡 时光静好 心 永远微笑向暖 从今天开始,开启一个新技能,感觉...
    我是我自己的骄傲阅读 261评论 0 4