您的位置:首页技术文章
文章详情页

android H5本地缓存加载优化的实战

【字号: 日期:2022-09-19 18:51:55浏览:17作者:猪猪

2020年最后一周,正准备摸摸鱼回家过年,须不知“惊天阴谋”已在领导层酝酿。竖日,组长带着诡异的微笑向我走来:

组长: “快过年了,你回家路途遥远,要不要请两天假?”

我: “组长,你真是我的知己,想我所想,思我所思,你这么一说我就不客气了,那我就请两天”

组长:“行,请假肯定没问题,我一向很照顾兄弟们!!”(那一刻,一股暖流心中而过,早已将这一年他对我的“压榨”抛之脑后)

“不过我还有个事跟你说下,回家前有个需求你得完成”

我:“what??? ,,,, TMD......”

组长:“需求是这样的:最近客户反应HTML加载有点慢,需要优化下,最好能做到秒开,,,,加油,我相信你”。

我:“不是这H5,加载慢那你前端的原因呀,你找我。。。我。。。”(组长已经远去)

带着沉重的心情开始研究优化,开始在webView 层做文章,开启缓存,预加载,一顿操作效果微乎其微。

然后开始打前端文件的注意,一般本地Html文件加载速度比通过Url的加载速度会快很多。于是去找前端要了一个本地文件放到项目里进行本地加载。果不其然,速度嗖嗖的,此时,尴尬的事情发生了,前端功能经常更新,如果放到项目里岂不是H5更新,我就得升级版本。且不说我得累死,估计这方案提到组长那,他得提刀来见了。 于是另辟蹊径,将通过接口下载HTMl文件,存在到手机本地,这样webView去加载手机本地文件即可。一弧诡异的微笑在脸上散开。

动手: 1.偷了懒,用 filedownloader 去下载了Html压缩文件

implementation ’com.liulishuo.filedownloader:library:1.7.7’

封装下载工具类:

public class FileDownloadUtils {public static FileDownloadUtils instance = null;public FileDownloadUtils() {}public static FileDownloadUtils getInstance() { if (null == instance) { instance = new FileDownloadUtils(); } return instance;}/** * 单任务下载 * * @param downLoadUri 文件下载网络地址 * @param destinationUri 下载文件的存储绝对路径 */public void startDownLoadFileSingle(String downLoadUri, String destinationUri,FileDownLoaderCallBack callBack) { FileDownloader.getImpl().create(downLoadUri).setPath(destinationUri).setListener(fileDownloadListener(callBack)).start();}// 下载方法private FileDownloadListener fileDownloadListener(final FileDownLoaderCallBack callBack) { return new FileDownloadListener() { @Override protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) { //等待,已经进入下载队列 } @Override protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) { //下载进度回调 if (callBack != null){callBack.downLoadProgress(task,soFarBytes,totalBytes); } } @Override protected void completed(BaseDownloadTask task) { //完成整个下载过程 if (callBack != null){callBack.downLoadCompleted(task); } } @Override protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) { //暂停下载 } @Override protected void error(BaseDownloadTask task, Throwable e) { //下载出现错误 if (callBack != null){callBack.downLoadError(task,e); } } @Override protected void warn(BaseDownloadTask task) { //在下载队列中(正在等待/正在下载)已经存在相同下载连接与相同存储路径的任务 } };} public interface FileDownLoaderCallBack { //文件是否下载完成 void downLoadCompleted(BaseDownloadTask task); //文件是否下载失败 void downLoadError(BaseDownloadTask task, Throwable e); //文件下载进度 void downLoadProgress(BaseDownloadTask task, int soFarBytes, int totalBytes); }}

解压Zip文件

public class ZipUtils { public static final String TAG = 'ZIP'; public ZipUtils() { }/** * 解压zip到指定的路径 * * @param zipFileString ZIP的名称 * @param outPathString 要解压缩路径 * @throws Exception */public static void UnZipFolder(String zipFileString, String outPathString) throws Exception { ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFileString)); ZipEntry zipEntry; String szName = ''; while ((zipEntry = inZip.getNextEntry()) != null) { szName = zipEntry.getName(); if (zipEntry.isDirectory()) { szName = szName.substring(0, szName.length() - 1); File folder = new File(outPathString + File.separator + szName); folder.mkdirs(); } else { Log.e(TAG, outPathString + File.separator + szName); File file = new File(outPathString + File.separator + szName); if (!file.exists()) {Log.e(TAG, 'Create the file:' + outPathString + File.separator + szName);file.getParentFile().mkdirs();file.createNewFile(); } // 获取文件的输出流 FileOutputStream out = new FileOutputStream(file); int len; byte[] buffer = new byte[1024]; // 读取(字节)字节到缓冲区 while ((len = inZip.read(buffer)) != -1) {// 从缓冲区(0)位置写入(字节)字节out.write(buffer, 0, len);out.flush(); } out.close(); } } inZip.close();}public static void UnZipFolder(String zipFileString, String outPathString, String szName) throws Exception { ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFileString)); ZipEntry zipEntry; while ((zipEntry = inZip.getNextEntry()) != null) { //szName = zipEntry.getName(); if (zipEntry.isDirectory()) { //获取部件的文件夹名 szName = szName.substring(0, szName.length() - 1); File folder = new File(outPathString + File.separator + szName); folder.mkdirs(); } else { Log.e(TAG, outPathString + File.separator + szName); File file = new File(outPathString + File.separator + szName); if (!file.exists()) {Log.e(TAG, 'Create the file:' + outPathString + File.separator + szName);file.getParentFile().mkdirs();file.createNewFile(); } // 获取文件的输出流 FileOutputStream out = new FileOutputStream(file); int len; byte[] buffer = new byte[1024]; // 读取(字节)字节到缓冲区 while ((len = inZip.read(buffer)) != -1) {// 从缓冲区(0)位置写入(字节)字节out.write(buffer, 0, len);out.flush(); } out.close(); } } inZip.close();}/** * 压缩文件和文件夹 * * @param srcFileString 要压缩的文件或文件夹 * @param zipFileString 解压完成的Zip路径 * @throws Exception */public static void ZipFolder(String srcFileString, String zipFileString) throws Exception { //创建ZIP ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFileString)); //创建文件 File file = new File(srcFileString); //压缩 // LogUtils.LOGE('---->'+file.getParent()+'==='+file.getAbsolutePath()); ZipFiles(file.getParent()+ File.separator, file.getName(), outZip); //完成和关闭 outZip.finish(); outZip.close();}/** * 压缩文件 * * @param folderString * @param fileString * @param zipOutputSteam * @throws Exception */private static void ZipFiles(String folderString, String fileString, ZipOutputStream zipOutputSteam) throws Exception { // LogUtils.LOGE('folderString:' + folderString + 'n' +'fileString:' + fileString + 'n=========================='); if (zipOutputSteam == null) return; File file = new File(folderString + fileString); if (file.isFile()) { ZipEntry zipEntry = new ZipEntry(fileString); FileInputStream inputStream = new FileInputStream(file); zipOutputSteam.putNextEntry(zipEntry); int len; byte[] buffer = new byte[4096]; while ((len = inputStream.read(buffer)) != -1) { zipOutputSteam.write(buffer, 0, len); } zipOutputSteam.closeEntry(); } else { //文件夹 String fileList[] = file.list(); //没有子文件和压缩 if (fileList.length <= 0) { ZipEntry zipEntry = new ZipEntry(fileString + File.separator); zipOutputSteam.putNextEntry(zipEntry); zipOutputSteam.closeEntry(); } //子文件和递归 for (int i = 0; i < fileList.length; i++) { ZipFiles(folderString+fileString+'/', fileList[i], zipOutputSteam); } }}/** * 返回zip的文件输入流 * * @param zipFileString zip的名称 * @param fileString ZIP的文件名 * @return InputStream * @throws Exception */public static InputStream UpZip(String zipFileString, String fileString) throws Exception { ZipFile zipFile = new ZipFile(zipFileString); ZipEntry zipEntry = zipFile.getEntry(fileString); return zipFile.getInputStream(zipEntry);}/** * 返回ZIP中的文件列表(文件和文件夹) * * @param zipFileString ZIP的名称 * @param bContainFolder 是否包含文件夹 * @param bContainFile 是否包含文件 * @return * @throws Exception */public static List<File> GetFileList(String zipFileString, boolean bContainFolder, boolean bContainFile) throws Exception { List<File> fileList = new ArrayList<File>(); ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFileString)); ZipEntry zipEntry; String szName = ''; while ((zipEntry = inZip.getNextEntry()) != null) { szName = zipEntry.getName(); if (zipEntry.isDirectory()) { // 获取部件的文件夹名 szName = szName.substring(0, szName.length() - 1); File folder = new File(szName); if (bContainFolder) {fileList.add(folder); } } else { File file = new File(szName); if (bContainFile) {fileList.add(file); } } } inZip.close(); return fileList;}}

下载:

File file = new File(Constants.saveH5FilePath); if (file.exists()) { file.delete(); } //开始下载ZIP压缩包 FileDownloadUtils.getInstance().startDownLoadFileSingle(bean.getUrl(), Constants.saveH5FilePath,new FileDownloadUtils.FileDownLoaderCallBack() { @Override public void downLoadCompleted(BaseDownloadTask task) { try { //解压ZIP压缩包 ZipUtils.UnZipFolder(Constants.saveH5FilePath, Constants.unH5ZipPath); PreferencesUtil.getInstance().saveParam('H5VersionName', H5VersionName); } catch (Exception e) { e.printStackTrace(); } } @Override public void downLoadError(BaseDownloadTask task, Throwable e) { } @Override public void downLoadProgress(BaseDownloadTask task, int soFarBytes, int totalBytes) { }});

webView 加载:

mWebSe.loadUrl('file:'+ Constants.unH5ZipPath+'/index.html');

此时,心如止水 ,,回家,搜噶。。。。

以上就是android H5本地缓存加载优化的实战的详细内容,更多关于android H5本地缓存加载优化的资料请关注好吧啦网其它相关文章!

标签: Android
相关文章: