SoFunction
Updated on 2025-04-10

Android solves the problem of WebView calling loadData method to display garbled code

Android solves the problem of garbled code calling loadData() method in WebView

first step:

().setDefaultTextEncodingName("UTF-8"); 

Step 2:

(data, "text/html; charset=UTF-8", null); 

Commonly used WebView configurations:

private void initWebView() { 
  ().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); 
  ().setDefaultTextEncodingName("UTF-8"); 
  ().setJavaScriptEnabled(true); 
  ().setJavaScriptCanOpenWindowsAutomatically(true); 
  ().setSupportZoom(true); 
  ().setDomStorageEnabled(true); 
  ().setAllowFileAccess(true); 
  ().setUseWideViewPort(true); 
  ().setBuiltInZoomControls(true); 
  ().setLoadWithOverviewMode(true); 
  (); 
  (url); 
  (new WebViewClient() { 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      return (view, url); 
    } 
 
    @Override 
    public void onLoadResource(WebView view, String url) { 
      (view, url); 
    } 
 
    @Override 
    public void onPageStarted(WebView view, String url, 
         favicon) { 
 
    }; 
 
    @Override 
    public void onPageFinished(WebView view, String url) { 
      (view, url); 
    } 
  }); 
 
} 

If you have any questions, please leave a message or go to the community of this site to exchange and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!