Android webview Load https Link Error or No Response Resolution

  • 2021-11-30 01:27:15
  • OfStack

Recently, when doing wireless WiFi, an advertisement page will pop up when the final authentication is successful, so webview is used to load it for 1 time, but the result does not respond. After printing url for 1 time, it is found that it is in https format. When using WebView to load https resource file, if the authentication certificate is not recognized by Android, the corresponding resource cannot be successfully loaded. Then, we should make corresponding treatment for this 1 situation.

So Baidu took 1 time, and recorded 1 time here to give you a reference:

1. Set WebView to accept certificates from all websites

In the case that the authentication certificate is not accepted by Android, we can solve it by setting onReceivedSslError method that rewrites WebViewClient, rewriting onReceivedSslError method of WebView and adding handler. proceed method, but App will be warned if it is put on GooglePlay. Therefore, it is recommended to use the second method below.

The specific code is as follows:


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

Note: When rewriting onReceivedSslError method of WebViewClient, note that super. onReceivedSslError of onReceivedSslError method must be removed (view, handler, error); Otherwise, the setting is invalid.

2. Enable mixed content

Android webView does not turn on MixedContentMode by default from Lollipop, so we can meet most of our needs by turning it on.


webView.setWebViewClient(new WebViewClient(){
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
webView.getSettings()
.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 cookie are disabled by default. You can use setMixedContentMode () and setAcceptThirdPartyCookies () to enable them, respectively.

The system can now intelligently select portion of HTML document to draw. This new feature can reduce the memory footprint and improve performance. To render the entire HTML document once, you can call this method enableSlowWholeDocumentDraw () if your app target api is lower than 21: mixed content and third party cookie are allowed, and the entire HTML document is always rendered once.

Add the following code to the class that uses WebView:


// android 5.0 The above is not supported by default Mixed Content
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
 webView.getSettings().setMixedContentMode(
  WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
}

Results:

My problem does not appear in these places, but in loading Url, some Url uses http instead of https, which makes it impossible to load and realize its functions. Then ask the background developer to change http to https. In this way, it worked. Or you can use some third-party libraries to load it.

Test:

1. Calling Gaode map can't be displayed. The front-end engineer used http... then changed to https

2. Some functions are not realized, because there is no certificate in the background... Then, all of them are changed back to http

Additional knowledge: Android WebView Pits encountered when loading web links

Originally, I thought 10 points was a simple problem, but I encountered many pits. Up to now, I can't guarantee that there will be no problems, but I have solved most of the problems. . .

Here is a list of several problems

There is no response to jump to other pages in webview

There is no response to download files in webview

Some URL links cannot be displayed

Automatic jump to browser when some URLs are opened

1. There is no response to jump to other pages in webview

The code before modification is as follows:


 WebSettings webSettings = webView.getSettings();
 // Settings WebView Property, capable of executing Javascript Script 
 webSettings.setJavaScriptEnabled(true);
 // Setting access to files 
 webSettings.setAllowFileAccess(true);
 // Set up support for scaling 
 webSettings.setBuiltInZoomControls(true);
 webView.loadUrl(link);

 webView.setWebViewClient(new webViewClient ());

After modification, many webview settings have been added, including:


webSettings.setAppCacheEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.supportMultipleWindows();
webSettings.setAllowContentAccess(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setSavePassword(true);
webSettings.setSaveFormData(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setLoadsImagesAutomatically(true);

webView.setWebChromeClient(new WebChromeClient());// It's best not to lose this business 

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

After the modification is completed, you can jump to other url links in webview, even if it solves the first problem.

2. There is no response to download files in webview

Because webview itself does not have download function, it is necessary for the system to handle or customize the download.

webview provides us with a download monitoring interface. Let's implement download processing below:


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

Then add:

webView.setDownloadListener(new MyDownLoad());

Here is the download function to the system to deal with, and there is no special needs, so it is not customized.

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

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


private class webViewClient extends WebViewClient {
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
   view.loadurl(url)// Return true Represents the current webview Open in, return false Indicates that the browser is open 
   return super.shouldOverrideUrlLoading(view,url);  }

  @Override
  public void onPageStarted(WebView view, String url, Bitmap favicon) {
   if(!dialog.isShowing()) {
    dialog.show();
   }
   super.onPageStarted(view, url, favicon);
  }

  @Override
  public void onPageFinished(WebView view, String url) {
   if(dialog.isShowing()){
    dialog.dismiss();
   }
   super.onPageFinished(view, url);
  }
 }

Pay attention to the shouldOverrideUrlLoading method. Usually we may set view. loadurl (url) in it. It is this code that causes some web pages to be unable to open. This may be because webview itself has restrictions on loading web pages. So I dropped this sentence and returned true (true means opening a web page in the current webview, while false prefers a browser to open a web page). In doing so, I found that some web pages still couldn't be opened. So webView. setWebViewClient (new webViewClient ()) was deleted directly, but webView. setWebChromeClient (new WebChromeClient ()) could not be deleted. As a result, the web page was opened, but some automatically jumped to the browser to open it. But our requirement is not to open the browser. . .

4. Some URLs open and automatically jump to the browser

After receiving the above, Open some URLs and jump to the browser. How to prevent him from jumping? Returning to true can prohibit jumping, but some URLs can't be opened in webview. Next, instead of returning it directly to true or false, it will return to super and let its parent class handle it. The result is not transferred to the browser and the webpage is opened. Now, it ends like this for the time being, and most URLs are compatible.


Related articles: