您的位置:首页 > 科技 > 能源 > 中国建筑装饰装修官网_牛奶网页设计素材_自动点击器软件_全球搜怎么样

中国建筑装饰装修官网_牛奶网页设计素材_自动点击器软件_全球搜怎么样

2025/4/5 11:19:47 来源:https://blog.csdn.net/ck3345143/article/details/146984505  浏览:    关键词:中国建筑装饰装修官网_牛奶网页设计素材_自动点击器软件_全球搜怎么样
中国建筑装饰装修官网_牛奶网页设计素材_自动点击器软件_全球搜怎么样

在WebView加载URL时添加时间戳可以防止缓存问题,以下是实现方法:

1.对于加载网页URL的情况:
 
  1. MainActivity.java
// ... existing code ...String originalUrl = "https://example.com/page.html";
String urlWithTimestamp = originalUrl + (originalUrl.contains("?") ? "&" : "?") + "t=" + System.currentTimeMillis();
webView.loadUrl(urlWithTimestamp);// ... existing code ...
2.对于加载本地HTML文件的情况:

MainActivity.java

// ... existing code ...String filePath = "file:///android_asset/index.html";
String htmlContent = getHtmlContentFromAsset("index.html"); // 需要实现这个方法
htmlContent = injectTimestamp(htmlContent);
webView.loadDataWithBaseURL(filePath, htmlContent, "text/html", "UTF-8", null);// ... existing code ...

3.辅助方法实现:

private String getHtmlContentFromAsset(String filename) {try {InputStream is = getAssets().open(filename);byte[] buffer = new byte[is.available()];is.read(buffer);is.close();return new String(buffer);} catch (IOException e) {e.printStackTrace();return "";}
}private String injectTimestamp(String html) {return html.replace("</body>", "<script>document.write('<input type=\"hidden\" name=\"t\" value=\"" + System.currentTimeMillis() + "\">')</script></body>");
}

4.如果使用WebViewClient统一处理:
 

MainActivity.java

webView.setWebViewClient(new WebViewClient() {@Overridepublic boolean shouldOverrideUrlLoading(WebView view, String url) {if(!url.contains("t=")) {String newUrl = url + (url.contains("?") ? "&" : "?") + "t=" + System.currentTimeMillis();view.loadUrl(newUrl);return true;}return false;}
});

注意事项:

  1. 时间戳参数名建议使用"t"或"timestamp"等简短名称
  2. 对于重要页面,建议将时间戳放在URL参数中(?t=xxx)
  3. 本地HTML注入的时间戳会在每次加载时变化

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com