HTML5 OFFLINE SOLUTION

本文详细介绍了如何配置HTML5应用以实现离线缓存功能。包括设置正确的MIME类型、指定缓存清单文件、配置缓存内容及特殊平台(如iOS和Android)上的额外设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Although PhoneGap is an offline solution where the HTML, CSS and JS files etc are held in the assets/www folder within the app, there are some cases where you might want the app to point to a remote server where the HTML5 file and its CSS/JS are located. They can be downloaded onto your device and ran as offline files when the device has no Internet connection.

The requirements to trigger the HTML5 offline cache are detailed below and each requirement must be implemented with care, otherwise it will not work:

1) The MIME Type for a Manifest file.
This is very important. Previously I have followed a solution to add a mime type by using a .htaccess file and place it together with index.html of my HTML5 app, but it didn’t work.

In my case, I was using the Tomcat server and the surefire way that the .manifest file will be correctly used by the server, is to modify “/conf/web.xml” and add in the mime mapping as below:

<mime-mapping>
<extension>manifest</extension>
<mime-type>text/cache-manifest</mime-type>
</mime-mapping>

 

2) Specify the Manifest File
This is a standard configuration. For every HTML file that wants to use the cache manifest, they have to update their tag to

3) Configure the Manifest File
The manifest file is a text file that you have configure to tell the HTML5 app what is to be cached for offline use. A sample file is below:
CACHE MANIFEST

# Version 0.1

# Explicitly cached entries
index.html
jqtouch/jqtouch.css
jqtouch/jqtouch.js
jqtouch/jquery-1.4.2.min.js
js/common.js
images/1.png
images/2.png

# All other resources (e.g. sites) require the user to be online.
NETWORK:
*

* note that the manifest file only caches static resources such as .html, .js and images, so files like JSP pages will not be applicable here.

***VERY IMPT: Each entry to be cached must be correctly named and if the entry is there, the physical file must be there, otherwise the whole caching will fail!

4) For iOS, steps 1-3 are sufficient for HTML5 offline caching to work. However, in the case of Android, we need to fix the Android Shell Native App to enable HTML5 caching:

In your App.java class, there are these lines of codes to be added as part of the solution to enable HTML5 caching:

public class App extends DroidGap {
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
super.appView.getSettings().setDomStorageEnabled(true);
super.appView.getSettings().setAppCacheMaxSize(1024*1024*8);
super.appView.getSettings().setAppCachePath("/data/data/com.yourdomain/cache");
super.appView.getSettings().setAllowFileAccess(true);
super.appView.getSettings().setAppCacheEnabled(true);
super.appView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
...
}
...
}

 =======================================

对于UIWebView不支持html5 cache的问题,可以参考:

http://stackoverflow.com/questions/1540240/html5-cache-manifest-in-a-uiwebview

apply plugin: 'jacoco' //gradlew clean & gradlew createOfflineTestCoverageReport & gradlew jacocoTestReport tasks.withType(Test) { jacoco.includeNoLocationClasses = true jacoco.excludes = ['jdk.internal.*'] } configurations { jacocoAnt jacocoRuntime } jacoco { toolVersion = "0.8.5" } def offline_instrumented_outputDir = "$buildDir.path/intermediates/classes-instrumented/debugCoverage" tasks.withType(Test) { jacoco.includeNoLocationClasses = true jacoco.excludes = ['jdk.internal.*'] } def coverageSourceDirs = [ 'src/main/java' ] task jacocoTestReport(type: JacocoReport, dependsOn: "test") { group = "Reporting" description = "Generate Jacoco coverage reports" classDirectories.from = fileTree( dir: './build/intermediates/javac/debugCoverage/classes/', excludes: [ // '**/R.class', // '**/R$*.class', // '**/Manifest*.*', // '**/*Test*.*', // 'android/**/*.*', // '**/BuildConfig.*', // '**/**Bean.class', // '**/inputmethod/*', // '**/config/*.*', // '**/flex/*.*', // '**/AssetsUtils.class' ] ) getSourceDirectories().setFrom(files(coverageSourceDirs)) getExecutionData().setFrom(files('./build/jacoco/testDebugUnitTest.exec')) } jacocoTestReport { reports { // xml.enabled true // html.enabled true xml.required = true html.required = true html.destination file("build/test-results/jacocoHtml") } } /* This task is used to create offline instrumentation of classes for on-the-fly instrumentation coverage tool like Jacoco. See jacoco classId * and Offline Instrumentation from the jacoco site for more info. * * In this case, some classes mocked using PowerMock were reported as 0% coverage on jacoco & Sonarqube. The issue between PowerMock and jacoco * is well documented, and a possible solution is offline Instrumentation (not so well documented for gradle). * * In a nutshell, this task: * - Pre-instruments the original *.class files * - Puts the instrumented classes path at the beginning of the task's classpath (for report purposes) * - Runs test & generates a new exec file based on the pre-instrumented classes -- as opposed to on-the-fly instrumented class files generated by jacoco. * * It is currently not implemented to run prior to any other existing tasks (like test, jacocoTestReport, etc...), therefore, it should be called * explicitly if Offline Instrumentation report is needed. * * Usage: gradle clean & gradle createOfflineInstrTestCoverageReport & gradle jacocoTestReport * - gradle clean //To prevent influence from any previous task execution * - gradle createOfflineInstrTestCoverageReport //To generate *.exec file from offline instrumented class * - gradle jacocoTestReport //To generate html report from newly created *.exec task */ task createOfflineTestCoverageReport(dependsOn: ['instrument', 'testDebugCoverageUnitTest']) { group = "reporting" doLast { ant.taskdef(name: 'report', classname: 'org.jacoco.ant.ReportTask', classpath: configur3+ations.jacocoAnt.asPath) ant.report() { executiondata { ant.file(file: "$buildDir.path/jacoco/testDebugCoverageUnitTest.exec") } structure(name: 'Example') { classfiles { fileset(dir: "$project.buildDir/intermediates/javac/debugCoverage") } sourcefiles { fileset(dir: 'src/main/java') } } //Uncomment if we want the task to generate jacoco html reports. However, the current script does not exclude files. //An alternative is to used jacocoTestReport after this task finishes //html(destdir: "$buildDir.path/reports/jacocoHtml") } } } createOfflineTestCoverageReport.dependsOn(clean) /* * Part of the Offline Instrumentation process is to add the jacoco runtime to the class path along with the path of the instrumented files. */ gradle.taskGraph.whenReady { graph -> if (graph.hasTask(instrument)) { tasks.withType(Test) { doFirst { systemProperty 'jacoco-agent.destfile', buildDir.path + '/jacoco/testDebugCoverageUnitTest.exec' classpath = files(offline_instrumented_outputDir) + classpath + configurations.jacocoRuntime } } } } /* * Instruments the classes per se */ task instrument(dependsOn:'compileDebugUnitTestSources') { doLast { println 'Instrumenting classes' ant.taskdef(name: 'instrument', classname: 'org.jacoco.ant.InstrumentTask', classpath: configurations.jacocoAnt.asPath) ant.instrument(destdir: offline_instrumented_outputDir) { fileset(dir: "$buildDir.path/intermediates/javac/debugCoverage") } } } 上面是我的jacoco配置,为什么我运行之后,Androidstudio的右边会显示EngineeringMode【jacocoTestReport】,但是我的build/test-results/jacocoHtml/目录下只有一个文件夹jacoco-resources和两个html分别是index.html,jacoco-sessions.html
最新发布
07-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值