Android loads web pages with WebView (HTTPS and HTTP)

1. Android cannot open the web page requested by HTTPS when loading

When there is an HTTPS page in SSL layer, if the security certificate of the website cannot be authenticated in Android, WebView will become a blank page and will not pop up a risk prompt box like in PC browser. Therefore, we must deal with this situation. (this certificate is only available for Android systems above version 2.1)

Check the Android browser source code and find it in the subwindowclient method in the tabcontrol.java class. If you simply accept all certificates, just call the process () method directly

After writing here, the direct run page can be opened. But after the signature bag is packed, it still can't be opened!!!

After tracking all the way, I found that the method was confused

Therefore, it is also necessary to add the following to the confusion file proguard.cfg:

-keep public class android.net.http.SslError

-dontwarn android.webkit.WebView

-dontwarn android.net.http.SslError

-dontwarn Android.webkit.WebViewClient

Another point to mention is that if the mobile phone adds an agent. It can't be opened

However, sslerror has only been available since 2.2. What about the previous version?

You can import the source code of onreceivedsslerror() in Android 2.2 into your own project. The specific methods are as follows:

First import sslerror.java and webviewclient.java in Android 2.2 package into your own project. When importing, you need to create the same package name as in Android 2.2 source package. Then, when using WebView, just import the package name in your own project!

Note that several places to set up WebView may be helpful:

1. By default, HTTPS cannot be accessed and onreceivedsslerror of webviewclient needs to be rewritten

PS: API level > 2.1, or provide sslerror.java and webviewclient.java files

Generally, the rewritten part can be directly handled by handler. Process () without special processing; Just accept the certificate

2. By default, you can't pop the JS box. You need to rewrite the onjsalert part of webchromeclient, and you don't need special processing. You can directly return super.onjsalert (view, URL, message, result);

PPS: WebView cannot take effect until setwebviewclient and setwebchromeclient are

2. Another situation may occur when Android loads web pages with WebView:

The web page loaded by WebView is requested by HTTP. If there is a picture in the web page and the address of the picture is requested by HTTPS, the picture will not be displayed when the web page is loaded by WebView.

Would report such a mistake

Mixed Content as loaded over HTTPS,but requested an insecure image

It means that HTTP requests and HTTPS requests are confused

Note: when loading web pages with WebView, you must use the same request

Android 5.0 WebView cannot load the mixed content of HTTP and HTTPS

On Android lollipop, WebView is not allowed to load the mixed content of HTTP and HTTPS by default. For example, the web page you visit is http://xxx.com/a.html , if a.html contains HTTPS res, the res will not be displayed. If you carefully observe the log, you can see the following prompts:

。。。。。。 was loaded over HTTPS,but requested an insecure image。。。。。。。。。。

So, how to solve it? It's very simple. Just set websettings and allow it to load mixed network protocol content.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>