【发布时间】:2012-12-26 11:53:52
【问题描述】:
当WebView 中没有可用的连接页面/警报时,我要做一些事情(例如,加载本地 html 页面或警报)。我必须和Prevent WebView from displaying "web page not available" 一起玩,但没有任何成功。任何建议将不胜感激。
【问题讨论】:
当WebView 中没有可用的连接页面/警报时,我要做一些事情(例如,加载本地 html 页面或警报)。我必须和Prevent WebView from displaying "web page not available" 一起玩,但没有任何成功。任何建议将不胜感激。
【问题讨论】:
这一切都归结为简单地显示来自 onReceivedError 的 AlertDialog:
@Override
public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
//Clearing the WebView
try {
webView.stopLoading();
} catch (Exception e) {
}
try {
webView.clearView();
} catch (Exception e) {
}
if (webView.canGoBack()) {
webView.goBack();
}
webView.loadUrl("about:blank");
//Showing and creating an alet dialog
AlertDialog alertDialog = new AlertDialog.Builder(youractivity.this).create();
alertDialog.setTitle("Error");
alertDialog.setMessage("No internet connection was found!");
alertDialog.setButton("Again", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
startActivity(getIntent());
}
});
alertDialog.show();
//Don't forget to call supper!
super.onReceivedError(webView, errorCode, description, failingUrl);
}
如果您是 WebView 的新手,您会希望像这样实现 onReceivedError:
mWebView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
//Code here
}
});
【讨论】:
上面的代码给了我两个弃用警告,所以我建议修改如下。这在包含 WebView 组件的活动中:
myWebView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
try {
webView.stopLoading();
} catch (Exception e) {
}
if (webView.canGoBack()) {
webView.goBack();
}
webView.loadUrl("about:blank");
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Error");
alertDialog.setMessage("Cannot connect to the R2R Server. Check your internet connection and try again.");
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Try Again", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
startActivity(getIntent());
}
});
alertDialog.show();
super.onReceivedError(webView, errorCode, description, failingUrl);
}
});
【讨论】:
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
// declare a text view in your xml
sample_TextView.setText(R.string.no_internet_connection);
view.loadUrl("about:blank");
}
【讨论】:
...。这只是一个友好的提醒,但这样的修改迟早会被拒绝。
您可以从资产加载 html 文件。 把你的 html 放在下面。 资产/html/no_connection.html
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error)
{
Toast.makeText(getActivity(),String.valueOf(error.getErrorCode())+":"+ error.getDescription(),Toast.LENGTH_LONG).show();
wv.loadUrl("file:///android_asset/html/no_connection.html");
super.onReceivedError(view, request, error);
}
以下一个在 api 23 中已弃用
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// TODO: Implement this method
super.onReceivedError(view, errorCode, description, failingUrl);
}
如果出现问题,请原谅我。我不熟练。
【讨论】: