Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check from Android WebView if a page is a "404 page not found"?

Tags:

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

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.

like image 334
weakwire Avatar asked Jul 05 '10 19:07

weakwire


People also ask

How do I get a 404 error page?

There are several reasons why you might be getting an HTTP 404 code: One typical trigger for an error 404 message is when the page has been deleted from the website. The page was moved to another URL and the redirection was done incorrectly. You entered an incorrect URL address.

What does it mean when a web page says 404 not found?

404 is a status code that tells a web user that a requested page is not available. 404 and other response status codes are part of the web's Hypertext Transfer Protocol response codes. The 404 code means that a server could not find a client-requested webpage.


1 Answers

Starting SDK 23 (Android M) you can use onReceivedHttpError method to catch 404 and other HTTP errors. Just override inside your WebViewClient

@Override public void onReceivedHttpError (WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {     Toast.makeText(view.getContext(), "HTTP error "+errorResponse.getStatusCode(), Toast.LENGTH_LONG).show(); } 
like image 178
Alexey Ozerov Avatar answered Oct 12 '22 12:10

Alexey Ozerov