Offline browsing

Curious, how are you getting your html files on the SD card. Are you saving them on your PC first and then transferring them?

I'm only asking because I'd like to do the same thing (offline browsing). So I started to work on an app that would download a webpage, save it to the SD card and then let you see it later.

My app currently saves the html file to the SD card, but when I try and use the built in WebKit browser to read back the html file, it still tries to go and grab all of the images etc that are tagged in the html file.

So I then realized I'd have to enhance my app to strip out all of those references etc when the html file gets saved to the SD card. But I'm lazy so there my app sits....half done. If I ever finish it I'll put it on the Market Place.

In the meantime I was thinking of just using "wget" or even just "Save page" on the browser and then transferring them to the SD card by hand before a flight. Is that what you're doing?
 
How would you access saved html?
Lets say if I browse to file:///sdcard/1.htm it says could not be loaded, though I have copied 1.htm to sdcard.

Answering to your qestion, I just copying files directly to flash from my pc.
 
Correct. The built in browser does not support reading local files with file://

You need my app....if I ever finish it and put it on the Market. Four day weekend coming up so maybe I'll work on finishing it up.

Have you tried the Opera Mini Browser that was just released? I haven't yet and I'm at work right now so I probably won't be able to try it until later.
 
As I mentioned above, it's a simple app. You type a URL, it then downloads that page and saves it to the SD card.

Then later, when on a plane or wherever, you open the app, but instead of hitting "Save" you hit "Load" and pick the web page you previously saved. It reads it from the SD card and displays it.

I have the save portion working, but I'm still working on displaying the saved HTML. If I use a simple html file it works, but when I try anything with lots of tags and / or javascript etc it doesn't.

I might try and work on it some more this weekend.
 
So, though you can not make browser to open file from SD directly, you can do this via Android API?

Have you tried loading file that you copied from your pc (not made by your app)?

ps: I'd rather go to Vegas or LA than spending TG day on this
 
Well it's a 4 day weekend. So even though I won't be working on it tomorrow, I'll probably work on it Sat or Sun

Yes, I created a "test.html" put it on my SD Card and I'm able to dispaly it just fine.

When I let my app download a site (I tried ask.com and msn.com just randomly) it won't display them. So that's what I have to work on to finish it up.
 
It's rather simple actually...but like I said, something isn't quite right. For example if I try it with http://www.msn.com it saves a file but then when I "load" it from the SD card I get a WebView with a blue background (so it appears to start to display the page) but that's it. No text. IF instead I create a really simple html file like:



This is a test.



then it works.

Code that saves the web page
--------------------------------------

public void processHttp(String saveAs) throws Exception
{
httpClient = new DefaultHttpClient();
httpGet = new HttpGet(mEditor.getText().toString());
ResponseHandler responseHandler = new BasicResponseHandler();
String response = httpClient.execute(httpGet, responseHandler);
File f = new File("/sdcard/websave");
if (!f.exists()) f.mkdir();
String fileName = "/sdcard/websave/" + saveAs;
FileWriter fw = new FileWriter(fileName);
fw.write(response);
fw.flush();
fw.close();
}

Code that reads from the SD card and launches a WebView
--------------------------------------------------------------------------

public void loadHttp(String fileName) throws Exception
{
final int MAXFILESIZE = 8096;
File f = new File(fileName);
if (!f.exists())
{
throw new Exception("File does not exist.");
}

byte[] array = new byte[MAXFILESIZE];
String data = "";

try
{
InputStream is = new FileInputStream(f);
int bytesRead = is.read(array);
while (bytesRead > 0)
{
data += new String(array, 0, bytesRead);
bytesRead = is.read(array);
}
is.close();
}
catch (IOException ex)
{
// read or close failed
return;
}
wv.loadData(data, "text/html", null);
setContentView(wv);
}
 
You cannot access files directly using the browser, but you can run a local server. If it's possible for ConnectBot to set up tunnels usable by the browser, it should be possible to run a cache server.

For example... set up an HTTP server on localhost:80 that normally proxies nytimes.com:80 and saves+rewrites all the data, but serves up cached content when the network is down.

Obviously, it's not nearly as good as a true caching proxy server, but it's a start. It should be possible to deploy a true proxy server as well, but I'm not sure if the browser supports proxy servers. If the browser could indeed work with proxy servers, then we'd be in business for bigger and better things

(oh, and if other apps support proxying too... this might be the solution to the offline GPS problem.)
 
True, but you can "load" a saved html file from the SD card and "pass" it to a WebView. My code above does just that. Just for some reason it only works with very simple html files.
 
It sure does. But as you said, it's probably an issue with linked documents not being saved. Might it be worth parsing these links out of the document and recursively downloading them while a connection is available?
 
YES!!! Ok, so I never finished working on my "offline browser" app, but I just figured out how to do what I wanted to do.

I noticed that the new version of Bender (now called Astro), has an open with HTML Viewer as an option. So I was able to browse to my SD card, and open up a saved index.html file.

Now I've only tested this with one site, but I was so excited it worked I wanted to share the info.

You'll need "wget", which I have on my Linux box, but if you're a Windows user you can install "Cygwin" and do the same thing:

Step 1:
Find a section you want to read offline (I choose the tech news section). Copy the URL (in this case it was: http://www.msnbc.msn.com/id/3032118/)

Step 2:
Use wget to download the website:
wget -r -l 3 -p -k --user-agent="Mozilla/5.0 (Linux; U; Android 0.5; en-us) AppleWebKit/522+ (KHTML, like Gecko) Safari/419.3" http://www.msnbc.msn.com/id/3032118/

-r says to recurse
-l says to go only 3 levels deep
-p says download CSS files and whatever else is needed to render locally.
-k says convert links to render correctly locally
--user-agent tells the msnbc website to dish us the mobile version (trust me it reads a WHOLE lot better offline in the mobile version)

Step 3:
Now you should have a directory called www.msnbc.msn.com with about 10 MB worth of data/files. Copy that to your SD card.

Step 4:
Open Astro, navigate to an index.html file (might be burried) and select open with HTML Viewer. From there, you can jump around the stories by just using the links.

I turned on Airplane mode to be extra sure that I was truly browsing offline and it all worked great!!!

THANK YOU ASTRO!!!
 
I had some html files saved with no images on and I emailed it myself and opened it up through the gmail. It worked fine even with the internal links for properly saved html files.
 
Back
Top