【发布时间】:2014-09-17 06:33:37
【问题描述】:
我正在开发我正在调用网页的小应用程序。
我想在同一个视图中打开网页时覆盖了 WebViewClient。
我的问题是如果出现任何问题(互联网连接中断或服务器断开连接),那么我将显示自定义错误页面。
但是当 Internet 连接时,它显示相同的错误页面,而不是刷新网页。
这是我的代码: Web查看问题web; 字符串问题网址;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DetectConnection detectConnection = new DetectConnection(getApplicationContext());
ActionBar bar = getActionBar();
// for color
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#5b5959")));
questionweb = (WebView) findViewById(R.id.webView1);
if (!DetectConnection.checkInternetConnection(this)) {
Toast.makeText(getApplicationContext(), "No Internet!", Toast.LENGTH_SHORT).show();
AlertDialog alertDialog = new AlertDialog.Builder(
MainActivity.this).create();
// Setting Dialog Title
alertDialog.setTitle("Internet Connection");
// Setting Dialog Message
alertDialog.setMessage("Please Check Internet Connection");
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog closed
Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
MainActivity.this.startActivity(intent);
}
});
// Showing Alert Message
alertDialog.show();
} else {
questionweb.getSettings().setJavaScriptEnabled(true);
// loads the WebView completely zoomed out
questionweb.getSettings().setLoadWithOverviewMode(true);
questionweb.getSettings().setUseWideViewPort(true);
// override the web client to open all links in the same webview
questionweb.setWebViewClient(new MyWebViewClient());
questionweb.setWebChromeClient(new MyWebChromeClient());
questionweb.addJavascriptInterface(new JavaScriptInterface(this),
"Android");
// load the home page URL
questionweb.loadUrl("http://10.2.1.119:8081/OnlineExamV2/login/loginpage");
}
// I am calling this to refresh the webpage
questionweb.loadUrl("javascript:window.location.reload( true )" );
}
private class MyWebViewClient extends WebViewClient {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
hideErrorPage(view);
}
private void hideErrorPage(WebView view) {
String customErrorPageHtml = "<html><body><table width=\"100%\" height=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"
+ "<tr>"
+ "<td><div align=\"center\"><font color=\"red\" size=\"22pt\">Sorry! Something went wrong</font></div></td>"
+ "</tr>" + "</table><html><body>";
view.loadData(customErrorPageHtml, "text/html", null);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
questionweb.loadUrl(url);
//questionweb.loadUrl("http://10.2.1.119:8081/OnlineExamV2/login/loginpage");
questionweb.clearHistory();
return true;
}
}
private class MyWebChromeClient extends WebChromeClient {
// display alert message in Web View
@Override
public boolean onJsAlert(WebView view, String url, String message,
JsResult result) {
Log.d("Web", message);
new AlertDialog.Builder(view.getContext()).setMessage(message)
.setCancelable(true).show();
result.confirm();
return true;
}
}
互联网恢复后如何刷新网页。
【问题讨论】: