I want to load a webpage, and if it was already loaded before and not modified then load it from cache. If it is found to be modified, then clear this page in the cache and reload.
How can I do this?
Write the webpage into the cache
Find the webpage in the cache
Show the webpage from the cache
Clear the cache and load a new version if the page was modified
Please help me. It would be awesome if can show me the code for each of the above.
Best regards.
If you are ok with using Webview then LOAD_NORMAL is for u.
webView.getSettings().setCacheMode(WebSettings.LOAD_NORMAL);
webView.loadUrl(HELPER.SERVER_BASE_LINK + "ads/s_image" + (i+1) + ".jpg");
It uses cache to load the webpage unless the page is modified, which is also mentioned in the description of setCacheMode();
http://developer.android.com/reference/android/webkit/WebSettings.html#setCacheMode%28int%29
It takes a few minutes (within 5) to reload the modified page. It is probably the poling time of Android for checking expired pages. I have tried it myself, but I felt bugs in it, sometimes it just doesn't load the cache. It might be that android is taking my cache back too quickly, which is usually not the case.
Using cache with browsers will have some similar method.
Related
I have been trying to enable caching in Android WebView but every config I saw on SO ends in failure.
Here is what I have:
I have a webpage which has an Iframe inside that loads 10-12 pages in a loop all the content in those pages have Cache-Control: max-age=86400 header.
If I load these pages normally in a browser they work and the pages are cached.
So here is the config that I have
getSettings().setJavaScriptEnabled(true);
getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
getSettings().setDomStorageEnabled(true);
getSettings().setMediaPlaybackRequiresUserGesture(false);
getSettings().setAppCacheEnabled(true);
getSettings().setAppCachePath(context.getCacheDir().getAbsolutePath());
getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
These settings work for media playback and enabling js
EDIT
I built a new app and tried loading google with the loadUrl method and reloaded that with a button while the net was off, it was able to reload the page without internet it means it cached that page.
So the issue is that webview is not caching pages which are being loaded by the page.
If anyone knows how we can cache the content which is loaded by webview page loads it would be a great help.
-Thanks
So after a series of detailed tests on our iframe and Android device, I came to realize that WebView does not cache files which are bigger than a particular size.
One of my pages which were being loaded by the iframe had a video of 30mb which was not getting cached and was resulting in huge data consumption. (as it was getting rendered every 2 mins)
I reduced it to 10mb and it still did not cache it finally I reduced it to 980kb and it started caching.
I am not sure about the caching limit per file for WebView but I came across an interesting bug here which states after Android 4.3 the total cache limit was hardcoded to 20mb which we can find in this file on line 98.
If anyone knows where these limits are documented do let the community know by replying here.
This code solve my problem, hope help you too:
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
EDIT: Also you don't needs to these lines:
getSettings().setAppCacheEnabled(true);
getSettings().setAppCachePath(context.getCacheDir().getAbsolutePath());
Remove them and check one more time. AppCache setting not related to type of cache that you want.
I'm running my app that has webview on different activities, they are loading online pages, the issue is that everytime the activity opens the webview takes a couple of seconds to load the page and the page is not available offline.
I was looking for a solution where the webview would always be accessing an offline copy of the website, and the app would download and replace the local website every 24 hours.
I thought it would be simple
Thanks
I see a couple potential solutions to this problem.
Change WebView Cache Mode
The first and simplest would be to change the cache mode of the WebView so that it is forced to load from the cache. This way it will try to load from the cache, and if any resources are not cached, then it will do a network load. This can be simply accomplished by using:
WebView webView; // initialized somewhere
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
Theoretically, you could use LOAD_CACHE_ONLY if the WebView has already loaded once and it doesn't need updating, otherwise you could use LOAD_DEFAULT. Using the LOAD_CACHE_ELSE_NETWORK should definitely speed up loading time, but it might still load some stuff from the network.
However, in practice, I have seen the cache settings not work as consistently as I would like them to.
Utilize WebView.saveWebArchive()
Another more robust solution would be to use the web archive capability that the WebView has. I have not used this solution myself, but you can read about it in this question.
Basically, you can save the contents of the WebView using the following:
WebView webView; // initialized somewhere
webView.loadUrl("http://blahblahblah.com"); // URL to save
Context context = this;
String pathFilename = context.getFilesDir().getPath() + File.separator + "savedWebPage.mht"; // You can use any path you want
webView.saveWebArchive(pathFilename);
Then you can load the saved archive file by using:
webView.loadUrl("file://" + pathFilename);
Please note that I am leaving out the logic for handling when to load from the website and when to load from the archive file, that's up to you how to handle it.
I am developing Android app now where I need to store image Url in DB for each object and then load this image from the web (if connection is on) OR get it form cache (if not).
Picasso seems to be best lib for handling images in Android, so I starter using it. But I can not get how do I use it properly for my case. Even more - Images strangely are loaded into views right after they are first time get from API but if user starts app again, we can see placeholder only (even with internet connection on). Can someone suggest a solution or at least any kind of idea of best way to do this?
My code in Adapter (is is also same in ShowActivity):
String img_url = item.getImage(); // img_url is valid image url
Picasso.with(mContext).load(img_url).placeholder(R.drawable.plchldr).fit().centerCrop().into(holder.image);
I tried to use Picasso at the start as well, and found out that their caching system is horrible.
It would cache some images i downloaded, but not others, and would therefore leave alot of images with placeholders.
I suggest you try Universal-Image-Loader (https://github.com/nostra13/Android-Universal-Image-Loader). I'm currently using this to cache all images in my app, and it works flawlessly (Around 200 images atm).
Finally got the problem - API is returning valid image url BUT actually it (image) is hosted on S3 cloud and it is expiring till the user loads application again. So, actually saving expiring image URL is useless in this situation. I am not sure what will I do here (it seems that the only way here is hard one - storing images on SD as Bitmap). But I just wanted to tell this for those who will get similar issue.
As the title implies, could anybody explain if and why it is required to clear webView cache in Android?
My concern is that the cache piles up endlessly and that doesn't sound a good idea. On the other side, it helps loading pages faster, so I prefer not to clear that cache but I wonder what is the standard? What is acceptable here?
Is it the same for clearing history?
Additionally, is there a way to clear the cache/ history in a certain time interval or as it reaches to a certain size limit?
Thank you.
I don't think that cache "piles up endlessly" - every sane cache mechanism that I've ever seen maintains a maximum size - usually as an MRU cache.
So, you shouldn't have to maintain it yourself.
If, for some reason, you really do need to clear the cache, however, you can use the WebView's clearCache method
In my webview project, the webview [in a fragment] was going to the last page visited -- from the cache -- on startup. This was no good, so, I clear the cache: mWebView.clearCache(true);
Now, it would be nice to clear the cache on program exit; then, you wouldn't even be storing cache when not running the app. But, that seems not possible, because, if you put the cache clear code in onDestroy(), then it gets cleared every time the device is rotated, so you lose benefit of having the cache. So, I do the clear cache as the first operation after the webview is initialized.
I'm pretty new at this, so, don't take my experience as gospel.
So that's about it, I want webview to download and cache multiple pages when the app is opened so that they're saved for offline use. They're only going to be a few kilobytes each and there's only going to be about 10 of them, we're talking less than 100kb all together, but I want to make sure all the info is up to date when the app is open. How would I do go about doing this? Just somewhere to start would be nice.
Edit: assume internet connection at first, but then this stops and there's no internet after the caching is complete.
You can create a WebView and make it not visible and load the resources here.