我尝试使用try和catch块创建android一种方式来处理没有互联网连接或如果web服务器关闭。在日食IOException
红色下划线。如果http://192.168.0.23/loc/index.php
的加载失败,则应该加载"file:///android_asset/myerrorpage.html"
。我知道只尝试和从PHP抓住,并已看过其他任何教程,但无法找到我的错误。尝试和捕获url连接错误
它显示以下消息:为IOException的
无法到达catch块。此异常不会从try语句体抛出
我的代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_localy);
mWebView = (WebView) findViewById(R.id.webview);
// Brower niceties -- pinch/zoom, follow links in place
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.setWebViewClient(new GeoWebViewClient());
// Below required for geolocation
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setGeolocationEnabled(true);
mWebView.setWebChromeClient(new GeoWebChromeClient());
// Load google.com
try {
mWebView.loadUrl("http://192.168.0.23/loc/index.php");
}
catch (IOException e) {
mWebView.loadUrl("file:///android_asset/myerrorpage.html");
}
}
这工作,如果没有互联网连接可用,但在可用互联网的情况下,webview只显示index.php。如果index.php有一个重定向错误页面显示。 – Droidboyx