In order to make WebView access fast, generally use caching technology, about caching here has a very detailed article about Android: hand by hand teach you to build WebView caching mechanism & resource preloading scheme, please be sure to see

In fact, HTTP protocol has its own caching mechanism, Android side through simple configuration can achieve, caching generally refers to the cache of Web static resources, such as HTML, JS, CSS, PNG, JPG, GIF, etc.

The cache configuration is mainly web front and back end Settings,

The cache configuration is mainly web front and back end Settings,

The cache configuration is mainly web front and back end Settings,

If caching is not configured on the front and back ends, HTTP servers have default Settings such as Etag and Last-Modified fields, but do not have cache-Control or Expires fields by default, and will still communicate with the server

Android WebView cache itself

Android WebView cache itself, also known as HTTP protocol cache, has a big defect here is the space given by the system is too small, only 12M, this should be a few years ago the standard, now I think it is far from enough, so it needs to be improved

Android WebView cache improvements

By intercepting all static resource requests and then improving on them by overriding the following WebViewClient method:


@Override

public WebResourceResponse shouldInterceptRequest(WebView view, String url){

return null;

}

Copy the code
@targetAPI (build.version_codes.lollipop)// call @Override public WebResourceResponse shouldInterceptRequest(WebView) view, WebResourceRequest request){ return null; }Copy the code

It then intercepts all static resources and puts them on local disk so that it can control the cache itself. There are two modes for caching

  • Normal mode

The common mode is the HTTP protocol cache. By default, the cache field in the header is used to determine whether the local cache has expired, whether the local cache has expired, and if the local cache has expired, and if the local cache has expired, then the local cache is compared with the server, and then the data is compared with the server or the local cache. If the web front and back end do not set the cache field, the request will still occur;

  • Mandatory mode

The force mode is to ignore the cache fields and force the cache of static resources, such as JS and CSS. If the static resource needs to be updated, let it change the static resource link, such as add MD5 value, add version, etc. General packaging tools have this function. This is normal for HTML files, because some HTML files don’t change their names, such as index.html

Basic flow chart





Enter picture description

Code implementation

I’ve wrapped this implementation into a library, CacheWebView, which is easy to use, and you’re welcome to slap it;

Two steps can be used:

  • Import library
The compile 'ren. Yale. The android: cachewebviewlib: 0.5'Copy the code
  • Change the WebView to ren. Yale. Android. Cachewebviewlib. CacheWebView

Over. Nothing else needs to be changed. CacheWebView has an internal cache200M by default, and the cache mode is the HTTP default

Other ways can be used to speed up access

  • Third party browser kernel, such as Tencent X5 Tencent browsing service

Advantages: Kernel-level acceleration, faster than the system’s own browser kernel

Disadvantages: Full reliance on third-party browsers, less flexibility

  • VasSonic

Advantages: Concurrency, caching and local updates, fast

Disadvantages: The configuration is complex and the server needs to modify a lot