1

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...

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
Mohan Viswa
  • 21
  • 1
  • 3

2 Answers2

0

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.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Look at this? http://stackoverflow.com/questions/7536797/storing-long-string-of-html-in-sqlite-database-causes-unknown-error – Kgrover Mar 28 '13 at 03:14
0

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

NightFury
  • 13,436
  • 6
  • 71
  • 120
Jashan PJ
  • 4,177
  • 4
  • 27
  • 41