Load second URL if first is not present in android webView - android

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);
}
}

Related

How to handle offline when loading url in WebView?

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");

How do I verify if the url entered in my webview is valid and if not valid how do i redirect to some other url?

I am using an authenticated webview in my app. Currently when we enter the search term in the search box,it is added to the url (say http.cookies.com/something/SEARCHTERM/something) and the url is called in the webview which loads the url as follows:
if(!StringUtils.isEmpty(URL)){
if(URLUtil.isValidUrl(URL)){
mWebview.loadUrl(URL);
}else{
mWebview.loadUrl("http://www.google.com");
}
However, even if the url is invalid, it still loads the invalid url which shows up as "412- precondition set and hence page cannot be displayed".Is there a way I can detect if this search term or url is valid and display it in the webview , if valid) as the requested url , if not valid, i display some other webview like say google.com?
Thanks!
justin
You can use the Webview's WebViewClient to catch the error and do whatever you see convenient. Here is a sample:
yourWebView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
// TODO Auto-generated method stub
super.onReceivedError(view, errorCode, description, failingUrl);
}
});

How to display custom message in WebView of android?

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");
}
});

How to display pdf document asynchronously in android?

I am displaying a pdf document in my android application. For that I followed from the link How to open a PDF from an Android app (in a separate PDF viewer app). My pdf document size is of 30mb. So it is taking time to display it. Hence I need to display it asynchronously. I am new to asynchronous tasks. Please give me some idea on how to display the pdf asynchronously.
use ProgressDialog as follow
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setPluginsEnabled(true);
progressDialog = ProgressDialog.show(Activity_PDF.this, "Loading",
"Please wait", true);
webview.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// TODO Auto-generated method stub
super.onReceivedError(view, errorCode, description, failingUrl);
Toast.makeText(Activity_PDF.this, description,
Toast.LENGTH_SHORT).show();
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
super.onPageFinished(view, url);
}
});
// used to read PDF files from docs.google.com
webview.loadUrl("http://docs.google.com/gview?embedded=true&url="
+ stPdfLink);
First of all, it's not you the one who open this pdf. You open it with other application, so if rendering speed is low, the only thing you can do is select other app for openeing it. It's not your responsibility and in your case it's not related to asynchronous tasks at all. I think it will be usefull for you to read about Android Fundamentals first of all and get some more knowlege abour Android framework and how it works.

Android - WebViewClient

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.

Categories

Resources