views:

123

answers:

3

SO i wan to check a "404 page not found" from a webview and if it's a 404 then i revert to the previus 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

+3  A: 

Attach a WebViewClient to your WebView, where you override onReceivedError() to find out about the 404 response -- though that will probably be returned as ERROR_FILE_NOT_FOUND.

CommonsWare
+1  A: 

I would try to detect loading of 404 page. You can do that by implementing shouldOverrideUrlLoading method in the WebViewClient class.

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.

darbat
A: 

I did it like this.It doen't need to download the while page to check if it's 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;
}
weakwire