2016-07-18 31 views
-1
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); 
webView.getSettings().setDomStorageEnabled(true); 

String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath(); 
webView.getSettings().setAppCachePath(appCachePath); 
webView.getSettings().setAllowFileAccess(true); 
webView.getSettings().setAppCacheEnabled(true); 

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) 
{ 
    webView.getSettings().setAllowUniversalAccessFromFileURLs(true); 
} 

webView.loadUrl(URL); 
+0

代碼格式化 –

回答

0

我不知道你究竟想要什麼,但你可以準確地嘗試這個

private void saveHtmlFile() { 

     String path = Environment.getExternalStorageDirectory().getPath(); 
     String fileName = DateFormat.format("dd_MM_yyyy_hh_mm_ss", System.currentTimeMillis()).toString(); 
     fileName = fileName + ".html"; 
     File file = new File(path, fileName); 
     String html = "<html><head><title>Title</title></head><body>This is random text.</body></html>"; 

     try { 
      FileOutputStream out = new FileOutputStream(file); 
      byte[] data = html.getBytes(); 
      out.write(data); 
      out.close(); 
      Log.e(TAG, "File Save : " + file.getPath()); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

然後,

class LoadListener{ 
    public void processHTML(String html) 
    { 
     Log.e("result",html); 
    } 
} 

...

webView.getSettings().setJavaScriptEnabled(true); 
webView.addJavascriptInterface(new LoadListener(), "HTMLOUT"); 

..

webView.setWebViewClient(new WebViewClient() { 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 

      return true; 
    }    

    @Override 
    public void onPageStarted(WebView view, String url, 
        Bitmap favicon) { 
    } 

    public void onPageFinished(WebView view, String url) { 
     view.loadUrl("javascript:window.HTMLOUT.processHTML('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');"); 
    } 
}); 

或者,您可以根據您的要求嘗試此操作。

https://github.com/dawidsamolyk/Android-Crawler

+0

感謝您reply.But我的要求是,在混合應用IAM工作,所以IAM試圖加載的that.But IAM讓所有HTML和JS文件URL上的每個載所以它似乎緩存不工作 – dalu547