android H5本地緩存加載優(yōu)化的實(shí)戰(zhàn)
2020年最后一周,正準(zhǔn)備摸摸魚(yú)回家過(guò)年,須不知“驚天陰謀”已在領(lǐng)導(dǎo)層醞釀。豎日,組長(zhǎng)帶著詭異的微笑向我走來(lái):
組長(zhǎng): “快過(guò)年了,你回家路途遙遠(yuǎn),要不要請(qǐng)兩天假?”
我: “組長(zhǎng),你真是我的知己,想我所想,思我所思,你這么一說(shuō)我就不客氣了,那我就請(qǐng)兩天”
組長(zhǎng):“行,請(qǐng)假肯定沒(méi)問(wèn)題,我一向很照顧兄弟們!!”(那一刻,一股暖流心中而過(guò),早已將這一年他對(duì)我的“壓榨”拋之腦后)
“不過(guò)我還有個(gè)事跟你說(shuō)下,回家前有個(gè)需求你得完成”
我:“what??? ,,,, TMD......”
組長(zhǎng):“需求是這樣的:最近客戶反應(yīng)HTML加載有點(diǎn)慢,需要優(yōu)化下,最好能做到秒開(kāi),,,,加油,我相信你”。
我:“不是這H5,加載慢那你前端的原因呀,你找我。。。我。。。”(組長(zhǎng)已經(jīng)遠(yuǎn)去)
帶著沉重的心情開(kāi)始研究?jī)?yōu)化,開(kāi)始在webView 層做文章,開(kāi)啟緩存,預(yù)加載,一頓操作效果微乎其微。
然后開(kāi)始打前端文件的注意,一般本地Html文件加載速度比通過(guò)Url的加載速度會(huì)快很多。于是去找前端要了一個(gè)本地文件放到項(xiàng)目里進(jìn)行本地加載。果不其然,速度嗖嗖的,此時(shí),尷尬的事情發(fā)生了,前端功能經(jīng)常更新,如果放到項(xiàng)目里豈不是H5更新,我就得升級(jí)版本。且不說(shuō)我得累死,估計(jì)這方案提到組長(zhǎng)那,他得提刀來(lái)見(jiàn)了。 于是另辟蹊徑,將通過(guò)接口下載HTMl文件,存在到手機(jī)本地,這樣webView去加載手機(jī)本地文件即可。一弧詭異的微笑在臉上散開(kāi)。
動(dòng)手: 1.偷了懶,用 filedownloader 去下載了Html壓縮文件
implementation ’com.liulishuo.filedownloader:library:1.7.7’
封裝下載工具類(lèi):
public class FileDownloadUtils {public static FileDownloadUtils instance = null;public FileDownloadUtils() {}public static FileDownloadUtils getInstance() { if (null == instance) { instance = new FileDownloadUtils(); } return instance;}/** * 單任務(wù)下載 * * @param downLoadUri 文件下載網(wǎng)絡(luò)地址 * @param destinationUri 下載文件的存儲(chǔ)絕對(duì)路徑 */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) { //等待,已經(jīng)進(jìn)入下載隊(duì)列 } @Override protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) { //下載進(jìn)度回調(diào) if (callBack != null){callBack.downLoadProgress(task,soFarBytes,totalBytes); } } @Override protected void completed(BaseDownloadTask task) { //完成整個(gè)下載過(guò)程 if (callBack != null){callBack.downLoadCompleted(task); } } @Override protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) { //暫停下載 } @Override protected void error(BaseDownloadTask task, Throwable e) { //下載出現(xiàn)錯(cuò)誤 if (callBack != null){callBack.downLoadError(task,e); } } @Override protected void warn(BaseDownloadTask task) { //在下載隊(duì)列中(正在等待/正在下載)已經(jīng)存在相同下載連接與相同存儲(chǔ)路徑的任務(wù) } };} public interface FileDownLoaderCallBack { //文件是否下載完成 void downLoadCompleted(BaseDownloadTask task); //文件是否下載失敗 void downLoadError(BaseDownloadTask task, Throwable e); //文件下載進(jìn)度 void downLoadProgress(BaseDownloadTask task, int soFarBytes, int totalBytes); }}
解壓Zip文件
public class ZipUtils { public static final String TAG = 'ZIP'; public ZipUtils() { }/** * 解壓zip到指定的路徑 * * @param zipFileString ZIP的名稱(chēng) * @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]; // 讀取(字節(jié))字節(jié)到緩沖區(qū) while ((len = inZip.read(buffer)) != -1) {// 從緩沖區(qū)(0)位置寫(xiě)入(字節(jié))字節(jié)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]; // 讀取(字節(jié))字節(jié)到緩沖區(qū) while ((len = inZip.read(buffer)) != -1) {// 從緩沖區(qū)(0)位置寫(xiě)入(字節(jié))字節(jié)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 { //創(chuàng)建ZIP ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFileString)); //創(chuàng)建文件 File file = new File(srcFileString); //壓縮 // LogUtils.LOGE('---->'+file.getParent()+'==='+file.getAbsolutePath()); ZipFiles(file.getParent()+ File.separator, file.getName(), outZip); //完成和關(guān)閉 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(); //沒(méi)有子文件和壓縮 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的名稱(chēng) * @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的名稱(chēng) * @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(); } //開(kāi)始下載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');
此時(shí),心如止水 ,,回家,搜噶。。。。
以上就是android H5本地緩存加載優(yōu)化的實(shí)戰(zhàn)的詳細(xì)內(nèi)容,更多關(guān)于android H5本地緩存加載優(yōu)化的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. asp中response.write("中文")或者js中文亂碼問(wèn)題2. CSS hack用法案例詳解3. ASP.NET Core實(shí)現(xiàn)中間件的幾種方式4. JSP servlet實(shí)現(xiàn)文件上傳下載和刪除5. 詳解盒子端CSS動(dòng)畫(huà)性能提升6. css代碼優(yōu)化的12個(gè)技巧7. 詳解瀏覽器的緩存機(jī)制8. CSS3中Transition屬性詳解以及示例分享9. 怎樣打開(kāi)XML文件?xml文件如何打開(kāi)?10. 怎樣才能用js生成xmldom對(duì)象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?
