2011-06-17 87 views
23

我想在加載webview頁面時發生錯誤消息(無連接)。這是我到目前爲止,沒有錯誤處理代碼:檢測Webview錯誤並顯示消息

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/"); 
    } 
} 

我該怎麼做?

回答

20

以上所有答案均已棄用。 您應使用此代碼後頁面完成

@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(); 
    } 
+1

如果您的項目的目標API級別<23,如何使用上述內容? – Michael

+0

@Michael,還有另一種API級別1引入的不同參數的方法 - onReceivedError(WebView,int,String,String) – cdeange

+1

如果沒有互聯網,我看到的只是「找不到網頁」,並且沒有任何回調叫做。怎麼來的?另外,使用onReceivedError的新API會更好嗎?根據文檔,它被稱爲頁面上的每個組件。如何在整個頁面中檢查它? –

27

你大部分的方式......只是實施onReceivedError並處理你想要的錯誤。

+4

執行此操作但不捕獲JavaScript錯誤只有連接錯誤等。 – Codebeat

+4

對於javascript錯誤,請設置WebChromeClient重寫onConsoleMessage()。 – Alex

15

添加此之後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(); 
     } 

不要忘了進口android.widget.Toast;

5

更新的答案,每個API 23棉花糖

WebViewClient錯誤處理

/* 
    * Added in API level 23 replacing :- 
    * 
    * onReceivedError(WebView view, int errorCode, String description, String failingUrl) 
    */ 
    @Override 
    public void onReceivedError(WebView view, WebResourceRequest request, 
      WebResourceError error) { 

     Toast.makeText(getActivity(), 
       "WebView Error" + error.getDescription(), 
       Toast.LENGTH_SHORT).show(); 

     super.onReceivedError(view, request, error); 

    } 

    /* 
     Added in API level 23 
    */ 
    @Override 
    public void onReceivedHttpError(WebView view, 
      WebResourceRequest request, WebResourceResponse errorResponse) { 

     Toast.makeText(getActivity(), 
       "WebView Error" + errorResponse.getReasonPhrase(), 
       Toast.LENGTH_SHORT).show(); 


     super.onReceivedHttpError(view, request, errorResponse); 
    } 

網絡錯誤處理

 webView.loadUrl(urlToLoad); 

     if (!isConnected(getActivity())) { 
      Toast.makeText(getActivity(), "You are offline ", Toast.LENGTH_SHORT).show(); 

     } 

    /** 
    * Check if there is any connectivity 
    * 
    * @param context 
    * @return is Device Connected 
    */ 
    public static boolean isConnected(Context context) { 

     ConnectivityManager cm = (ConnectivityManager) context 
       .getSystemService(Context.CONNECTIVITY_SERVICE); 

     if (null != cm) { 
      NetworkInfo info = cm.getActiveNetworkInfo(); 

      return (info != null && info.isConnected()); 
     } 
     return false; 
    } 
2
public class WebClient extends WebViewClient { 

    @Override 
    @TargetApi(Build.VERSION_CODES.M) 
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { 
     super.onReceivedError(view, request, error); 
     final Uri uri = request.getUrl(); 
     handleError(view, error.getErrorCode(), error.getDescription().toString(), uri); 
    } 

    @SuppressWarnings("deprecation") 
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
     super.onReceivedError(view, errorCode, description, failingUrl); 
     final Uri uri = Uri.parse(failingUrl); 
     handleError(view, errorCode, description, uri); 
    } 

    private void handleError(WebView view, int errorCode, String description, final Uri uri) { 
     final String host = uri.getHost();// e.g. "google.com" 
     final String scheme = uri.getScheme();// e.g. "https" 
     // TODO: logic 
    } 
}