WebView Cache

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

WebView Cache

Postby Afzal » Tue Jul 07, 2009 8:53 am

Hi..I have an application with a webview which changes the page by clicking on a native image button..i.e I call
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. webview.loadUrl()
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
but the problem is that it takes too long to load..what we can do is load the page from the cache when the user clicks the imagebutton and the in a seperate thread, we can get the latest page. I dont think we have the access to the html content in the webview so i created another http client to get the html data and save it as string. But when i load that data from
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. webview.loadData()
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
it only shows the text so i called
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  webview.loadDataWithBaseURL()
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
which takes almost the same time !!..

I also tried setting the cache mode to "CACHE_ONLY" and the response was fast but it gave no page found error..

My question is how to load page instantly when the user clicks the image button in the app from the cache.

Thnx
Afzal
Freshman
Freshman
 
Posts: 8
Joined: Thu Jun 18, 2009 12:07 pm

Top

Postby Afzal » Tue Jul 21, 2009 5:18 am

Did it !
There is a java script interface that you can define in your android application which lets the java script in a web page access the classes of your android application.

This link explains that: http://rapidandroid.org/wiki/Graphing

I used this to pass a the whole html of the web page from javascript to android and the save that html as cache. When i need to display this html i use

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. webview.loadDataWithBaseURL("file:///android_asset/",data, "text/html", "utf8","");
Parsed in 0.036 seconds, using GeSHi 1.0.8.4



Its nice to know that android automatically caches all the resources such as images and css files so when the html is loaded..all the images and css works..but be careful the web page must contain the full url of the images not relative.

If the images url is relative like "/images/abc.jpg" keep the images locally in "images/" folder in assests folder of the android application.

I hope it would help.

Afzal
Afzal
Freshman
Freshman
 
Posts: 8
Joined: Thu Jun 18, 2009 12:07 pm

Caching a Web Page

Postby Shalni » Tue Aug 04, 2009 1:30 pm

Hi Afzal,

I am new to Android. Wanted to know how can we access a web page offline? Can we get it from cache memory. If yes where will it be stored and how to access it.

The other way is how u have done. Can we store that page in asset and then use the command webview.loadDataWithBaseURL().?
Can u please share your code.

Please help me as soon as possible... Delivery dead line. Confused

The problem is simple. There is one web page and if there is no internet then I have to be able to access it offline.
Shalni
Junior Developer
Junior Developer
 
Posts: 17
Joined: Wed Apr 29, 2009 6:31 am

Postby Afzal » Tue Aug 04, 2009 4:39 pm

Hello Shalni,

My way to do this is to create a simple java class say "PageCacheHandler" in android application add a public function say

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void savePage(String html)
  2.  
  3. {
  4.  
  5. html = html.replace("window.cacheManager.savePage(document.getElementsByTagName('html')[0].innerHTML)", "");
  6.  
  7. _globalHTMLVariable = "<html>"+pageHtml+"</html>";
  8.  
  9. }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


now on the html page write on body load function (JavaScript)
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. window.cacheManager.savePage(document.getElementsByTagName('html')[0].innerHTML)
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


now when declaring webview in the android application write
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. webview.addJavascriptInterface(PageCacheHandler, "cacheManager");
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Now the function "savePage" of your android application will be called whenever the html page will finish loading.
You can save the _globalHTMLVariable and call
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         webview.loadDataWithBaseURL("file:///android_asset/",_globalHTMLVariable, "text/html", "utf8","");
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Be sure that all the images and css files in the web page must have an absolute path not relative.

You will face a problem when you don't have control over the web page you need to display in the webview.

As far as asset goes...i haven't tried that...but it seemes doable if you can save the page as .html in the savePage function say in abc.html file and then can call
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         webview.loadURL("file:///android_asset/abc.html");
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


it may work but dont really know about the images and css file links..

Hope this helps :roll:
Afzal
Afzal
Freshman
Freshman
 
Posts: 8
Joined: Thu Jun 18, 2009 12:07 pm

WebView Cache

Postby Shalni » Wed Aug 05, 2009 8:19 am

Hi Afzal,

Thanks soooo much for a quick reply. :)

I m not getting anything on my screen. Its blank. I have attached the whole code. Can u plz tell me where have I gone wrong.

Let me define my problem once again. Lets consider I want to access http://www.anddev.org/ offline.
How do i do it. Where do I write the function onLoad script? Once when I access anddev.org from my mobile it will be in the history. How do i retrieve it offline?

And I had earlier tried with webview.loadURL("file:///android_asset/abc.html"); It works but images wont be downloaded.


Please help as soon as possible. :?

Thanks in advance...
Attachments
offline_access.zip
(56.62 KiB) Downloaded 242 times
Shalni
Junior Developer
Junior Developer
 
Posts: 17
Joined: Wed Apr 29, 2009 6:31 am

Postby Afzal » Thu Aug 06, 2009 5:47 am

Ok..there can be a problem if the webpage you are displaying is not created by you. you must have control over the page to use my technique.

will c your code in a while..

Afzal
Afzal
Freshman
Freshman
 
Posts: 8
Joined: Thu Jun 18, 2009 12:07 pm

Top

Postby bostone » Wed Sep 02, 2009 6:47 pm

Afzal wrote:Did it !
When i need to display this html i use

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. webview.loadDataWithBaseURL("file:///android_asset/",data, "text/html", "utf8","");
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


Afzal


Hi Afzal,

Since your class can be dropped and recreated at anytime how do you deal (if you do) with persisting "data" in the example above?
bostone
Freshman
Freshman
 
Posts: 2
Joined: Wed Nov 26, 2008 6:27 pm

Postby Afzal » Thu Sep 03, 2009 4:32 am

bostone wrote:
Afzal wrote:Did it !
When i need to display this html i use

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. webview.loadDataWithBaseURL("file:///android_asset/",data, "text/html", "utf8","");
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Afzal


Hi Afzal,

Since your class can be dropped and recreated at anytime how do you deal (if you do) with persisting "data" in the example above?


Hello Bostone,

I persist the html data as string in the Shared Preferences

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 app_preferences = PreferenceManager.getDefaultSharedPreferences(context); //context = current activity
  2.             SharedPreferences.Editor editor = app_preferences.edit();
  3.             editor.putString(KEY, data);
  4.             editor.commit();
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Afzal
Afzal
Freshman
Freshman
 
Posts: 8
Joined: Thu Jun 18, 2009 12:07 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 12 guests