Detecting Webview Error and Show Message

匿名 (未验证) 提交于 2019-12-03 01:32:01

问题:

I'd like to show an error message when there is an error loading a webview page (No connection). This is what I have so far, without the error handling code:

public class TrackerPage extends Activity {      // @Override     private WebView webview;     private ProgressDialog progressDialog;      private boolean error;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);          // Get rid of the android title bar         requestWindowFeature(Window.FEATURE_NO_TITLE);          // Set the XML layout         setContentView(R.layout.tracker_page);          // Bundle objectbundle = this.getIntent().getExtras();         webview = (WebView) findViewById(R.id.tracker);          final Activity activity = this;          // Enable JavaScript and lets the browser go back         webview.getSettings().setJavaScriptEnabled(true);         webview.canGoBack();          webview.setWebViewClient(new WebViewClient() {             public boolean shouldOverrideUrlLoading(WebView view, String url) {                 view.loadUrl(url);                 return true;             }              public void onLoadResource(WebView view, String url) {                 // Check to see if there is a progress dialog                 if (progressDialog == null) {                     // If no progress dialog, make one and set message                     progressDialog = new ProgressDialog(activity);                     progressDialog.setMessage("Loading please wait...");                     progressDialog.show();                      // Hide the webview while loading                     webview.setEnabled(false);                 }             }              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);                 }             }          });          // The URL that webview is loading         webview.loadUrl("http://url.org/");     } }

How would I do this?

回答1:

All answer above are deprecated. You should use this code after on Page finished

 @Override     public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error){            //Your code to do         Toast.makeText(getActivity(), "Your Internet Connection May not be active Or " + error , Toast.LENGTH_LONG).show();     }


回答2:

You're most of the way there... Just implement onReceivedError and handle the errors that you want.



回答3:

Add this after onpagefinished :

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

Don't forget to import android.widget.Toast;



回答4:

Updated answer as per API 23 Marshmallow

WebViewClient Errors Handling

    /*      * Added in API level 23 replacing :-      *      * onReceivedError(WebView view, int errorCode, String description, String failingUrl)      */     @Override     public void onReceivedError(WebView view,        
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!