Page not found Error in WebView android - android

I am trying to open URL in webview of my application, but I am getting "Page not found" error.
The same URL is not opening in my device(Android Tablet) browser also.
If I try to open this URL in my desktop browser, Its opening.
Please let me know how to solve this issue. Below is my code.
public class LinkCompanyPageWebView extends Activity{
private String url;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.my_webview);
WebView wvbrowser;
wvbrowser=(WebView)findViewById(R.id.wvbrowser);
wvbrowser.getSettings().setJavaScriptEnabled(true);
wvbrowser.getSettings().setPluginState(WebSettings.PluginState.ON);
url = getIntent().getStringExtra("URL");
wvbrowser.loadUrl(url);
wvbrowser.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.i("WEB_VIEW_TEST", "error code:" + errorCode);
super.onReceivedError(view, errorCode, description, failingUrl);
}
});
}
}
"onReceivedError" not displaying any error.
p.s: Added Internet permissions to my app.

Related

Can't redirect failed post requests to my error page in Android WebView

i have this
final String linkErrorPage = "file:///android_asset/ErrorPage.html";
public class Callback extends WebViewClient{
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
webView.loadUrl(linkErrorPage);
}}
and this in my mainactivity.java
public class browser extends WebViewClient {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
webView.loadUrl(linkErrorPage);
}
}
when i turn my server off i'm expecting to see my error page from the local assets...
if it's a get request everything is fine, i'm seeing the error page... but if it's a post request i'm seeing this page instead:
how can i redirect to the error page when a post request fails?
okay i found the solution!
webView.setWebViewClient(new browser(){
public void onPageFinished(WebView view, String url) {
String string = "net::ERR_CONNECTION_TIMED_OUT";
webView.findAllAsync(string);
webView.setFindListener((activeMatchOrdinal, numberOfMatches, isDoneCounting) -> {
if(numberOfMatches>0) {
webView.loadUrl(linkErrorPage);
}
});
}
});
though it briefly displays the default error page. if there's a better way i'd like to know it... this is not a perfect solution.

Why CordovaWebViewClient not working in Cordova 6 anymore

I have written custom webviewclient class to override onPageStarted, onPageFinished etc in cordova 3.7 which was working fine.
In following code is I have hosted the www directory to web server and interacting cordova plugins from there (barcodescanner, nfc, bluetooth etc).
public class MainActivity extends CordovaActivity {
private WebView webView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
loadUrl("https://example.com");
}
public class CustomCordovaWebViewClient extends CordovaWebViewClient {
public CustomCordovaWebViewClient(CordovaInterface cordova, CordovaWebView view) {
super(cordova, view);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.i("CSP Log", "onPageStarted: " + url);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Log.i("CSP Log", "onPageFinished: " + url);
}
#Override
public void doUpdateVisitedHistory(WebView view, String url, boolean isReload){
super.doUpdateVisitedHistory(view, url, isReload);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
}
}
}
After a year, I have migrated project from cordova 3.7 to cordova 6 but I found above code broken like CordovaWebViewClient, super.onPageStarted etc can't resolve symbols. I also tried CordovaWebViewImpl and confused myself.
After searching alot on google I found solution which were mostly given in 2011-14 which are not applicable. I couldn't found cordova docs helpful.
It was replaced by SystemWebViewClient
You should do something like this:
SystemWebView wv = (SystemWebView)appView.getView();
wv.setWebViewClient(new SystemWebViewClient((SystemWebViewEngine)appView.getEngine()){
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.i("CSP Log", "onPageStarted: " + url);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Log.i("CSP Log", "onPageFinished: " + url);
}
#Override
public void doUpdateVisitedHistory(WebView view, String url, boolean isReload){
super.doUpdateVisitedHistory(view, url, isReload);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
}
});
Cordova 4 removed the CordovaWebViewClient : look here
You may use WebViewClient instead of CordovaWebViewClient (The cordova-plugin-inappbrowser plugin use that for override onPageStarted event).
public class CustomCordovaWebViewClient extends WebViewClient
Apache cordova removed CordovaWebViewClient inorder to support external webviews like Crosswalk. If you check out the 14 changed files link in the following commit link, you could see CordovaWebViewClient is removed and AndroidWebViewClient is added.
So i guess you cannot use the same old code work in Cordova 6.0
You can probably try using org.apache.cordova.engine.SystemWebViewClient instead.
Infact, the same question is answered here and it was also accepted. So i believe this is the possible solution to the issue. Hope it helps.

Page not found , android webView

This is my code:
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
WebView webview = (WebView) findViewById(R.id.webView);
/*webview.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.i("WEB_VIEW_TEST", "error code:" + errorCode);
super.onReceivedError(view, errorCode, description, failingUrl);
}
});*/
webview.loadUrl("www.google.com");
}
...
...
...
I set the Internet permission on the manifest.
If I uncomment the setWebViewClient the result it's the same.
How I can solve my problem?
WebView does not assume you want to use HTTP by default. When parsing URLs with WebView, you need to always identify the protocol you wish to use to retrieve the resource.
So instead of just specifying www.google.com load the website with:
webview.loadUrl("http://www.google.com");

WebView onPageFinished is being called when Page Not Available error. ¿Solution?

I have a webview that must make a LOG when onReceivedError() is called. But the problem is that when i have very bad wifi connection (almost 100% loss) and the webview is showing a Page Not Available html error, the method onPageFinished is being called and onReceivedError is not being called
How can this be solved? I want that when the page is not available the method onReceivedError gets called!
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Log.d("WEBVIEw","ON PAGE FINISHED");
}
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Log.d("WEBVIEw","ON PAGE error");
}
});
onPageFinished will always be triggered even if there is an error. It would be great to have a method called onPageSucceeded - but this is fairly easy to create.
Create a property on the top of the page private boolean webviewSuccess = true; and
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Log.d("WEBVIEW","ON PAGE FINISHED");
if(webviewSuccess) {
Log.d("WEBVIEW","ON PAGE SUCCEEDED");
}
}
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
webviewSuccess = false;
Log.d("WEBVIEW","ON PAGE error");
}

How does one intercept the "Page Not Found" error in webview?

I am writing an app containing the WebView. and I want to put in some functionality to interecpt the "Page Not Found" message in case the content I am trying to show in my app ever is offline or for some other reason unreachable.
I tried using the onReceivedError() but to no avail. Unless perhaps my syntax is wrong, but if soI don't see the error. can someone help?
code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.somesite.net");
mWebView.setWebViewClient(new HelloWebViewClient());
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.i("WEB_VIEW_TEST", "error code:" + errorCode);
super.onReceivedError(view, errorCode, description, failingUrl);
//TO DO - do something else in here if the site is down
}
});
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
The WebViewClient should be set before you load the url.
Just came across this. onReceivedError() works fine. You will receive a -2 when the error happens.

Categories

Resources