Tôi muốn hiển thị thông báo lỗi khi có lỗi khi tải trang xem trên web (Không có kết nối). Đây là những gì tôi có cho đến nay, không có mã xử lý lỗi:Phát hiện lỗi xem trên web và hiển thị thông báo
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/");
}
}
Tôi làm như thế nào?
Cách sử dụng ở trên nếu dự án của bạn nhắm mục tiêu cấp API <23? – Michael
@Michael, có một phương pháp khác với các tham số khác nhau được giới thiệu ở cấp API 1 - onReceivedError (WebView, int, String, String) – cdeange
Nếu không có internet, tất cả những gì tôi thấy là "không tìm thấy trang web" và không ai gọi lại được gọi là. Làm thế nào mà? Ngoài ra, tốt hơn là sử dụng API mới hơn của onReceivedError? Theo tài liệu, nó được gọi cho mọi thành phần trên trang. Làm thế nào tôi có thể kiểm tra nó khi nó chỉ cho toàn bộ trang? –