2014-02-08 91 views
0

我正在加載一些HTML字符串到我的應用程序的Web視圖。我的代碼:webView緩存不與loadDataWithBaseURL()

public class AboutUsFragment extends SherlockFragment { 
    String url = CommonUtils.aboutUsUrl; 
    private FetchTask fetchTask = null; 
    WebView webView = null; 
    @SuppressLint("SetJavaScriptEnabled") 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.wrapper, container, false); 
     webView = (WebView) rootView.findViewById(R.id.wrapper); 
     webView.getSettings().setJavaScriptEnabled(true); 
     webView.getSettings().setAppCacheMaxSize(5 * 1024 * 1024); // 5MB 
     webView.getSettings().setAppCachePath(
       getSherlockActivity().getCacheDir().getAbsolutePath()); 
     webView.getSettings().setAllowFileAccess(true); 
     webView.getSettings().setAppCacheEnabled(true); 
     return rootView; 
    } 
    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     // check whether internet connection is available or not 
     ConnectionDetector cd = new ConnectionDetector(getSherlockActivity()); 
     boolean hasConnection = cd.isConnectingToInternet(); 
     if(hasConnection) { 
      webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); 
      fetchTask = new FetchTask(); 
      fetchTask.execute(); 
     } else { 
      MainActivity.networkAvailabilityNotice(getSherlockActivity()); 
      webView.getSettings().setCacheMode(
        WebSettings.LOAD_CACHE_ELSE_NETWORK); 
     } 
    } 

    protected class FetchTask extends AsyncTask<Void, Void, String> { 

     @Override 
     protected String doInBackground(Void... params) { 
      // data fetching from JSON feed 
      return data; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      super.onPostExecute(result); 
      webView.loadDataWithBaseURL("", result, "text/html", "UTF-8", ""); 
     } 

    } 
} 

問題是 - 高速緩存不工作,即網頁未顯示在離線模式下。我設法通過loadURL()方法提供緩存。如何在離線模式下使用loadDataWithBaseURL()提供webview緩存?

回答

0

好吧,我沒有找到任何內置方法來緩存內容,就像典型的webview。所以我將文本保存在一個.txt文件中,並以離線模式讀取該內容。 :)

相關問題