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?
-
how are you connecting to the host? – SteelBytes Feb 05 '11 at 04:21
12 Answers
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.

- 5,910
- 8
- 35
- 60
-
isValidUrl returns false for the following URL, although the device seems happy enough with it (it's missing the double slash after file:). "file:/storage/emulated/0/Android/data/com.samsung.android.app.pinboard/files/ClipData/Screenshot_NormarAppImage.png" – Tom Hamshere Jan 14 '14 at 13:42
-
8I would not rely on this method, because it doesn't perform deep validation, only most simple WebView cases. See [source](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4.2_r1/android/webkit/URLUtil.java#URLUtil.isValidUrl%28java.lang.String%29) – marwinXXII Jun 08 '14 at 15:31
-
21Better answer: http://stackoverflow.com/questions/5617749/how-to-validate-a-url-website-name-in-edittext-in-android/7754297 – marwinXXII Jun 08 '14 at 15:33
-
2it seems that as long as it has `http://` or `https://` it will return true – hehe Jun 06 '17 at 05:38
-
Would this help avoid urls like mailto: abc@xyz.com and about:blank#blocked – RamPrasadBismil Apr 28 '20 at 21:03
URLUtil.isValidUrl(url);
If this doesn't work you can use:
Patterns.WEB_URL.matcher(url).matches();

- 60,504
- 58
- 273
- 437

- 4,172
- 3
- 30
- 31
-
1Does this not just check the pattern rather than whether the URL is actually active? – Declan McKenna Apr 29 '16 at 13:25
-
1As far as i know,if we need to check whether url is actually pinging or not we should manually check for that using HttpURLConnection class. – Pranav May 01 '16 at 14:24
-
Use the second option if you need to check urls without schema. URLUtil.isValidUrl will return false if schema is not added. – user3783123 Sep 27 '19 at 08:47
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;
}

- 8,334
- 4
- 61
- 56
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
}

- 1,758
- 3
- 20
- 34
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.

- 19,814
- 17
- 56
- 77
-
I don't mind handling the exception but how do I prevent the app from crashing due to that very exception? – kidalex Feb 05 '11 at 04:49
-
6@kidalex Err, *catch* the exception? What *else* could 'handling the exception' possibly mean? – user207421 Feb 25 '15 at 09:22
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;
}

- 9,501
- 5
- 69
- 106

- 952
- 10
- 11
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;
}

- 581
- 6
- 9
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

- 644
- 12
- 19
You cans validate the URL by following:
Patterns.WEB_URL.matcher(potentialUrl).matches()

- 2,335
- 2
- 18
- 23
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
.

- 359
- 1
- 2
- 11
Just add this line of code:
Boolean isValid = URLUtil.isValidUrl(url) && Patterns.WEB_URL.matcher(url).matches();

- 306
- 2
- 7