SoFunction
Updated on 2025-03-01

Solve the problem of blanking of WebView loading H5 interface through URL

1. Permissions issue: You need to set network permissions in the configuration file

<uses-permission android:name="" />

2. Basic configuration issues

WebSettings webSettings = (); //Scaling is supported, default is true.

.setUseWideViewPort(true); // Scale to screen size webSettings

.setLoadWithOverviewMode(true); //Set the default encoding

webSettings .setDefaultTextEncodingName("utf-8"); ///// Set up automatic loading of pictures

webSettings .setLoadsImagesAutomatically(true);

.(true);// Settings to run JS scripts
.(false);// Used to set webview amplification
.(false);

3.Uncaught TypeError: Cannot call method 'getItem' of null exception appears

An exception occurred in this line. This is exactly the feature of html5. A locally stored thing has a larger storage volume than a cookie, but this must be started with code in the Android webview

Solution:Start the local storage function of html5 in webview.

().setDomStorageEnabled(true);

().setAppCacheMaxSize(1024*1024*8);

String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();

().setAppCachePath(appCachePath);

().setAllowFileAccess(true);

().setAppCacheEnabled(true);

4. When calling the getDeviceID method, js is not loaded, resulting in blank

Solution:

(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
(url);
return true;
}

@Override
public void onPageFinished(WebView view, String url) {
(view, url);
//Execute the js function you want to call hereif(!flag_get_deviceid){
load();
}
}

@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
(view, errorCode, description, failingUrl);
}

});

private boolean flag_get_deviceid=false;
public void load(){
String key="";
String androidID="";
try{
androidID = (getContentResolver(),Secure.ANDROID_ID);
(TAG, "androidID:"+androidID);}catch(Exception e){
(TAG, "");
}finally{
String script=("javascript:getDeviceID('"+androidID+"')");
(script, new ValueCallback&lt;String&gt;() {

@Override
public void onReceiveValue(String value) {
(TAG, "onReceiveValue value=" + value);

if(value!=null){
flag_get_deviceid=true;
}
}});
}
}

The mobile version problem is that the H5 interface is now diversified, resulting in many H5 interfaces being unable to display on lower version models or being inconsistent in style.

Solution:One is to redesign the lower version of the h5 interface, and the other is to set the lowest version of the project

Supplementary knowledge:WebView - Use WebView to access the Url list in turn

Sometimes, we need to use WebView to access the Url list in turn to refresh the web page;

1.1 WebView creation

webView = (WebView) findViewById();

1.2 WebView setting parameters

  // Set cache  ().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
  // No cache setting  // ().setCacheMode(WebSettings.LOAD_NO_CACHE);
  // Clean the cache  (true);
  // Clean up history  ();
  // Clean up cookies  (this);
  ().startSync();
  ().removeSessionCookie();
  // Settings can support zoom  ().setSupportZoom(true);
  // Set the zoom tool appears  ().setBuiltInZoomControls(true);
  ().setJavaScriptEnabled(true);

1.3 Get the Url list

int index = 0;
 
String [] strList = new String[]{"https://",
"","",};

1.4 WebView Settings WebViewClient

// Create WebViewClient directly  (new WebViewClient() {
   @Override
   public void onPageFinished(WebView view, String url) {
    (view, url);
    index++;
    if(index&gt;){
 
    }else {
     (true);
     ();
     ("===onPageFinished====", index + "=======");
     refreshWebpage(index);
    }
   }
  });

1.5 WebView Loading Url

 public void refreshWebpage(int index) {
  String csdnStr = urlStr + artStr[index];
 
  // Call url directly  (csdnStr);
 }

The above article solves the problem of blanking the WebView interface loading through URLs is all the content I share with you. I hope it can give you a reference and I hope you support me more.