Find out if Android WebView is showing cached page - android

I'm making an Android application that, among other things, shows websites in a webview. As far as I understand, the webview automatically shows a cached version of the page if a connection can't be established.
Is there any way to find out if the page shown has been fetched from the server or cache?
Maybe even how old the cached page is.
This is to be able to notify the user if he/she is viewing old information.

You could try a hack - at first set WebView's cache mode to WebSettings.NO_CACHE and load the URL. Then, create a custom WebChromeClient like this:
final String urlToLoad = "http://www.my-url.com";
webview.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl)
{
if (view.getSettings().getCacheMode() == WebSettings.LOAD_NO_CACHE && urlToLoad.equals(failingUrl))
{
view.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ONLY);
view.loadUrl(urlToLoad);
return;
}
else
if (urlToLoad.equals(failingUrl))
{
// cache failed as well, load a local resource as last resort
// or inform the user
}
super.onReceivedError(view, errorCode, description, failingUrl);
}
});
webview.loadUrl(urlToLoad);

Since the WebView work like the browser the answer is no.
There are some hacky way to detect that situation.
Or an alternative solution would be preventing the page from being cached (see WebView documentation)

Related

Handle iFrame errors inside of webview

I have a page that displays a news story, the page's content is loaded from a json response which is cached, the problem occurs when the uses is not connected to the internet and tries to access the story, the story loads correctly but the iFrame shows an ER_INTERNET_DISCONNECTED error.
We want to still be able to use the offline feature but if the iFrame has any error remove it from the page, is there anyway to do this ?
I've uploaded a screenshot if that helps
enter image description here
webView.setWebViewClient(new WebViewClient() {
#Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
// either load a new url, or show a popup to the user
webView.loadUrl("file:///android_asset/custom_error.html");
} });
You can decide for yourself what to do in the onReceivedError

Android WebView, err net::Name_Not_Resolved

I have tried searching through the docs and other forums but no success.
I have an android application that is simply a webView that opens up a website as soon as you open the app. However, when connecting to my website I receive an error, net::ERR_NAME_NOT_RESOLVED. Changed the loadUrl with a popular website like Google.com and it worked just fine.
My website works fine from Google Chrome, on dekstop, etc.. just not in WebView.. Can anyone help explain why?
On Another note, I am also trying to find a way to manually enter the dns settings, hoping this would resolve the matter. Any tips or pointing me down the right path is very much appreciated.
Thanks.
Did you set WebViewClient and get error net::ERR_NAME_NOT_RESOLVED from this method?
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
Log.d(TAG, error.getDescription());
}
if yes, I've resolved this using this method
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
Log.d(TAG, description);
}
the first method, I got an error even though the website fully loaded, my suspect is the first method too sensitive so if the website fails to load javascript or CSS or something, the first method returns the error too.

How to prevent the user from going to the apps and continue with browser

In my mobile site I have links to other websites that also have apps. when my users click on the links they get options in Android (I don't know about iPhone) to go to the app or continue with the browser. How do I prevent the user from going to the apps and just cntinue with the browser after clicking my links without seeing this menu (browser/app)?
What you're asking is how to prevent the expected behavior of a webpage you load in your app. That's generally not good UI/UX design. But, to do so you would need to set a WebViewClient on your WebView and evaluate the url that is being loaded to see if it is for the download of an app:
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
if (url.contains("play.google.com")) {
//handle alternate action here
}
}
#Override
public void onPageFinished(WebView view, String url){
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
}
});
That's something that's been set up on the 3rd party site, not something that's being done by the OS. There's no way to control the behavior of the browser after the user has left your web site.
For example LinkedIn will do this but Last Pass does not, even though they also have an app. This is something LinkedIn has chosen to do with their site; you can't do anything about that from your web site.

Android's WebView can't go back in local assets

I have a problem with WebView. My app has build in help files made with html in local assets. And I show html file with WebView.
My problem is, WebView's goBack() function can't go back to previous page when it tries to return to bookmarked position.
For example:
Page1 has link to bookmark1 in Page1.
And Page1 has link to Page2.
Showing Page1 and hit link to Page2, then goBack() can go back to Page1. Fine.
Showing Page1 and hit link to bookmark1, and then hit link to Page2, then goBack() can't go back to page1. WebView shows error like it can't find url with #bookmark1.
I suspect WebView is not handing viewing history for local files. I'm using OS 4.0.3.
Any solutions?
I have found a (compromising) solution for this issue. This issue was discussed here and one solution can be found here (Japanese page, sorry). In my case, after knowing it's a bug in some range of Android OS, going back to previous page (not to exact anchor position) is fine. So I ended up with next code.
webView = (WebView)findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient()
{
// https://stackoverflow.com/questions/6542702/basic-internal-links-dont-work-in-honeycomb-app
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
super.onReceivedError(view, errorCode, description, failingUrl);
String url = failingUrl;
int index = url.indexOf("#");
if (index != -1)
{
url = url.substring(0, index);
}
webView.loadUrl(url);
}
});
With this code when it can't go back to previous page with anchor, it goes back to previous page without anchor. I compromised with it. I tried to make it better with other solutions using Java Script but did not work.

Android WebView window.onload event issue

I am working on one Android App where I am loading local file contents into webview using following syntax.
url = "file://" + mLocalFilePath + java.io.File.separator + CURRENT_FILE;
webView.loadUrl(url);
The issue is the webview loads file contents and also loads the various images from url present in webpage ( webpage has many images ).
The challenge I am facing is I want to get notified when webview finish rendering entire html page.I want to get notified when webpage is 100% loaded.
I reffered various forums and also tried using
window.onload function
as well as
public void onPageFinished(WebView view, String url)
But I did not get desire output,I get callback before html page is completely loaded.I am looking for way so that I will get notified only when webpage has finished rendering all the images present in web page.( i.e Webpage is 100% loaded.)
I am not looking for complete answer even hints will be appreciated.
Thanks
Try this way
webView.setWebChromeClient(new WebChromeClient(){
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
if(newProgress==100){
// page loading completes
}
}
});
I used deprecated PictureListener that start when all images on the pages are loaded. It deprecated and you need at least one image in the html but if you can manage this (you could place transparent 1px*1px image) it's a good solution.
wv.setPictureListener(new PictureListener() {
#Override
public void onNewPicture(WebView view, Picture picture) {
// some code here
}
}
});

Categories

Resources