Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set loadURLTImeOutValue on WebView

Tags:

I'm working with PhoneGap and Android and have my .html and js files on an external server. When I use the following code, the app loads my external .html files and everything works fine:

this.setIntegerProperty("loadUrlTimeoutValue", 60000); this.loadUrl("http://www.myserver.com"); 

However, when work via a WebView I can't seem to set the loadURLTimeoutValue for a WebView:

private WebView webView; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);    try {      webView = (WebView) findViewById(R.id.webview);          webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);      webView.loadUrl("http://www.myserver.com");      } 

This doesn't work. How can I set the timeout value on the WebView?

like image 483
user989557 Avatar asked Oct 14 '11 19:10

user989557


People also ask

How do I send a post request in WebView?

setWebViewClient(new WebViewClient(){ public void onPageStarted(WebView view, String url, Bitmap favicon) { super. onPageStarted(view, url, favicon); } public boolean shouldOverrideUrlLoading(WebView view, String url) { webView. postUrl(Base_Url, postData. getBytes()); return true; } });

How do you override a WebView?

If you want to override certain methods, you have to create a custom WebView class which extends WebView . Also, when you are inflating the WebView , make sure you are casting it to the correct type which is CustomWebView . CustomWebView webView = (CustomWebView) findViewById(R. id.

How do I find my WebView URL?

String webUrl = webView. getUrl();

What is WebView API?

The WebView API for Ads allows in-app ad monetization using WebView . If you display web content that implements ads with AdSense code or Google Publisher Tag in your app through WebView , you should use this API to enable ads monetization.


2 Answers

This is a workaround to simulate the described behavior. You can use a WebViewClient, and override the onPageStarted method:

public class MyWebViewClient extends WebViewClient {     boolean timeout;      public MyWebViewClient() {         timeout = true;     }      @Override     public void onPageStarted(WebView view, String url, Bitmap favicon) {         new Thread(new Runnable() {             @Override             public void run() {                 timeout = true;                  try {                     Thread.sleep(1000);                 } catch (InterruptedException e) {                     e.printStackTrace();                 }                 if(timeout) {                     // do what you want                 }             }         }).start();     }      @Override     public void onPageFinished(WebView view, String url) {         timeout = false;     } } 

If timeout, you can load, for example, an error page...

To add the WebViewClient to you WebView, just do this:

webView.setWebViewClient(new MyWebViewClient()); 
like image 156
Vito Gentile Avatar answered Sep 19 '22 06:09

Vito Gentile


I used this to set a time out for my WebView:

public class MyWebViewClient extends WebViewClient {      boolean timeout = true;      @Override     public void onPageStarted(WebView view, String url, Bitmap favicon) {         Runnable run = new Runnable() {             public void run() {                 if(timeout) {                     // do what you want                     showAlert("Connection Timed out", "Whoops! Something went wrong. Please try again later.");                 }             }         };         Handler myHandler = new Handler(Looper.myLooper());         myHandler.postDelayed(run, 5000);     }      @Override     public void onPageFinished(WebView view, String url) {         timeout = false;     } } 
like image 39
Nick Avatar answered Sep 22 '22 06:09

Nick