探究背景:作为一名安卓新手,要实现这个是过程是比较简单的,因为思路需要在网上寻找,各项细节技术都比较不熟练,不过经过一整天的探究,总算是有了点成果
整体思路:
1.首先通过jsoup下载html,保存在本地,关于保存路径挺纠结的,后来参考了
http://www.cnblogs.com/freeliver54/archive/2011/09/16/2178910.html文档,使用安卓自带的
openFileOutput和openFileInput在
//读文件在./data/data/com.tt/files/下面存取我们的html文件
代码如下package com.example.htmlcache; import java.io.FileInputStream; import java.io.FileOutputStream; import org.apache.http.util.EncodingUtils; import android.content.Context; //http://www.cnblogs.com/freeliver54/archive/2011/09/16/2178910.html // http://www.th7.cn/web/html-css/201410/62906.shtml public class FileUtils { Context context; public FileUtils(Context context) { super(); this.context = context; } public void writeFileData(String fileName,String message){ try{ FileOutputStream fout =context.openFileOutput(fileName, Context.MODE_PRIVATE); byte [] bytes = message.getBytes(); fout.write(bytes); fout.close(); } catch(Exception e){ e.printStackTrace(); } } public String readFileData(String fileName){ String res=""; try{ FileInputStream fin = context.openFileInput(fileName); int length = fin.available(); byte [] buffer = new byte[length]; fin.read(buffer); res = EncodingUtils.getString(buffer, "UTF-8"); fin.close(); } catch(Exception e){ e.printStackTrace(); } return res; } }
2.当我们请求一个url,如何映射到本地文件呢,这里就要用到SQLite数据库保存我们的URL,另外我们在存储html文件名时使用
固定名称+id的方式,这样当我们请求一个url时候,首先查找数据库是否有对应的记录,从而加载本地文件
package com.example.htmlcache; import java.io.IOException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import android.content.Context; public class ParseHtmlService { Context context; String cachePage ="cacgePage"; public ParseHtmlService(Context context) { super(); this.context = context; } public String getHtmlByUrl(String url, String news_title, String news_date) { String data=""; int id = DBOpenHelper.getInstance(context).queryId(url); if(id != -1){ //读取本地html data = new FileUtils(context).readFileData(cachePage+id+".html"); return data; } Boolean flag = false; Document document = null; data = "<body>" + "<center><h2 style='font-size:16px;'>" + news_title + "</h2></center>"; data = data + "<p align='left' style='margin-left:10px'>" + "<span style='font-size:10px;'>" + news_date + "</span>" + "</p>"; data = data + "<hr size='1' />"; try { document = Jsoup.connect(url).timeout(9000).get(); String body = document.getElementsByTag("body").html(); if(body.length()>10){ flag = true; }else{ body="数据加载失败"; } data+=body; data = data + "</body>"; } catch (IOException e) { e.printStackTrace(); data = "数据加载异常" + "</body>"; } if(flag){ long rowId = DBOpenHelper.getInstance(context).insert(url); new FileUtils(context).writeFileData(cachePage+rowId+".html", data); } return data; } }
3.那么如何缓存图片呢,这里的解决方案是使用webview查看一下,这样webview自身会对图片进行缓存的
package com.example.htmlcache; import android.content.Context; import android.os.AsyncTask; import android.webkit.WebSettings; import android.webkit.WebView; public class MyAsnycTask extends AsyncTask<String, String, String> { WebView webView; Context context; @Override protected String doInBackground(String... urls) { String htmls = new ParseHtmlService(context).getHtmlByUrl(urls[0],urls[1],urls[2]); return htmls; } public MyAsnycTask(WebView webView, Context context) { super(); this.webView = webView; this.context = context; } @Override protected void onPostExecute(String htmls) { super.onPostExecute(htmls); if(webView != null){ webView.loadDataWithBaseURL (null, htmls, "text/html", "utf-8",null); }else{ WebView wv = new WebView(context); WebSettings settings = wv.getSettings(); settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); settings.setAppCacheEnabled(true); wv.loadDataWithBaseURL (null, htmls, "text/html", "utf-8",null); } } } DEMO下载地址:http://pan.baidu.com/s/1jGj98SA