简体   繁体   中英

how to store html page in sqlite database in android

How do you store an html page in a sqlite database in android? I tried to convert the html page into bytes array to store in database. Please help me how to store insert that into db and then open that load in webview...

Don't convert to bytes. Store it as a string in the database- its just text. As for opening it in a webview- you would read it from the database and then call loadData() to load it into the webview.

I too think the Converting to String is the best solution. Here am pasting some codes which may help you. Get the html in the string and save it to db.. (saving to db code is omitted)

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);

check this out :- http://developer.android.com/reference/android/webkit/WebView.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM