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.
Related
I am getting message 'Server hangup' when loading url into webView and this message is not implemented in either Android side or server side. If anyone knows how to solve this issue, Please help. Thanks
I have used following code:
private WebView mWebview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new MyWebChromeClient(this));
mWebview .loadUrl("our server url");
setContentView(mWebview);
}
private class MyWebChromeClient extends WebChromeClient {
Context context;
public MyWebChromeClient(Context context) {
super();
this.context = context;
}
}
Please check screenshot
Google.com is secure domain. You have to use https://www.google.com instead of http://www.google.com.
Please use this method and set your website url :
Example :startWebView("https://stackoverflow.com");
private void startWebView(String url) {
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
webview.getSettings().setBuiltInZoomControls(true);
webview.getSettings().setUseWideViewPort(true);
webview.getSettings().setLoadWithOverviewMode(true);
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(getApplicationContext(), "Error:" + description, Toast.LENGTH_SHORT).show();
}
});
webview.loadUrl(url);
}
Webview shows you the HTML returned by the URL that has been loaded.
onReceivedError() will not be called if you get response containing the error message.
Check what you receive as HTML from server using the following code in onPageFinished().
webView.evaluateJavascript(
"(function() { return ('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>'); })();",
new ValueCallback<String>() {
#Override
public void onReceiveValue(String html) {
// displays the HTML received after the URL is loaded.
Log.e("#Eval", "Html -> " + html);
}
});
As you have mentioned that the message is not implemented in your app, it must have been be received from the server end.
I am using webview for showing Flipkart website in my application.
Everything is working fine as needed except one thing:
when I click on BuyNow button, select variant view gets visible.
This view comes for 1 sec and then show the white blank page.
Is there any setting i need to enable in webview to show this types of view?
I had already gone through many questions on Stackoverflow but nothing helped me.
I tried setting webchromeclient and below methods on webview with no success.
shopping_webview.getSettings().setAllowFileAccess(true);
shopping_webview.getSettings().setAllowContentAccess(true);
shopping_webview.getSettings().setAllowFileAccessFromFileURLs(true);
shopping_webview.getSettings().setAllowUniversalAccessFromFileURLs(true);
shopping_webview.getSettings().setDomStorageEnabled(true);
shopping_webview.getSettings().setUseWideViewPort(true);
shopping_webview.getSettings().setLoadWithOverviewMode(true);
shopping_webview.getSettings().setLoadsImagesAutomatically(true);
shopping_webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
shopping_webview.getSettings().setSupportMultipleWindows(true);
shopping_webview.getSettings().setBuiltInZoomControls(true);
shopping_webview.getSettings().setDisplayZoomControls(false);
shopping_webview.getSettings().setJavaScriptEnabled(true);
webviewLink.getSettings().setLoadsImagesAutomatically(true);
webviewLink.getSettings().setJavaScriptEnabled(true);
webviewLink.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webviewLink.loadUrl(postlink);
webviewLink.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO show you progress image
progressBar.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO hide your progress image
progressBar.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
});
load the Url with this way it's work for me in the same app..
WebView webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl(_URL);
}
private class MyWebViewClient extends WebViewClient{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(getApplicationContext(), "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
}
I want to be able to recognize when an error occurs and tell show a toast message with the error but I can never get them to call.
I also want to be able to tell when the webview starts navigating to a url and detect which url it's going to.
Unfortunately it just seems to ignore everything and I have no idea how to start a function or method or anything once this occurs.I don't really care how I go about determining when a page starts loading or an error occurs I just need a way that works.
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
myWebView.setWebChromeClient(new WebChromeClient()
{
private boolean error;
public void onPageStarted(WebView view, String url, Bitmap favicon) {
onPageStarted(view, url, favicon);
Toast.makeText(getApplicationContext(), "page started:" + url, Toast.LENGTH_SHORT).show();
error = false;
}
public void onReceivedError( WebView view, int errorCode, String description, String failingUrl) {
error = true;
System.out.println("description error" + description);
view.setVisibility( View.GONE );
Toast.makeText(getApplicationContext(), "error:" + description, Toast.LENGTH_SHORT).show();
}
public void onPageFinished(WebView view, String url) {
if (!error) {
view.setVisibility( View.VISIBLE );
}
error = false;
Toast.makeText(getApplicationContext(), "page finished" + url, Toast.LENGTH_SHORT).show();
}
}
);
myWebView.setBackgroundColor(0);
WebSettings webSettings = myWebView.getSettings();
webSettings.setDefaultTextEncodingName("utf-8");
webSettings.setJavaScriptEnabled(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setAllowFileAccess(true);
// Other webview settings
myWebView.addJavascriptInterface(new MyJavascriptInterface(this), "Android");
myWebView.loadUrl("https://example.com/");
}
}
The onPageStarted(), onPageFinished(), and onReceivedError() methods are not defined in the WebChromeClient class. They're members of WebViewClient. You subclassed the wrong one.
If you add the #Override annotation above each method, your IDE should yell at you that those methods don't override anything in the super class, which is why that annotation is useful.
Simply move those overrides to the WebViewClient instance.
myWebView.setWebViewClient(new WebViewClient() {
private boolean error;
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
...
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
...
}
#Override
public void onPageFinished(WebView view, String url) {
...
}
}
);
myWebView.setWebChromeClient(new WebChromeClient());
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");
I have a WebView trying to load a WebPage like this:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
wv=(WebView)findViewById(R.id.webview);
WebSettings ws=wv.getSettings();
ws.setJavaScriptEnabled(true);
wv.loadUrl("http://pro39.blutechnologies.com/crimes.aspx");
}
I have added the internet permission to the manifest
<uses-permission android:name="android.permission.INTERNET"/>
However the WebView is blank,it neither throws an error nor loads the web-page.How do I load webpages like this,I have only tried loading local html files before and I would like to know if I have to do something different.
Try like this way:
mWebView.loadUrl("http://pro39.blutechnologies.com/crimes.aspx");
mWebView.setWebViewClient(new HelloWebViewClient());
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
Use this code .shouldOverrideUrlLoading() not use compulsary.use this code and please reply me this code work or not
Try below code.
public class Main extends Activity {
private WebView mWebview ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
mWebview = (WebView)findViewById(R.id.webview);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("http://pro39.blutechnologies.com/crimes.aspx");
setContentView(mWebview );
}
}