2014-03-31 71 views
4

如果加载显示错误消息需要很长时间,我想超时查看webview。我正在使用setWebViewClient,因为我需要使用public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error)如果加载时间超过一定时间,Android超时webview

我一直在环顾四周,看到我可以使用方法onProgressChanged(WebView view, int newProgress)。现在我不能在setWebViewClient中使用这种方法,也无法弄清楚如何解决这个问题。我遇到的另一个问题是一旦页面加载后进度条永不消失,我也无法向方法public void onPageFinished(WebView view, String url)添加断点。

Web视图设置方法:

public void WebViewSettings(){ 

    webView = (WebView) findViewById(R.id.webview); 
    webView.getSettings().setJavaScriptEnabled(true); 
    webView.getSettings().setBuiltInZoomControls(true); 
    webView.getSettings().setSupportZoom(true); 
    webView.getSettings().setLoadWithOverviewMode(true); 
    webView.canGoBack(); 

    webView.setWebViewClient(new WebViewClient(){ 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      if (Uri.parse(url).getHost().equals(urlString)) { 
       // This is my web site, so do not override; let my WebView load the page 
       return false; 
      } 
      // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs 
      Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
      startActivity(intent); 
      return true; 
     } 

     @Override 
     public void onLoadResource(WebView view, String url) { 
      // Check to see if there is a progress dialog 
      if (progressDialog == null) { 
       progressDialog = new ProgressDialog(context); 
       progressDialog.setTitle("Loading..."); 
       progressDialog.setMessage("Please wait."); 
       //progressDialog.setCancelable(false); 
       progressDialog.setIndeterminate(true); 
       progressDialog.show(); 
      } 
     } 

     @Override 
     public void onPageFinished(WebView view, String url) { 
      // Page is done loading; 
      // hide the progress dialog and show the webview 
      if (progressDialog.isShowing()) { 
       progressDialog.dismiss(); 
       progressDialog = null; 
       webView.setEnabled(true); 
      } 
     } 

     @Override 
     public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) { 
      handler.proceed(); 
     } 

     @Override 
     public void onReceivedError(WebView view, int errorCod,String description, String failingUrl) { 
      Toast.makeText(context, "Your Internet Connection May not be active Or " + description , Toast.LENGTH_LONG).show(); 
     } 
    }); 
} 

所以问题我已经是一次网页加载进度条没有得到清除,我需要超时web视图,如果它接管一定的时间加载。它看起来像进度条显示,然后消失就像它应该,然后再次开始加载,并不会停止。谢谢

回答

8

我已更新您的webview设置方法,请在下面找到更新的代码。 我添加了一个loaderTimerTask类。添加了计时器的代码并为更多理解添加了评论。如果您需要,请更新此代码。

private boolean isPageLoadedComplete = false; //declare at class level 

public void WebViewSettings(){ 

webView = (WebView) findViewById(R.id.webview); 
webView.getSettings().setJavaScriptEnabled(true); 
webView.getSettings().setBuiltInZoomControls(true); 
webView.getSettings().setSupportZoom(true); 
webView.getSettings().setLoadWithOverviewMode(true); 
webView.canGoBack(); 

/** 
    *I had put code here 
    */ 
Timer myTimer = new Timer(); 
    //Start this timer when you create you task 
myTimer.schedule(loaderTask, 3000); // 3000 is delay in millies 

webView.setWebViewClient(new WebViewClient(){ 

    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     if (Uri.parse(url).getHost().equals(urlString)) { 
      // This is my web site, so do not override; let my WebView load the page 
      return false; 
     } 
     // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs 
     Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
     startActivity(intent); 
     return true; 
    } 

    @Override 
    public void onLoadResource(WebView view, String url) { 
     // Check to see if there is a progress dialog 
     if (progressDialog == null) { 
      progressDialog = new ProgressDialog(context); 
      progressDialog.setTitle("Loading..."); 
      progressDialog.setMessage("Please wait."); 
      //progressDialog.setCancelable(false); 
      progressDialog.setIndeterminate(true); 
      progressDialog.show(); 
     } 
    } 

    @Override 
    public void onPageFinished(WebView view, String url) { 
      isPageLoadedComplete = true; 
     // Page is done loading; 
     // hide the progress dialog and show the webview 
     if (progressDialog.isShowing()) { 
      progressDialog.dismiss(); 
      progressDialog = null; 
      webView.setEnabled(true); 
     } 
    } 

    @Override 
    public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) { 
     handler.proceed(); 
    } 

    @Override 
    public void onReceivedError(WebView view, int errorCod,String description, String failingUrl) { 
     Toast.makeText(context, "Your Internet Connection May not be active Or " + description , Toast.LENGTH_LONG).show(); 
    } 
}); 
} 

/** 
    *This class is invoke when you times up. 
    */ 
class loaderTask extends TimerTask { 
    public void run() { 
    System.out.println("Times Up"); 
    if(isPageLoadedComplete){ 
    }else{ 
     if (progressDialog.isShowing()) { 
      progressDialog.dismiss(); 
      progressDialog = null; 
      webView.setEnabled(true); 
     } 
     //show error message as per you need. 
    } 
    } 
} 
+0

谢谢你的伴侣。唯一的一点是,当我添加一个错误消息,你已经把'//显示错误消息,根据你的需要。',我得到一个运行时错误,说'不能创建处理程序内线程没有调用looper。制备()'。现在我已经环顾四周,显然是与它的线程有关,你可以提出任何建议吗? – Paddy1990

+1

我已经设法解决它我不得不明确告诉它运行'runOnUiThread(新的Runnable(){公共无效运行(){Toast.makeText(上下文,“网页超时,再试一次” ,Toast.LENGTH_SHORT).show();}});' – Paddy1990