大佬教程收集整理的这篇文章主要介绍了Android WebView没有从缓存中加载第二页,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在进行一个简单的测试,看看缓存是否正常工作,但不幸的是,当我离线时,加载了一个我以前在线模式加载的页面.为了使事情更清晰,在加载在线模式下,我将加载以下2页.
webView.loadUrl("http://www.bR_441_11845@imobile.co.uk/why-bmi.PHP",getHeaders()); webView.loadUrl("http://www.bR_441_11845@imobile.co.uk/",getHeaders());
.
我希望将“why-bmi.PHP”页面加载到缓存以及随后的页面http://www.bmimobile.co.uk/.后一页面有一个链接,指的是第一页.如果我然后从应用程序出来,关闭网络适配器,然后回到应用程序“http://www.bmimobile.co.uk/”页面显示,但是当我点击该页面的“为什么 – bmi”链接不显示.我的口味信息显示说“错误加载页面”.
任何人都可以告诉我为什么webview没有缓存加载的页面以供以后离线使用?
这是主要的活动,我已经扩展了定义Appcachepath的Application对象.
提前致谢
马特
package uk.bR_441_11845@i.mobile; import java.io.File; import java.util.HashMap; import java.util.Map; import android.app.Activity; import android.content.Context; import android.net.Connectivitymanager; import android.net.NetworkInfo; import android.os.bundle; import android.util.Log; import android.webkit.WebSetTings; import android.webkit.WebView; import android.webkit.WebViewClient; public class MainActivity extends Activity { private WebView webView; private static final String TAG = MainActivity.class.getSimplename(); ApplicationExt bmiAppObj; //instruct server to set it's headers to make resources cachable private Map<String,String> getHeaders() { Map<String,String> headers = new HashMap<String,String>(); headers.put("IS_ALEX_APP","1"); return headers; } @Override public void onCreate(Bundle savedInstanceStatE) { super.onCreate(savedInstanceStatE); setContentView(R.layout.activity_main); Log.e(tag,"in onCreate in mainactivity"); } //end of oncreate private Boolean isNetworkAvailable() { Connectivitymanager connectivitymanager = (Connectivitymanager) getSystemservice(Context.CONNECTIVITY_serviCE); NetworkInfo activeNetworkInfo = connectivitymanager.getActiveNetworkInfo(); return activeNetworkInfo != null; } @Override protected void onResume() { super.onResume(); Log.e(tag,"in onResume in mainactivity"); webView = (WebView)findViewById(R.id.webView1); bmiAppObj = (ApplicationExt)getApplication(); if(isNetworkAvailable() == truE){ webView.getSetTings().setSupportZoom(true); webView.getSetTings().setBuilTinZoomControls(true); webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); webView.setScrollbarFadingEnabled(true); webView.getSetTings().setLoadsImagesAutomatically(true); webView.getSetTings().setDomStorageEnabled(true); webView.getSetTings().setAppCacheEnabled(true); // Set cache size to 8 mb by default. should be more than enough webView.getSetTings().setAppCacheMaxSize(1024*1024*8); // This next one is crazy. It's the DEFAULT LOCATIOn for your app's cache // But it didn't work for me without this line. // updatE: no hardcoded path. Thanks to Kevin Hawkins String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath(); Log.e(tag,"appCachePath = " + appCachePath); webView.getSetTings().setAppCachePath(appCachePath); webView.getSetTings().setAllowFileAccess(true); webView.getSetTings().setJavaScriptEnabled(true); // Load the URLs inside the WebView,not in the external web browser webView.setWebViewClient(new WebViewClient()); webView.getSetTings().setCacheMode(WebSetTings.LOAD_DEFAULT); webView.loadUrl("http://www.bR_441_11845@imobile.co.uk/why-bmi.PHP",getHeaders()); webView.loadUrl("http://www.bR_441_11845@imobile.co.uk/",getHeaders()); }else{ webView.getSetTings().setSupportZoom(true); webView.getSetTings().setBuilTinZoomControls(true); webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); webView.setScrollbarFadingEnabled(true); webView.getSetTings().setLoadsImagesAutomatically(true); webView.getSetTings().setDomStorageEnabled(true); webView.getSetTings().setAppCacheEnabled(true); // Set cache size to 8 mb by default. should be more than enough webView.getSetTings().setAppCacheMaxSize(1024*1024*8); // This next one is crazy. It's the DEFAULT LOCATIOn for your app's cache // But it didn't work for me without this line. // updatE: no hardcoded path. Thanks to Kevin Hawkins String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath(); Log.e(tag,"appCachePath = " + appCachePath); webView.getSetTings().setAppCachePath(appCachePath); webView.getSetTings().setAllowFileAccess(true); webView.getSetTings().setJavaScriptEnabled(true); // Load the URLs inside the WebView,not in the external web browser webView.setWebViewClient(new WebViewClient()); webView.getSetTings().setCacheMode(WebSetTings.LOAD_CACHE_ONLY); webView.loadUrl("http://www.bR_441_11845@imobile.co.uk/",getHeaders()); } } @Override public File getCacheDir() { // NOTE: this method is used in Android 2.1 Log.e(tag,"getcachedir"); return getApplicationContext().getCacheDir(); } @Override protected void onSaveInstanceState(Bundle outStatE) { super.onSaveInstanceState(outStatE); // Save the state of the WebView webView.saveState(outStatE); } @Override protected void onRestoreInstanceState(Bundle savedInstanceStatE) { super.onRestoreInstanceState(savedInstanceStatE); // Restore the state of the WebView webView.restoreState(savedInstanceStatE); } }//end of mainActivity
.
package uk.bR_441_11845@i.mobile; import java.io.File; import android.app.Application; import android.os.Environment; import android.util.Log; public class ApplicationExt extends Application { private static final String TAG = ApplicationExt.class.getSimplename(); // NOTE: the content of this path will be deleted // when the application is uninstalled (Android 2.2 and higher) protected File extStorageAppBasePath; protected File extStorageAppCachePath; Webservice webservice; BmiDB bmiDb; @Override public void onCreate() { super.onCreate(); Log.e(tag,"inside appext"); webservice = new Webservice(this); bmiDb = new BmiDB(this); // check if the external storage is writeable if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { // Retrieve the base path for the application in the external storage File externalStorageDir = Environment.getExternalStorageDirectory(); if (externalStorageDir != null) { // {SD_PATH}/Android/data/com.devahead.androidwebviewcacheonsd extStorageAppBasePath = new File(externalStorageDir.getAbsolutePath() + File.separator + "Android" + File.separator + "data" + File.separator + getPackagename()); } if (extStorageAppBasePath != null) { // {SD_PATH}/Android/data/com.devahead.androidwebviewcacheonsd/cache extStorageAppCachePath = new File(extStorageAppBasePath.getAbsolutePath() + File.separator + "cache"); Boolean isCachePathAvailable = true; if (!extStorageAppCachePath.exists()) { // Create the cache path on the external storage isCachePathAvailable = extStorageAppCachePath.mkdirs(); } if (!isCachePathAvailablE) { // Unable to create the cache path extStorageAppCachePath = null; } } } }//end of onCreate @Override public File getCacheDir() { // NOTE: this method is used in Android 2.2 and higher if (extStorageAppCachePath != null) { // Use the external storage for the cache Log.e(tag,"extStorageAppCachePath = " + extStorageAppCachePath); return extStorageAppCachePath; } else { // /data/data/com.devahead.androidwebviewcacheonsd/cache return super.getCacheDir(); } } }
这是当应用程序首次加载在在线模式时的日志记录
02-16 08:38:52.744: I/NONPRIME(8871): <CallBACkProxy> Send to WebViewClient. 02-16 08:38:56.314: D/skia(8871): ----- started: [1 325] http://www.bR_441_11845@imobile.co.uk/images/mobile/bg-index.png 02-16 08:38:56.499: D/skia(8871): ----- started: [1 64] http://www.bR_441_11845@imobile.co.uk/CubeCore/modules/cubeMobile/images/bg-black-bar.png 02-16 08:38:56.509: D/skia(8871): ----- started: [26 20] http://www.bR_441_11845@imobile.co.uk/images/mobile/home-icon.png 02-16 08:38:56.529: D/skia(8871): ----- started: [275 189] http://www.bR_441_11845@imobile.co.uk/images/mobile/home-img.png 02-16 08:38:56.549: D/skia(8871): ----- started: [320 450] http://www.bR_441_11845@imobile.co.uk/images/mobile/welcome/bg-welcome.jpg 02-16 08:38:56.554: D/skia(8871): ----- started: [270 38] http://www.bR_441_11845@imobile.co.uk/images/mobile/welcome/next.png 02-16 08:38:56.584: D/skia(8871): ----- started: [16 17] http://www.bR_441_11845@imobile.co.uk/images/mobile/why.png 02-16 08:38:56.584: D/skia(8871): ----- started: [18 17] http://www.bR_441_11845@imobile.co.uk/images/mobile/services.png 02-16 08:38:56.584: D/skia(8871): ----- started: [20 15] http://www.bR_441_11845@imobile.co.uk/images/mobile/visit.png 02-16 08:38:56.589: D/skia(8871): ----- started: [20 15] http://www.bR_441_11845@imobile.co.uk/images/mobile/consultants.png 02-16 08:38:56.589: D/skia(8871): ----- started: [13 19] http://www.bR_441_11845@imobile.co.uk/images/mobile/contact.png
.
这是当我从应用程序中退出网络适配器,然后在离线模式下回到应用程序的日志记录.
02-16 08:41:37.799: E/MainActivity(8871): in onResume in mainactivity 02-16 08:41:37.804: E/ApplicationExt(8871): extStorageAppCachePath = /storage/sdcard0/Android/data/uk.bR_441_11845@i.mobile/cache 02-16 08:41:37.804: E/MainActivity(8871): appCachePath = /storage/sdcard0/Android/data/uk.bR_441_11845@i.mobile/cache 02-16 08:41:37.834: W/dalvikvm(8871): disableGcForExternalAlloc: false
[EDIT1]
实际上,仔细检查日志记录,在加载在线模式下似乎已经改变了.以下是omline模式下的logcat.缓存存储似乎有问题.
02-19 15:16:10.497: E/ApplicationExt(5467): inside appext 02-19 15:16:10.687: E/ApplicationExt(5467): extStorageAppCachePath = /storage/sdcard0/Android/data/uk.bR_441_11845@i.mobile/cache 02-19 15:16:10.722: E/MainActivity(5467): in onCreate in mainactivity 02-19 15:16:10.727: E/MainActivity(5467): in onResume in mainactivity 02-19 15:16:10.737: E/ApplicationExt(5467): extStorageAppCachePath = /storage/sdcard0/Android/data/uk.bR_441_11845@i.mobile/cache 02-19 15:16:10.737: E/MainActivity(5467): appCachePath = /storage/sdcard0/Android/data/uk.bR_441_11845@i.mobile/cache 02-19 15:16:10.792: E/(5467): file /data/data/com.nvidia.NvCPLSvc/files/driverlist.txt: not found! 02-19 15:16:10.792: I/(5467): AttempTing to load EGL implementation /system/lib//egl/libEGL_tegra_impl 02-19 15:16:10.807: I/(5467): Loaded EGL implementation /system/lib//egl/libEGL_tegra_impl 02-19 15:16:10.842: I/(5467): Loading GLESv2 implementation /system/lib//egl/libGLESv2_tegra_impl 02-19 15:16:10.882: E/sqliteLog(5467): (1) no such table: CacheGroups 02-19 15:16:10.882: D/WebKit(5467): ERROR: 02-19 15:16:10.882: D/WebKit(5467): Application Cache Storage: Failed to execute statement "deletE FROM CacheGroups" error "no such table: CacheGroups" 02-19 15:16:10.882: D/WebKit(5467): external/webkit/source/WebCore/loader/appcache/ApplicationCacheStorage.cpp(558) : bool WebCore::ApplicationCacheStorage::executesqlCommand(const WTF::string&) 02-19 15:16:10.882: E/sqliteLog(5467): (1) no such table: Caches 02-19 15:16:10.882: D/WebKit(5467): ERROR: 02-19 15:16:10.882: D/WebKit(5467): Application Cache Storage: Failed to execute statement "deletE FROM Caches" error "no such table: Caches" 02-19 15:16:10.882: D/WebKit(5467): external/webkit/source/WebCore/loader/appcache/ApplicationCacheStorage.cpp(558) : bool WebCore::ApplicationCacheStorage::executesqlCommand(const WTF::string&) 02-19 15:16:10.882: E/sqliteLog(5467): (1) no such table: Origins 02-19 15:16:10.882: D/WebKit(5467): ERROR: 02-19 15:16:10.882: D/WebKit(5467): Application Cache Storage: Failed to execute statement "deletE FROM Origins" error "no such table: Origins" 02-19 15:16:10.882: D/WebKit(5467): external/webkit/source/WebCore/loader/appcache/ApplicationCacheStorage.cpp(558) : bool WebCore::ApplicationCacheStorage::executesqlCommand(const WTF::string&) 02-19 15:16:10.882: E/sqliteLog(5467): (1) no such table: deletedCacheresources 02-19 15:16:10.992: E/ApplicationExt(5467): extStorageAppCachePath = /storage/sdcard0/Android/data/uk.bR_441_11845@i.mobile/cache 02-19 15:16:11.022: W/dalvikvm(5467): disableGcForExternalAlloc: false 02-19 15:16:13.787: I/NONPRIME(5467): <CallBACkProxy> Send to WebViewClient. 02-19 15:16:21.427: D/skia(5467): ----- started: [1 325] http://www.bR_441_11845@imobile.co.uk/images/mobile/bg-index.png 02-19 15:16:21.517: D/skia(5467): ----- started: [1 64] http://www.bR_441_11845@imobile.co.uk/CubeCore/modules/cubeMobile/images/bg-black-bar.png 02-19 15:16:21.542: D/skia(5467): ----- started: [26 20] http://www.bR_441_11845@imobile.co.uk/images/mobile/home-icon.png 02-19 15:16:21.577: D/skia(5467): ----- started: [275 189] http://www.bR_441_11845@imobile.co.uk/images/mobile/home-img.png 02-19 15:16:21.597: D/skia(5467): ----- started: [270 38] http://www.bR_441_11845@imobile.co.uk/images/mobile/welcome/next.png 02-19 15:16:21.677: D/skia(5467): ----- started: [16 17] http://www.bR_441_11845@imobile.co.uk/images/mobile/why.png 02-19 15:16:21.677: D/skia(5467): ----- started: [20 15] http://www.bR_441_11845@imobile.co.uk/images/mobile/visit.png 02-19 15:16:21.677: D/skia(5467): ----- started: [18 17] http://www.bR_441_11845@imobile.co.uk/images/mobile/services.png 02-19 15:16:21.687: D/skia(5467): ----- started: [20 15] http://www.bR_441_11845@imobile.co.uk/images/mobile/consultants.png 02-19 15:16:21.687: D/skia(5467): ----- started: [13 19] http://www.bR_441_11845@imobile.co.uk/images/mobile/contact.png 02-19 15:16:21.692: D/skia(5467): ----- started: [320 450] http://www.bR_441_11845@imobile.co.uk/images/mobile/welcome/bg-welcome.jpg
.
[笔记]
如果我在在线模式下点击why-bmi按钮,然后从应用程序中出来,请关闭适配器,然后再次单击why-bmi按钮,然后显示“错误加载页面”消息.
如果我改变到以下url,我的SO页面显示.如果我点击链接到我的赏金页面(这个页面),然后离线,这个SO页面是按照你所期望的显示,但是如果你在离线模式下点击赏金链接,那么它显示.所以SO站点和bmi站点之间存在差异.
if(isNetworkAvailable() == truE){ webView.getSetTings().setSupportZoom(true); webView.getSetTings().setBuilTinZoomControls(true); webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); webView.setScrollbarFadingEnabled(true); webView.getSetTings().setLoadsImagesAutomatically(true); webView.getSetTings().setDomStorageEnabled(true); webView.getSetTings().setAppCacheEnabled(true); // Set cache size to 8 mb by default. should be more than enough webView.getSetTings().setAppCacheMaxSize(1024*1024*8); // This next one is crazy. It's the DEFAULT LOCATIOn for your app's cache // But it didn't work for me without this line. // updatE: no hardcoded path. Thanks to Kevin Hawkins String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath(); Log.e(tag,not in the external web browser webView.setWebViewClient(new WebViewClient()); webView.getSetTings().setCacheMode(WebSetTings.LOAD_DEFAULT); //webView.loadUrl("http://www.bR_441_11845@imobile.co.uk/why-bmi.PHP",getHeaders()); //webView.loadUrl("http://www.bR_441_11845@imobile.co.uk/",getHeaders()); webView.loadUrl("https://stackoverflow.com/users/532462/turtleboy?tab=bounties"); webView.loadUrl("https://stackoverflow.com/users/532462/turtleboy"); }else{ webView.getSetTings().setSupportZoom(true); webView.getSetTings().setBuilTinZoomControls(true); webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); webView.setScrollbarFadingEnabled(true); webView.getSetTings().setLoadsImagesAutomatically(true); webView.getSetTings().setDomStorageEnabled(true); webView.getSetTings().setAppCacheEnabled(true); // Set cache size to 8 mb by default. should be more than enough webView.getSetTings().setAppCacheMaxSize(1024*1024*8); // This next one is crazy. It's the DEFAULT LOCATIOn for your app's cache // But it didn't work for me without this line. // updatE: no hardcoded path. Thanks to Kevin Hawkins String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath(); Log.e(tag,not in the external web browser webView.setWebViewClient(new WebViewClient()); webView.getSetTings().setCacheMode(WebSetTings.LOAD_CACHE_ONLY); // webView.loadUrl("http://www.bR_441_11845@imobile.co.uk/",getHeaders()); webView.loadUrl("https://stackoverflow.com/users/532462/turtleboy"); } }
[EDIT2]
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://scheR_441_11845@as.android.com/apk/res/android" package="uk.bR_441_11845@i.mobile" android:versionCode="5" android:versionName="1.0.4" > <!-- GCM requires Android SDK version 2.2 (API level <img src="http://www.androidhive.info/wp-includes/images/smilies/icon_cool.gif" alt="8)" class="wp-smiley"> or above. --> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" /> <!-- GCM connects to Internet services. --> <uses-permission android:name="android.permission.INTERNET" /> <!-- GCM requires a Google Account. --> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <!-- Keeps the processor from sleeping when a message is received. --> <uses-permission android:name="android.permission.WAKE_LOCK" /> <!-- Creates a custom permission so only this app can receive its messages. --> <permission android:name="uk.bR_441_11845@i.mobile.permission.C2D_messaGE" android:protectionLevel="signature" /> <uses-permission android:name="uk.bR_441_11845@i.mobile.permission.C2D_messaGE" /> <!-- This app has permission to register and receive data message. --> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <!-- Network State Permissions to detect Internet status --> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- Permission to vibrate --> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permisson.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <!-- Main activity. --> <application android:icon="@drawable/bmi_icon" android:label="@String/app_name" android:name="uk.bR_441_11845@i.mobile.ApplicationExt" > <!-- Register Activity --> <activity android:name=".RegisterActivity" android:label="@String/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- Main Activity --> <activity android:name="uk.bR_441_11845@i.mobile.MainActivity" android:configChanges="orientation|keyboardHidden" android:label="@String/app_name" android:screenOrientation="porTrait" > </activity> <receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" > <intent-filter> <!-- Receives the actual messages. --> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <!-- Receives the registration id. --> <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> <category android:name="uk.bR_441_11845@i.mobile" /> </intent-filter> </receiver> <service android:name="uk.bR_441_11845@i.mobile.GCMIntentservice" /> </application> </manifest>
// saving page from web to file File file = new File(this.getExternalFilesDir(null),"filename.html"); FileUtils.copyURLToFile(new URL("http://www.bR_441_11845@imobile.co.uk/why-bmi.PHP"),filE); // loading saved file in webview webview.loadUrl("file://" + file.getPath());
以上是大佬教程为你收集整理的Android WebView没有从缓存中加载第二页全部内容,希望文章能够帮你解决Android WebView没有从缓存中加载第二页所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。