[英]How can I show a custom "webpage not available" page in Android WebView?
I'm out to do something when no connection is available page/alert in WebView
(eg load a local html page or alert).当WebView
没有可用的页面/警报(例如加载本地 html 页面或警报)时,我要做一些事情。 I've to play with Prevent WebView from displaying "web page not available" but without any success.我必须玩防止 WebView 显示“网页不可用”但没有任何成功。 Any suggestions would be appreciated.任何建议,将不胜感激。
It all came down to simply showing an AlertDialog from onReceivedError: 这一切都归结为只是从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);
}
If you're new to WebView, you'll be looking to implement onReceivedError like this: 如果您是WebView的新手,那么您将希望实现onReceivedError,如下所示:
mWebView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
//Code here
}
});
The above code gives me two deprecation warnings, so I would suggest modifying it as follows. 上面的代码给了我两个弃用警告,所以我建议修改如下。 This goes in the activity that contains the WebView component: 这包含在包含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");
}
You can load html file from assets.您可以从资产加载 html 文件。 Put your html like below.把你的html像下面一样。 assets/html/no_connection.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);
}
The following one is deprecated in api 23以下一个在 api 23 中已弃用
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// TODO: Implement this method
super.onReceivedError(view, errorCode, description, failingUrl);
}
Forgive me if something went wrong.如果出了什么问题,请原谅我。 I am not proficient.我不熟练。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.