12-01-2011 12:58 PM
Hi, I want to save an HTML document complete with all of it resources (images, css, etc.) into the device's external memory so that I can display it later in the BrowserField without having to reconnect to the internet (a sort of web cache, only it's located on the external memory). I tried using the code below to do this but it doesn't seem to save the document's dependent resources such as images, and css. CMIIW.
try
{ StringBuffer raw = null; HttpConnection httpConn = (HttpConnection) Connector.open("http://google.com"); int status = httpConn.getResponseCode(); if (status == HttpConnection.HTTP_OK) { InputStream input = httpConn.openInputStream(); byte[] data = new byte[256]; int len = 0; int size = 0; raw = new StringBuffer(); while (-1 != (len = input.read(data))) { raw.append(new String(data, 0, len)); size += len; } } httpConn.close(); DataOutputStream os = null; FileConnection fconn = null; try { fconn = (FileConnection) Connector.open("file://" + "/SDCard/BlackBerry/" + "google.html", Connector.READ_WRITE); if (fconn.exists()) { fconn.delete(); } if (!fconn.exists()) { fconn.create(); } os = fconn.openDataOutputStream(); os.write(raw.toString().getBytes()); } catch (IOException e) { } finally { try { if (null != os) os.close(); if (null != fconn) fconn.close(); } catch (IOException e) { } } } catch(Exception ex) { }
Another solution I am thinking of is adding a BrowserFieldListener on the BrowserField and saving the document when the documentLoaded method in the listener gets called. But I'm not sure how to do this and whether it would even work.
I'm quite sure it's possible since the BlackBerry's browser can save web pages for offline viewing. Can someone help me with this?
Thanks in advance.
Solved! Go to Solution.
12-01-2011 01:58 PM
If you want to save it yourself, you are going to have to parse the HTML and download the component parts (images etc) yourself. You will also have to change the references so that they refer to the local storage repository rather then the internet URL that the HTML might refer to.
I think the BrowserField hooks will let you save each of the individual components, but you are still left with needing to change the URL in the web page to match where the downloaded files are stored.
This might be challenge to do generically, but if you only going to allow the download of some sites that you maintain, then you simplify the processing using relative URLs and easy to parse HTML so it might not be that much work.
12-01-2011 03:48 PM
Thanks for your quick reply Peter.
I think I'll be able to redirect the web request to the local storage repository by overriding the handleResourceRequest method in ProtocolController interface.
But I still don't understand how to parse each component in the web page. And which BrowserField method will let me save these components?
Could you possibly give me an example or a link to an article that could explain about this? =)
local storage repository
12-02-2011 05:44 AM
On reflection, you might be able to do all of it using handleResourceRequest, but there would two phases:
a) In the save phase, you will override this, call super to get the stream, then save the stream to disk
b) In the load phase, you will supply the stream form disk.
Try typing just BrowserField in the Search box, there are a number of KB articles that deal with things like this.