1

See, I have to check like 50+ URLs for validity, and I'm assuming that catching more than 50 exceptions is kind of over the top. Is there a way to check if a bunch of URLs are valid without wrapping it in a try catch to catch exceptions? Also, just fyi, in Android the class "UrlValidator" doesn't exist (but it does exist in the standard java), and there's UrlUtil.isValidUrl(String url) but that method seems to be pleased with whatever you throw at it as long as it contains http://... any suggestions?

ZimZim
  • 3,291
  • 10
  • 49
  • 67
  • 1
    How do you define validity? Does it need to conform to the spec or do you want to perform a DNS and/or HTTP request to make sure it refers to a valid resource? Also, it will help you get more responses if you accept some of the answers to your previous questions. – Ian Newson Feb 12 '12 at 12:37
  • As in if the URL exists (i.e. if it doesn't load or returns a 404 error or something similar). – ZimZim Feb 12 '12 at 12:38
  • http://stackoverflow.com/questions/9198158/android-doesnt-recognize-org-apache-commons-validator and http://stackoverflow.com/questions/2230676/how-to-check-for-a-valid-url-in-java – Sergey Benner Feb 12 '12 at 12:39
  • Sergey, the only good answers are the ones which contain the UrlValidator class, and like I told you, Android doesn't have those classes... – ZimZim Feb 12 '12 at 12:41
  • as you read there mate you can package the library in your app – Sergey Benner Feb 12 '12 at 12:45
  • I know, just didn't want to use anything external. Anyway, I got it already. I just used getResponseCode() method from the HttpUrlConnection class. This gives me the integer 404 back when a page won't load. I used it before, but instead of testing for 404 I accidentally typed 400... so I was basically pondering for hours because of a stupid mistype... thanks for the help though guys – ZimZim Feb 12 '12 at 12:51
  • 1
    @user1007059 please, post as a answer and mark it as correct, so users that are trying to help don't waste time seeing this question ;) –  Feb 12 '12 at 13:03

1 Answers1

6

This solution does catch exceptions, however others may find it useful and doesn't require any libraries.

public boolean URLIsReachable(String urlString)
{
    try
    {
        URL url = new URL(urlString);
        HttpURLConnection urlConnection = (HttpURLConnection) url
                .openConnection();
        responseCode = urlConnection.getResponseCode();
        urlConnection.disconnect();
        return responseCode != 200;
    } catch (MalformedURLException e)
    {
        e.printStackTrace();
        return false;
    } catch (IOException e)
    {
        e.printStackTrace();
        return false;
    }
}
William Pownall
  • 760
  • 7
  • 9
  • I asked this a long time ago, and I'm far more experienced with Android/Java now, so I can tell that this answer is indeed one of the few answers that could do the trick. The else-statement isn't necessary though. Thanks for the answer! – ZimZim May 07 '13 at 11:21
  • I noticed it was asked quite a long time ago, I just found myself looking for a simple solution to the same problem so I thought I'd post it in case others found it useful :) – William Pownall May 21 '13 at 23:40
  • 1
    Wouldn't this need to be done asynchronously? Is just opening a connection exempted from the internet on UI thread rule? – Tonithy Jun 28 '13 at 23:53
  • Yep, as with all network operations, you'd likely want to do this in an IntentService or AsyncTask. – William Pownall Jul 01 '13 at 00:10