SoFunction
Updated on 2025-03-11

Detailed explanation of android using webview to load web pages (https and http)

Can't open the webpage for https request

When loading an https page with an ssl layer, if the security certificate of this website cannot be authenticated on Android, the WebView will become a blank page, and a risk warning box will not pop up like in the PC browser. Therefore, we have to deal with this situation. (This certificate is limited to Android systems with version 2.1 or above)

(new WebViewClient(){

@override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error){

//(); The default processing method, WebView becomes blank page ();Accept the certificate

//handleMessage(Message msg); Other processing}

// This line of code must be added otherwise the effect will not appear ().setJavaScriptEnabled(true); 

Check the Android browser source code in the SubWindowClient method in the class. If you simply accept all certificates, just click the process() method.

After writing this,Run directly, the page can be opened。 But after putting in the signature package, it still couldn't be opened! ! !

After following along the way, I found that the method was confused

proguard:

   this$0 -> a
  void onReceivedSslError(,,) -> onReceivedSslError

Therefore, it is necessary to add the following to the obfuscation file:

-keep public class

-dontwarn

-dontwarn

-dontwarn

Another thing to mention is that if the phone adds a proxy. It can't be opened either

But sslerror is provided only since 2.2. What to do with the previous version?

You can import the source code of onReceivedSslError() in android2.2 into your own project. The specific method is as follows:

First import the sum in the Android 2.2 package into your own project. When importing, you need to create the same package name as in the Android 2.2 source code package. Then when using webview, just direct the package name in your project!

Note that several places to set the webView may help:

1. By default, https cannot be accessed, and the onReceivedSslError of WebViewClient needs to be rewrite

ps: API Level > 2.1, or provide and files

The rewrite part can generally be processed without special treatment, just (); just accept the certificate.

2. By default, the js box cannot be popped, and the onJsAlert of WebChromeClient needs to be rewrite
The rewrite part does not require special processing, and it returns directly (view, url, message, result);

pps: The webView needs to be effective if setWebViewClient and setWebChromeClient

Loading a webpage with a webview may occur:

The webpage loaded by the webview is requested by http. If there is an image in the webpage and the address of the image is requested by https, then the webpage is loaded with the webview, and the picture will not be displayed.

This kind of error will be reported

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

It means that http request and https request are confused

Note: To load a webpage with webview, you must use the same request

The problem of Android 5.0 webview not loading mixed content between http and https

On Android Lollipop, webview does not allow loading of mixed content between http and https. For example, if the web page you visit is /, if the res of https are included, this res will not be displayed. If you look closely at the log, you can see the following prompts:

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

So, how to solve it? It's simple, just set up webSettings and allow it to load mixed network protocol content.

(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.