Webview onReceivedError方法在webview setWebViewClient中大部分时间被调用。我该如何解决?
出于多种原因可能会调用onReceivedError方法。我怎么知道它是否仅被称为网页不可用的问题?
webView = (WebView) findViewById(R.id.webViewOpen);
webView.loadUrl(url);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setEnableSmoothTransition(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
toolbar.setTitle(title);
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
view.loadUrl(request.getUrl().toString());
} else {
view.loadUrl(url);
}
} catch (Exception e) {
Toast.makeText(WebViewOpen.this, "Opening Browser", Toast.LENGTH_SHORT).show();
return false;
}
return true;
}
/*@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
toolbar.setTitle(title);
try {
webView.loadUrl(url);
} catch (Exception e) {
Toast.makeText(WebViewOpen.this, "Opening Browser", Toast.LENGTH_SHORT).show();
return false;
}
return true;
}*/
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
Toast.makeText(getApplicationContext(), "Error! Check your internet connection\nor refresh\n" + error.toString(),
Toast.LENGTH_SHORT).show();
webView.loadUrl("file:///android_asset/dealsoftheday.html");
}
});
答案 0 :(得分:0)
您需要检查错误,并根据需要进行处理。类似的东西:
switch (error.getErrorCode()) {
case WebViewClient.ERROR_BAD_URL:
...
break;
case WebViewClient.ERROR_CONNECT:
...
break;
case ...:
break;
default:
break;
}
您可以在此处查看完整的错误列表:WebViewClient errors