SoFunction
Updated on 2025-04-10

Android webview loading https link error or unresponsive solution

Recently, when I was doing wireless WiFi, an advertisement page popped up when the authentication was successful. So I used the webview to load it, but it didn't respond. I printed the url and looked at it. I found that it was in https format. When using WebView to load the https resource file, if the authentication certificate is not recognized by Android, the corresponding resource will not be successfully loaded. Then, we must make corresponding treatments to this situation.

So I took a look at it on Baidu, recorded it here, and gave you a reference:

1. Set WebView to accept certificates from all websites

When the authentication certificate is not accepted by Android, we can solve it by setting the onReceivedSslError method of the WebViewClient to rewrite the certificate of all websites. Rewriting the onReceivedSslError method of the WebView, adding the method, but if the App is listed on GooglePlay, it will be warned. So it is recommended to use the second method below.

The specific code is as follows:

(new WebViewClient(){
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error){
();
}
});

Note: When rewriting the onReceivedSslError method of WebViewClient, be careful to remove the (view, handler, error); of the onReceivedSslError method, otherwise the setting is invalid.

2. Enable mixed content

Android webView starts with Lollipop and does not enable MixedContentMode by default, so we can enable it to meet most of our needs.

(new WebViewClient(){
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error){
if (.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
()
.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
});

In Android 5.0, some changes have been made to WebView. If your system target api is above 21:

Mixed content and third-party cookies are prohibited by default. You can use setMixedContentMode() and setAcceptThirdPartyCookies() to enable it separately.

The system can now intelligently select the portion of the HTML document to draw. This new feature reduces memory footprint and improves performance. To render the entire HTML document at once, you can call this method enableSlowWholeDocumentDraw(), if your app's target api is less than 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 using WebView:

// Mixed Content is not supported by default on Android 5.0 or aboveif (.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
 ().setMixedContentMode(
  WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
}

result:

My problem does not appear in these places, but in the event that some Url uses http instead of https when loading Url, resulting in the inability to load and the function cannot be implemented. Then the background developers are required to change http to https. That's it, it succeeded. Or you can also use some third-party libraries to load.

test:

1. Calling Gaode map cannot be displayed, the front-end engineer used http... so change to https

2. Some functions are not implemented because the background does not have a certificate... Then, all of them will be changed back to http

Supplementary knowledge:What pitfalls encountered when loading web page links on Android WebView

I thought it was very simple, but I encountered many pitfalls. I still can't guarantee that there will be no problems, but I just solved most of the problems. . .

Here are a few questions listed

Jumping other pages in webview without response

Downloading file in webview is unresponsive

Some URL links cannot be displayed

Some URLs will automatically jump to the browser

1. Jumping other pages in webview without response

The previous code is as follows:

 WebSettings webSettings = ();
 //Set WebView properties to be able to execute Javascript scripts (true);
 //Set to access files (true);
 //Settings support zoom (true);
 (link);

 (new webViewClient ());

After modification, many webview settings were added, including:

(true);
(true);
();
(true);
(.NARROW_COLUMNS);
(true);
(true);
(true);
(true);
(true);
(true);

(new WebChromeClient());//It is best not to throw away this line

Of course, some of these attributes may not be added. In order to meet various needs as much as possible, I added them all.

After the modification is completed, you can jump to other URL links in the webview, even if the first problem is solved.

2. Downloading files in webview without response

Because the webview itself does not have the download function, the system needs to handle or customize the download.

The webview provides us with a download listening interface, and we will implement the download processing below:

class MyDownLoad implements DownloadListener {
  @Override
  public void onDownloadStart(String url, String userAgent,
         String contentDisposition, String mimetype, long contentLength) {
   if ((".apk")) {
    /**
     * Download apk through the system
     */
    Uri uri = (url);
    Intent intent = new Intent(Intent.ACTION_VIEW,uri);
    startActivity(intent);
   }
  }
 }

Then add in the settings:

(new MyDownLoad());

Here we hand over the download function to the system for processing, and there are no special needs, so we don’t customize it.

3. Some URL links cannot be displayed, and an error is reported net::err_unknown_url_scheme

Since we sometimes customize the WebViewClient, the content is roughly as follows:

private class webViewClient extends WebViewClient {
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
   (url)//Return true means open in the current webview, return false means opening the browser   return (view,url);  }

  @Override
  public void onPageStarted(WebView view, String url, Bitmap favicon) {
   if(!()) {
    ();
   }
   (view, url, favicon);
  }

  @Override
  public void onPageFinished(WebView view, String url) {
   if(()){
    ();
   }
   (view, url);
  }
 }

Pay attention to the shouldOverrideUrlLoading method. We may set (url) in it. It is this code that causes some web pages to be unable to open. This may be because the webview itself has restrictions on loading the web page. So I removed this sentence and returned true when returning (true means opening the web page in the current webview, false is more inclined to open the web page in the browser). Doing this, I found that some web pages are still not open. So I deleted it directly (new webViewClient ()), but (new WebChromeClient ()) cannot be deleted. As a result, the web page is opened, but some will automatically jump to the browser to open. But our need is not to open the browser. . .

4. Some URLs will automatically jump to the browser when they are opened

As mentioned above, when you open some URLs and jump to the browser, how can you not let them jump? Returning true can prohibit jumping, but some URLs cannot be opened in the webview. Next, you will not directly return true or false, but will return super and let it handle the parent class. As a result, it is not adjusted to the browser and the web page is also opened. Now it is temporarily ending like this to achieve compatibility of most URLs.

Summarize:My understanding of webview is limited, so I don’t go into depth and only solve some problems. There may be deviations in understanding. Please forgive me if you have any questions. I hope I can give you a reference and I hope you can support me more.