2013-02-16 28 views
10

Tôi có một ứng dụng Android chỉ đơn giản là chứa một trang web. Tôi muốn ứng dụng lưu vào bộ nhớ cache các trang của trang web để sử dụng ngoại tuyến.Android WebView không tải trang thứ hai từ bộ nhớ cache

Tôi đang thực hiện một thử nghiệm đơn giản để xem bộ nhớ cache có đang hoạt động hay không nhưng tiếc là không tải được trang tôi đã tải trước đó ở chế độ trực tuyến, khi ngoại tuyến. Để làm cho mọi thứ rõ ràng hơn, tôi tải 2 trang sau khi ở chế độ trực tuyến.

webView.loadUrl("http://www.bmimobile.co.uk/why-bmi.php", getHeaders()); 
webView.loadUrl("http://www.bmimobile.co.uk/", getHeaders()); 

.

Tôi hy vọng rằng trang "why-bmi.php" được tải vào bộ nhớ cache cũng như trang tiếp theo http://www.bmimobile.co.uk/. Trang thứ hai có một liên kết trên trang liên quan đến trang đầu tiên. Nếu sau đó tôi ra khỏi ứng dụng và tắt bộ điều hợp mạng rồi quay lại ứng dụng, trang "http://www.bmimobile.co.uk/" KHÔNG hiển thị nhưng khi tôi nhấp vào liên kết "why-bmi" mà trang KHÔNG hiển thị. Tôi hiển thị tin nhắn bánh mì nướng ngắn nói "lỗi tải trang".

Bất cứ ai có thể cho tôi biết lý do tại sao webview không lưu trong bộ nhớ cache trang đã tải để sử dụng ngoại tuyến sau này?

Đây là hoạt động chính và tôi đã mở rộng đối tượng Application xác định appcachepath.

Cảm ơn trước

Matt

package uk.bmi.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.bmimobile.co.uk/why-bmi.php", getHeaders()); 
      webView.loadUrl("http://www.bmimobile.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.bmimobile.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.bmi.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(); 
     } 
    } 
} 

.Đây là khai thác gỗ khi ứng dụng được đầu tiên nạp trong chế độ trực tuyến

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.bmimobile.co.uk/images/mobile/bg-index.png 
02-16 08:38:56.499: D/skia(8871): ----- started: [1 64] http://www.bmimobile.co.uk/CubeCore/modules/cubeMobile/images/bg-black-bar.png 
02-16 08:38:56.509: D/skia(8871): ----- started: [26 20] http://www.bmimobile.co.uk/images/mobile/home-icon.png 
02-16 08:38:56.529: D/skia(8871): ----- started: [275 189] http://www.bmimobile.co.uk/images/mobile/home-img.png 
02-16 08:38:56.549: D/skia(8871): ----- started: [320 450] http://www.bmimobile.co.uk/images/mobile/welcome/bg-welcome.jpg 
02-16 08:38:56.554: D/skia(8871): ----- started: [270 38] http://www.bmimobile.co.uk/images/mobile/welcome/next.png 
02-16 08:38:56.584: D/skia(8871): ----- started: [16 17] http://www.bmimobile.co.uk/images/mobile/why.png 
02-16 08:38:56.584: D/skia(8871): ----- started: [18 17] http://www.bmimobile.co.uk/images/mobile/services.png 
02-16 08:38:56.584: D/skia(8871): ----- started: [20 15] http://www.bmimobile.co.uk/images/mobile/visit.png 
02-16 08:38:56.589: D/skia(8871): ----- started: [20 15] http://www.bmimobile.co.uk/images/mobile/consultants.png 
02-16 08:38:56.589: D/skia(8871): ----- started: [13 19] http://www.bmimobile.co.uk/images/mobile/contact.png 

.

Đây là lần đăng nhập khi tôi thoát khỏi ứng dụng đã tắt bộ điều hợp mạng sau đó quay lại ứng dụng ở chế độ ngoại tuyến.

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.bmi.mobile/cache 
02-16 08:41:37.804: E/MainActivity(8871): appCachePath = /storage/sdcard0/Android/data/uk.bmi.mobile/cache 
02-16 08:41:37.834: W/dalvikvm(8871): disableGcForExternalAlloc: false 

[edit1] Trên thực tế về kiểm tra chặt chẽ hơn của khai thác gỗ, nó dường như đã thay đổi khi nạp trong chế độ trực tuyến. dưới đây là logcat trong chế độ omline. Dường như có sự cố với bộ nhớ cache.

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.bmi.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.bmi.mobile/cache 
02-19 15:16:10.737: E/MainActivity(5467): appCachePath = /storage/sdcard0/Android/data/uk.bmi.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.bmi.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.bmimobile.co.uk/images/mobile/bg-index.png 
02-19 15:16:21.517: D/skia(5467): ----- started: [1 64] http://www.bmimobile.co.uk/CubeCore/modules/cubeMobile/images/bg-black-bar.png 
02-19 15:16:21.542: D/skia(5467): ----- started: [26 20] http://www.bmimobile.co.uk/images/mobile/home-icon.png 
02-19 15:16:21.577: D/skia(5467): ----- started: [275 189] http://www.bmimobile.co.uk/images/mobile/home-img.png 
02-19 15:16:21.597: D/skia(5467): ----- started: [270 38] http://www.bmimobile.co.uk/images/mobile/welcome/next.png 
02-19 15:16:21.677: D/skia(5467): ----- started: [16 17] http://www.bmimobile.co.uk/images/mobile/why.png 
02-19 15:16:21.677: D/skia(5467): ----- started: [20 15] http://www.bmimobile.co.uk/images/mobile/visit.png 
02-19 15:16:21.677: D/skia(5467): ----- started: [18 17] http://www.bmimobile.co.uk/images/mobile/services.png 
02-19 15:16:21.687: D/skia(5467): ----- started: [20 15] http://www.bmimobile.co.uk/images/mobile/consultants.png 
02-19 15:16:21.687: D/skia(5467): ----- started: [13 19] http://www.bmimobile.co.uk/images/mobile/contact.png 
02-19 15:16:21.692: D/skia(5467): ----- started: [320 450] http://www.bmimobile.co.uk/images/mobile/welcome/bg-welcome.jpg 

.

[ghi chú] nếu tôi nhấp vào nút Why-bmi khi ở chế độ trực tuyến sau đó thoát khỏi ứng dụng, tắt bộ điều hợp rồi nhấp vào nút why-bmi một lần nữa, nó sẽ hiển thị thông báo "error loading page" .

Nếu tôi thay đổi thành các url sau, trang SO của tôi được hiển thị. Nếu tôi nhấp vào liên kết đến trang tiền thưởng của tôi (trang này), sau đó đi ngoại tuyến, trang SO được hiển thị như bạn mong đợi nhưng nếu bạn nhấp vào liên kết tiền thưởng trong chế độ ngoại tuyến thì nó sẽ hiển thị. vì vậy có sự khác biệt giữa trang SO và trang web 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, "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.bmimobile.co.uk/why-bmi.php", getHeaders()); 
      //webView.loadUrl("http://www.bmimobile.co.uk/", getHeaders()); 

      webView.loadUrl("http://stackoverflow.com/users/532462/turtleboy?tab=bounties"); 
      webView.loadUrl("http://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, "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.bmimobile.co.uk/", getHeaders()); 
       webView.loadUrl("http://stackoverflow.com/users/532462/turtleboy"); 


      } 


    } 

[edit2]

<?xml version="1.0" encoding="utf-8"?> 
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="uk.bmi.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.bmi.mobile.permission.C2D_MESSAGE" 
      android:protectionLevel="signature" /> 

     <uses-permission android:name="uk.bmi.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.bmi.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.bmi.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.bmi.mobile" /> 
       </intent-filter> 
      </receiver> 

      <service android:name="uk.bmi.mobile.GCMIntentService" /> 
     </application> 

    </manifest> 
+0

bạn đã đặt quyền READ_EXTERNAL_STORAGE chưa? –

+0

@Yalla T. Xin chào, tôi không cần phải đưa nó vào? – turtleboy

+0

Không, tôi có cần bao gồm không? – turtleboy

Trả lời

1

Có lẽ một giải pháp cho vấn đề của bạn sẽ được thực hiện một HTTP được yêu cầu bên cạnh bốc webview của bạn.

Kết quả của yêu cầu nhận có thể được lưu trữ trong chuỗi liên tục trong sharedpreferences và đó sẽ là HTML mà php của bạn hiển thị.

trong vòng đời Android của bạn, bạn có thể xác định xem ứng dụng đang ẩn, và nếu nó là ẩn bạn có thể tải phiên bản lưu cuối cùng của trang web từ chuỗi

webview.loadData(yourSavedString, "text/html", "UTF-8");

mặc dù nếu có hình ảnh bạn sẽ phải cân nhắc thêm, nhưng nếu hình ảnh không động, bạn có thể lưu trữ chúng trong thư mục nội dung của ứng dụng và thay thế URL trong chuỗi đã lưu bằng vị trí nội dung.

mặc dù nó không giải quyết được tại sao webview của bạn không phải là bộ nhớ đệm, nó sẽ đạt được mục tiêu cuối cùng cùng hoặc xem ẩn trang

8

Đây không phải là câu trả lời chính xác cho câu hỏi của bạn, như bạn đang yêu cầu về bộ nhớ cache webview. Nhưng, nó có thể đạt được kết quả tương tự.

// saving page from web to file 
File file = new File(this.getExternalFilesDir(null), "fileName.html"); 
FileUtils.copyURLToFile(new URL("http://www.bmimobile.co.uk/why-bmi.php"), file); 


// loading saved file in webview 
webview.loadUrl("file://" + file.getPath()); 

Đây là cách tiếp cận linh hoạt hơn, vì bạn có quyền kiểm soát tải, lưu và hơn thế nữa.

+0

Điều này rất tốt. +1 –

1

Một tùy chọn khác rất linh hoạt và mạnh mẽ có thể được sử dụng HTML5 để quản lý bộ nhớ đệm trong ứng dụng của bạn ..

Có một cái nhìn tại http://diveintohtml5.info/offline.html

Bạn chỉ có thể kích hoạt bộ nhớ đệm trong ứng dụng của bạn và quản lý bộ nhớ đệm từ web tự kết thúc.

Trân trọng

Aman Gautam

0

Có những trường hợp không thể lưu trữ một WebView một cách tự nhiên. Nếu tiêu đề trang chứa các trường sau, WebView sẽ không thể lưu trữ nội dung từ trang đó.

Cache-Control: no-store, no-cache 
Pragma: no-cache 

Trong trường hợp này, bạn sẽ phải sửa đổi thuộc tính trang trên máy chủ để khắc phục sự cố lưu vào bộ nhớ cache.

Các vấn đề liên quan