8 Replies - 12537 Views - Last Post: 28 October 2011 - 08:06 AM

#1 Tayacan   User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

Checking if a webpage exists

Posted 27 October 2011 - 01:23 PM

Hi dreamincode!

I've been playing around with android development for a few days - can't have a brand new smartphone without developing a bit for it - and I've decided on a small project to get started. I'm going to build a viewer for a comic called Weregeek, so I can read it on my android without having to load the entire webpage.

So what I want is to get the url of the actual image file and display that in a WebView. This works just fine, right now it automatically loads the comic of the day - if there is one. If there isn't, it goes to weregeeks own page-not-found page - naturally.

Now, what I would like to do is to check if the page I'm trying to load exists, and if not, then try with the previous day. I can figure out the part about going back one day on my own, right now I'm just trying to get some sort of error if the page doesn't exist. I'm not really sure how to go about that, though, since weregeek just sends me to a page not found without changing the url. My error catching works fine when I send it a garbage url, like "http://www.sklghnksj.com", but as soon as it's on the weregeek.com domain, it doesn't receive an error at all.

Example of weregeek's page-not-found: http://www.weregeek..../2011-10-27.jpg (removed the 'i' from 'comics').

Any ideas how to go about this?

Edit: Error catching code:

webber.setWebViewClient(new WebViewClient() {
        public void onReceivedError (WebView view, int errorCode, String description, String failingUrl) {
        	error.setText(errorCode);
        }
});


This post has been edited by Tayacan: 27 October 2011 - 01:26 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Checking if a webpage exists

#2 farrell2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 874
  • View blog
  • Posts: 2,706
  • Joined: 29-July 11

Re: Checking if a webpage exists

Posted 27 October 2011 - 08:17 PM

Instead of checking to see if a page exists, it is probably easier to just check the title of the page, and if it contains "404" or "page not found", you have your answer. WebView has a getTitle().
Was This Post Helpful? 2
  • +
  • -

#3 Tayacan   User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

Re: Checking if a webpage exists

Posted 27 October 2011 - 11:18 PM

That seems to be exactly what I need. Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 Tayacan   User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

Re: Checking if a webpage exists

Posted 28 October 2011 - 12:58 AM

Uh... Okay, interesting problem... calling webber.getTitle() doesn't seem to return a string. It doesn't seem to return anything at all, actually, and trying to use indexOf() or contains() on it just crashes the app - if I feed it an empty string, it doesn't crash, it just returns -1 or false. Trying to show the title in a TextView shows absolutely nothing.

This code causes a crash:

if(webber.getTitle().contains("Page not found")) {
	error.setText("Page not found");
}



Doing error.setText(webber.getTitle()); doesn't seem to be a problem, it just doesn't show anything.
Was This Post Helpful? 0
  • +
  • -

#5 farrell2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 874
  • View blog
  • Posts: 2,706
  • Joined: 29-July 11

Re: Checking if a webpage exists

Posted 28 October 2011 - 01:24 AM

Well, getTitle() only returns a string when the page is loaded. I just tested it with a webview and a button that logs the title. I have no more input on this. It may not be a good solution after all. Good luck. If you find something that works well, please share it.
Was This Post Helpful? 0
  • +
  • -

#6 farrell2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 874
  • View blog
  • Posts: 2,706
  • Joined: 29-July 11

Re: Checking if a webpage exists

Posted 28 October 2011 - 01:31 AM

Oh. Goodie. It looks like you can subclass WebViewClient and override onPageFinished() then you can grab the title of the page whe it's loaded. You need to do a webView.setWebViewClient() to tell your web view to listen to notifications from WebViewClient.

This was interesting.
Was This Post Helpful? 1
  • +
  • -

#7 Tayacan   User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

Re: Checking if a webpage exists

Posted 28 October 2011 - 07:08 AM

Thanks for all your help, farrell2k. I did end up finding a different solution, though. Here it is:

	public void getComic(Date day, WebView view, TextView error) {
		String url = "http://www.weregeek.com/comics/" + sdf.format(day) + ".jpg";
		int responseCode;
		
		try {
			HttpURLConnection.setFollowRedirects(false);
			URL testUrl;
			while(true) {
				testUrl = new URL(url);
				HttpURLConnection con = (HttpURLConnection) testUrl .openConnection();
				con.setRequestMethod("HEAD");
                                // Because of a weird bug documented at http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4191815, we have to call getResponseCode() twice.
				try {
					responseCode = con.getResponseCode();
				} catch (EOFException eof) {
					responseCode = con.getResponseCode();
				}
				if (responseCode == 404) {
					Date newDate = dayBefore(day);
					getComic(newDate,view,error); // Recursive function call, ha!
					error.setText(sdf.format(newDate));
	                break;
	            } else {
	            	view.loadUrl(url);
	            	break;
	            }
			}
		} catch (Exception e) {
			error.setText(e.toString());
		}
	}



Of course, I still need to work on this function, like putting in an edge case for the recursive call - but it works! Tested with the 29th (tomorrow) where it goes back to today's comic, and the 23rd, where it goes back to the 21st.
Was This Post Helpful? 2
  • +
  • -

#8 farrell2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 874
  • View blog
  • Posts: 2,706
  • Joined: 29-July 11

Re: Checking if a webpage exists

Posted 28 October 2011 - 07:49 AM

Nice. I learned something! Wouldn't it be better though to just use that as a separate utility method to determine whether or not your webview can load a comic, something like checkComic(comicUrl)?;

You start the utility method to check to see if a comic exists, and if it doesn't return 404 or -1, then you can safely load the comic. That way your getComic() isn't doing both the server check and loading, and there is no need for the messy recursion.

    if (checkComic(todaysComic) != 404)
            webView.loadUrl(todaysComic));
    else if
        (checkComic(yesterdaysComic) != 404)
            webView.loadUrl(yesterdaysComic));
    else
        ....you ran out of comics, so display an error.




Hmm. I think I confused myself. Does my code make sense?

This post has been edited by farrell2k: 28 October 2011 - 01:40 PM

Was This Post Helpful? 1
  • +
  • -

#9 Tayacan   User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

Re: Checking if a webpage exists

Posted 28 October 2011 - 08:06 AM

True! Well, the important thing is that I figured out how to do the check, so now I can re-arrange my code however I want.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1