如果页面是“未找到 404 页面”,如何从 Android WebView 检查?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3181843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I check from Android WebView if a page is a "404 page not found"?
提问by weakwire
I want to check a "404 page not found" from a WebView and if it's a 404 then I revert to the previous page. Ty for your help
我想从 WebView 检查“未找到 404 页面”,如果是 404,则返回到上一页。你的帮助
EDIT also the webpages I want to see are pure .jpg 1.jpg 2.jpg 3.jpg but I have no info of how many images exist. So if anyone can propose another method, he is welcome to do so.
编辑我想看到的网页也是纯 .jpg 1.jpg 2.jpg 3.jpg 但我没有关于存在多少图像的信息。所以如果有人可以提出另一种方法,欢迎他提出。
回答by Alexey Ozerov
Starting SDK 23 (Android M) you can use onReceivedHttpError
method to catch 404 and other HTTP errors. Just override inside your WebViewClient
从 SDK 23 (Android M) 开始,您可以使用onReceivedHttpError
方法来捕获 404 和其他 HTTP 错误。只需覆盖您的内部WebViewClient
@Override
public void onReceivedHttpError (WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
Toast.makeText(view.getContext(), "HTTP error "+errorResponse.getStatusCode(), Toast.LENGTH_LONG).show();
}
回答by LordMarty
I might be a few years too late, but here is how I had to solve it since none of these answers worked.
我可能晚了几年,但这是我必须解决的方法,因为这些答案都不起作用。
I ended up using onReceivedTitle and comparing the title with the title of the page (in this case "page not found") from the site I was trying to open.
我最终使用了 onReceivedTitle 并将标题与我试图打开的网站的页面标题(在本例中为“找不到页面”)进行比较。
webview.setWebChromeClient(new WebChromeClient() {
@Override
public void onReceivedTitle(WebView view, String title) {
// TODO Auto-generated method stub
super.onReceivedTitle(view, title);
CharSequence pnotfound = "The page cannot be found";
if (title.contains(pnotfound)) {
pagenotfound = true;
view.stopLoading();
webview.loadUrl("https://www.google.com/search?hl=en&q=stackoverflow");
}
}
public void onProgressChanged(WebView view, int progress) {
activity.setProgress(progress * 1000);
}
});
webview.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description,
Toast.LENGTH_SHORT).show();
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
});
The "pnotfound" will be different from site to site. But usually one site use the same "page not found" therefore you can use the Title on the site. You might want to add a else if for if you're using multiple sites.
“pnotfound”会因站点而异。但通常一个站点使用相同的“找不到页面”,因此您可以使用站点上的标题。如果您使用多个站点,您可能需要添加 else if for。
Hope it helps for someone.
希望它对某人有帮助。
回答by Dariusz Bacinski
I would try to detect loading of 404 page. You can do that by implementing shouldOverrideUrlLoading
method in the WebViewClient
class.
我会尝试检测 404 页面的加载。您可以通过shouldOverrideUrlLoading
在WebViewClient
类中实现方法来做到这一点。
mGenericWebClient = new GenericWebClient();
mWebView.setWebViewClient(mGenericWebClient);
public class GenericWebClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if (url=="your404page.html") {
view.goBack();
return true;
}
return false;
}
}
You can also check if onReceivedError
event appears, when 404 error occurs.
您还可以检查onReceivedError
事件是否出现,当 404 错误发生时。
回答by weakwire
You don't need to fully load the page to check if it results in a 404 error:
您不需要完全加载页面来检查它是否导致 404 错误:
private int getRange() {
try {
HttpURLConnection.setFollowRedirects(false);
int Count = 1;
URL testURL;
while (true) {
testURL = new URL(
(myURL + "/" + Integer.toString(Count++) + ".jpg"));
HttpURLConnection con = (HttpURLConnection) testURL
.openConnection();
con.setRequestMethod("HEAD");
if (con.getResponseCode() == 404) {
return Count - 2;
}
Log.e("RESPONCE", Integer.toString(con.getResponseCode()));
}
} catch (Exception e) {
}
return 1;
}
回答by Adrián Baeza Prieto
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
String pageTitle = myWebView.getTitle();
String[] separated = pageTitle.split("-");
if(separated[0].equals("404")) {
Log.i(TAG, "detect page not found error 404");
}
else {
findViewById(R.id.progress1).setVisibility(View.GONE);
}
}
回答by themaster33
If you are webmaster of the page which is loading in webview you can set your page title like '404'. Then after your page is loaded you can get page's title with the getTitle()
method. Then if it contains 404
or if it is different from your page title you can do what you want.
如果您是在 webview 中加载的页面的网站管理员,您可以将页面标题设置为“404”。然后在您的页面加载后,您可以使用该getTitle()
方法获取页面的标题。然后,如果它包含404
或与您的页面标题不同,您可以做您想做的。
Example:
例子:
myView.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
String pageTitle = myView.getTitle();
Toast.makeText(getBaseContext(), pageTitle, Toast.LENGTH_SHORT).show();
}
});