你如何在android的sqlite数据库中存储一个html页面?我试图将 html 页面转换为字节数组以存储在数据库中。请帮助我如何将插入存储到数据库中,然后在 webview 中打开该负载...
问问题
2642 次
2 回答
0
不要转换为字节。将其作为字符串存储在数据库中 - 它只是文本。至于在 webview 中打开它 - 您将从数据库中读取它,然后调用 loadData() 将其加载到 webview 中。
于 2013-03-28T03:07:46.423 回答
0
我也认为转换为字符串是最好的解决方案。在这里粘贴一些可能对您有所帮助的代码。获取字符串中的html并保存到db..(省略保存到db代码)
private String getDownloadButtonOnly(String url){
HttpGet pageGet = new HttpGet(url);
ResponseHandler<String> handler = new ResponseHandler<String>() {
public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
HttpEntity entity = response.getEntity();
String html;
if (entity != null) {
html = EntityUtils.toString(entity);
return html;
} else {
return null;
}
}
};
pageHTML = null;
try {
while (pageHTML==null){
pageHTML = client.execute(pageGet, handler);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Pattern pattern = Pattern.compile("<h2>Direct Down.+?</h2>(</div>)*(.+?)<.+?>", Pattern.DOTALL);
Matcher matcher = pattern.matcher(pageHTML);
String displayHTML = null;
while(matcher.find()){
displayHTML = matcher.group();
}
return displayHTML;
}
// after retreiving string from db,
String summary = "<html><body>You scored <b>192</b> points.</body></html>";
//load the data
webview.loadData(summary, "text/html", null);
看看这个:- http://developer.android.com/reference/android/webkit/WebView.html
于 2013-03-28T03:53:31.583 回答