I'm trying to display web page using WebView in android, when a particular page doesn't load, it displays error message with URL, saying that abcd.com is not available, how do I replace the error message with my custom message instead of displaying the URL?
storeLocator.setWebViewClient(new WebViewClient(){
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
storeLocator.loadUrl("file:///assets/error.html");
}
});
storeLocator.loadUrl("http://goog.c");
You can create the html page whatever you want to show whenever the error comes or your page is not available ,the code is given here-
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webview.loadUrl("file:///android_asset/myerrorpage.html");
}
});
Related
I was making a webview application for my local network. I have two static IPs. All I want to do is to load second if 1st one is down. (Because one of them is always running).
What I'm doing is to check it in onRecievedError() function and change the URL value, then call the onCreate() again like this.
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
Toast.makeText(getApplicationContext(), "Failed loading app!", Toast.LENGTH_SHORT).show();
url1="http://192.168.43.XXX"; //here XXX is used just to hide my IP from public
onCreate(new Bundle());
}
the onCreate() method has to render the new URL but my application closes itself upon this call. Why is this so?
Please help because I'm a beginner.
use this code
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
Toast.makeText(getApplicationContext(), "Failed loading app!", Toast.LENGTH_SHORT).show();
if(failingUrl.equals(url1))
{
//you can use load url if fail first url
// dont't use onCreate again
webview.loadUrl(url2);
}
}
When WebView is loading url, user can lose access to internet (for example, he work with router, but money in account is ended).
In this case WebView show own html "Webpage not available".
I try to use custom WebViewClient and callback:
#Override
public void onReceivedError(android.webkit.WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
}
but it`s not called.
How to handle offline when loading url in WebView?
P.S. Do not offer solutions to check the network status, please. I am interested only WebView.
Try this :
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.e(String.valueOf(errorCode), description);
// API level 5: WebViewClient.ERROR_HOST_LOOKUP
if (errorCode == -2) {
String summary = "<html><body style='background: black;'><p style='color: red;'>Unable to load information. Please check if your network connection is working properly or try again later.</p></body></html>"; view.loadData(summary, "text/html", null);
return;
}
// Default behaviour
super.onReceivedError(view, errorCode, description, failingUrl);
}
Hey do this for give popup to user that check your data..
StackOverflow link
AndroidHive link
I found the reason why not work error callbacks of my WebView:
getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
After removing, this method:
#Override
public void onReceivedError(android.webkit.WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
}
started working again
1.Save yout error template as error.html in asset
2.when error occurs load template from asset
webview.loadUrl("file:///android_asset/error.html");
I am new in android. I am trying to make just a sample application to open websites in webview. The problem is, website opens in desktop mode. How can I open websites just like UCBrowser (mobile mode) ?
Try this. It should work.
webview1.getSettings().setJavaScriptEnabled(true);
webview1.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(getApplicationContext(), description, Toast.LENGTH_SHORT).show();
}
});
webview1.getSettings().setLoadWithOverviewMode(true);
webview1.getSettings().setUseWideViewPort(true);
webview1.getSettings().setBuiltInZoomControls(true);
webview1 .loadUrl("http://www.matrixsystems.net.in/"); // set url
webview1.requestFocus();
I'm trying to do a webview based application for this website to show it in a mobile application.
when I put any different site the application work great, but in this specific site never show the second page when I clicked in the button to go to the desk page. However, I put a Log statement in the onPageFinished method and log that the page is loaded completely.
My Code Here
final WebView myWebView = (WebView) rootView.findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(getActivity(), "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
#Override
public void onPageFinished(WebView view, String url) {
Log.d("WEBSITE", "Page Loaded.");
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("WEBSITE", url);
myWebView.loadUrl(url);
return false;
}
});
myWebView.loadUrl("https://demo.frappecloud.com/");
I believe the problem is with your shouldOverrideUrlLoading. If you check the documentation you will see that :
This method is not called for requests using the POST "method"
When you are submitting the Form, you are making a POST request, which is basically ignored.
Here is the link: WebViewClient
Also check this reference where it says how you could load a URL with POST data: LOAD A POST REQUEST INTO A WEBVIEW IN ANDROID
Consider checking this thread: Android - how to intercept a form POST
I am trying to load URL inside a WebViewClient as below:
webview.setWebViewClient(new WebViewClient()
{
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url)
{
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
}
});
webview.loadUrl(articleLink);
Problem:
Web URL is loading successfully but in some occurrence i got the following errors, at the same time i would like to display Alert Dialog instead of Webview with Error message.
So can you please let me know ,How do i handle the following kind of errors:
"Web page not available" error
"Directory listing Denied"
I have attached the 2nd one's image for your reference:
Both of those are server side errors.
1. is the page is either physically not there (404) or a
2. the server is serving you a page that states it will not show you the directory (200)
but you should be able to handle them inside the
OnReceivedError()
you can create a dialog box from there.