How to check if URL is valid in Android

AndroidUrlUrl Validation

Android Problem Overview


Is there a good way to avoid the "host is not resolved" error that crashes an app? Some sort of a way to try connecting to a host ( like a URL ) and see if it's even valid?

Android Solutions


Solution 1 - Android

Use URLUtil to validate the URL as below.

 URLUtil.isValidUrl(url)

It will return True if URL is valid and false if URL is invalid.

Solution 2 - Android

URLUtil.isValidUrl(url);

If this doesn't work you can use:

Patterns.WEB_URL.matcher(url).matches();

Solution 3 - Android

I would use a combination of methods mentioned here and in other Stackoverflow threads:

public static boolean IsValidUrl(String urlString) {
    try {
        URL url = new URL(urlString);
        return URLUtil.isValidUrl(urlString) && Patterns.WEB_URL.matcher(urlString).matches();
    } catch (MalformedURLException ignored) {
    }
    return false;
}

Solution 4 - Android

If you are using from kotlin you can create a String.kt and write code bellow:

fun String.isValidUrl(): Boolean = Patterns.WEB_URL.matcher(this).matches()

Then:

String url = "www.yourUrl.com"
if (!url.isValidUrl()) {
    //some code
}else{
   //some code
}

Solution 5 - Android

Wrap the operation in a try/catch. There are many ways that a URL can be well-formed but not retrievable. In addition, tests like seeing if the hostname exists doesn't guarantee anything because the host might become unreachable just after the check. Basically, no amount of pre-checking can guarantee that the retrieval won't fail and throw an exception, so you better plan to handle the exceptions.

Solution 6 - Android

I have tried a lot of methods.And find that no one works fine with this URL:

Now I use the following and everything goes well.

public static boolean checkURL(CharSequence input) {
	if (TextUtils.isEmpty(input)) {
		return false;
	}
    Pattern URL_PATTERN = Patterns.WEB_URL;
	boolean isURL = URL_PATTERN.matcher(input).matches();
	if (!isURL) {
	    String urlString = input + "";
	    if (URLUtil.isNetworkUrl(urlString)) {
	        try {
	            new URL(urlString);
	            isURL = true;
	        } catch (Exception e) {
	        }
	    }
	}
	return isURL;
}

Solution 7 - Android

import okhttp3.HttpUrl;
import android.util.Patterns;
import android.webkit.URLUtil;

            if (!Patterns.WEB_URL.matcher(url).matches()) {
                error.setText(R.string.wrong_server_address);
                return;
            }

            if (HttpUrl.parse(url) == null) {
                error.setText(R.string.wrong_server_address);
                return;
            }

            if (!URLUtil.isValidUrl(url)) {
                error.setText(R.string.wrong_server_address);
                return;
            }

            if (!url.substring(0,7).contains("http://") & !url.substring(0,8).contains("https://")) {
                error.setText(R.string.wrong_server_address);
                return;
            }

Solution 8 - Android

In my case Patterns.WEB_URL.matcher(url).matches() does not work correctly in the case when I type String similar to "first.secondword"(My app checks user input). This method returns true.

URLUtil.isValidUrl(url) works correctly for me. Maybe it would be useful to someone else

Solution 9 - Android

You cans validate the URL by following:

Patterns.WEB_URL.matcher(potentialUrl).matches()

Solution 10 - Android

public static boolean isURL(String text) {
    String tempString = text;

    if (!text.startsWith("http")) {
        tempString = "https://" + tempString;
    }

    try {
        new URL(tempString).toURI();
        return Patterns.WEB_URL.matcher(tempString).matches();
    } catch (MalformedURLException | URISyntaxException e) {
        e.printStackTrace();
        return false;
    }
}

This is the correct sollution that I'm using. Adding https:// before original text prevents text like "www.cats.com" to be considered as URL. If new URL() succeed, then if you just check the pattern to exclude simple texts like "https://cats" to be considered URL.

Solution 11 - Android

Just add this line of code:

Boolean isValid = URLUtil.isValidUrl(url) && Patterns.WEB_URL.matcher(url).matches();       

Solution 12 - Android

new java.net.URL(String) throws MalformedURLException

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkidalexView Question on Stackoverflow
Solution 1 - Androiddd619View Answer on Stackoverflow
Solution 2 - AndroidPranavView Answer on Stackoverflow
Solution 3 - AndroidAyaz AlifovView Answer on Stackoverflow
Solution 4 - Androidjo joView Answer on Stackoverflow
Solution 5 - AndroidnobodyView Answer on Stackoverflow
Solution 6 - AndroidandroidyueView Answer on Stackoverflow
Solution 7 - AndroidKrzysztof DziubaView Answer on Stackoverflow
Solution 8 - AndroidJackky777View Answer on Stackoverflow
Solution 9 - AndroidMuhammad Naeem ParachaView Answer on Stackoverflow
Solution 10 - AndroidChirila VasileView Answer on Stackoverflow
Solution 11 - AndroidPrabhuView Answer on Stackoverflow
Solution 12 - AndroidkrekerView Answer on Stackoverflow