Android-WebView

webView.getSettings().setJavaScriptEnabled(true); //設置是否支持js
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); //不做緩存處理
緩存五種模式
LOAD_CACHE_ONLY: 不使用網絡,只讀取本地緩存數據
LOAD_DEFAULT: 根據cache-control決定是否從網絡上取數據。
LOAD_CACHE_NORMAL: API level 17中已經廢棄, 從API level 11開始作用同LOAD_DEFAULT模式
LOAD_NO_CACHE: 不使用緩存,只從網絡獲取數據.
LOAD_CACHE_ELSE_NETWORK,只要本地有,無論是否過期,或者no-cache,都使用緩存中的數據。
webView.getSettings().setDefaultTextEncodingName(“UTF-8”);//設置編碼
webView.getSettings().setLoadsImagesAutomatically(true);//支持網頁圖片加載
webView.getSettings().setDomStorageEnabled(true);// 開啓 DOM storage API 功能
webView.getSettings().setDatabaseEnabled(true);// 開啓 database storage API 功能
webView.getSettings().setDatabasePath(cacheDirPath);// 設置數據庫緩存路徑
webView.getSettings().setAppCachePath(cacheDirPath);// 設置 Application Caches 緩存目錄
webView.getSettings().setAppCacheEnabled(true);// 開啓 Application Caches 功能

刪除緩存

 private void clearCache(String cacheDirPath) {
 deleteDatabase("webview.db");
 deleteDatabase("webviewCache.db");
 CookieSyncManager.createInstance(this);
 CookieManager.getInstance().removeAllCookie();
 File file = new File(cacheDirPath);
 File chromiumFile = new File(StorageUtils.getCachePath(this) + "webviewCacheChromium");
 if (file.exists()) {
 deleteFile(file);
 }
 if (chromiumFile.exists()) {
 deleteFile(chromiumFile);
 }
 }
 /** 刪除緩存 */
 private void deleteFile(File file) {
 // TODO Auto-generated method stub
 if (file != null && file.isDirectory()) {
 for (File item : file.listFiles()) {
 item.delete();
 }
 file.delete();
 }
 }

Jsoup.jar 用來解析Html頁面;
步驟:
1、獲取到要加載的網絡的html元素;
可用:

public String getHtmlString(String urlString) {
try {
URL url = new URL(urlString);
URLConnection ucon = url.openConnection();
InputStream instr = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(instr);
ByteArrayBuffer baf = new ByteArrayBuffer(500);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
return EncodingUtils.getString(baf.toByteArray(), “gbk”);
} catch (Exception e) {
return “”;
}
}
也可以:
Document doc = Jsoup.parse(new URL(“http://www.cnbeta.com”), 5000);
2、用Jsoup 解析這個html元素,返回一個Document對象;
Document document = Jsoup.parse(htmlString);
3、獲取每個節點的標籤;
String title = document.head().getElementsByTag(“title”).text();

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章