Java Android-如何验证url和添加http://www
我想验证url字符串并在需要时添加。 url可能是google.com或google.co.in,因此很难在字符串末尾传递Java Android-如何验证url和添加http://www,java,android,Java,Android,我想验证url字符串并在需要时添加。 url可能是google.com或google.co.in,因此很难在字符串末尾传递 如何操作?如果您只想验证字符串是否为有效URL,可以使用类。示例代码也在那里。如果您只想验证您的字符串是有效的URL,您可以使用类。示例代码也在那里。您可以尝试正则表达式: if (!url.contains("http://www") { url = "http://www" + url; } public static final String URL_REG
如何操作?如果您只想验证字符串是否为有效URL,可以使用类。示例代码也在那里。如果您只想验证您的字符串是有效的URL,您可以使用类。示例代码也在那里。您可以尝试正则表达式:
if (!url.contains("http://www") {
url = "http://www" + url;
}
public static final String URL_REGEX = "^((https?|ftp)://|(www|ftp)\\.)?[a-z0-9-]+(\\.[a-z0-9-]+)+([/?].*)?$";
Pattern p = Pattern.compile(URL_REGEX);
Matcher m = p.matcher("example.com");//replace with string to compare
if(m.find()) {
System.out.println("String contains URL");
}
信用证:您可以尝试正则表达式:
public static final String URL_REGEX = "^((https?|ftp)://|(www|ftp)\\.)?[a-z0-9-]+(\\.[a-z0-9-]+)+([/?].*)?$";
Pattern p = Pattern.compile(URL_REGEX);
Matcher m = p.matcher("example.com");//replace with string to compare
if(m.find()) {
System.out.println("String contains URL");
}
信用证:好吧,在我看来,验证URL的最佳方法就是实际尝试。有很多方法可以搞乱URL,然后是http://或https://之类的东西 无论如何,如果您想实际测试URL,以确保其真正有效且实际在线,这里有一个替代方案。是的,我知道这要慢得多,但至少你肯定知道这是好的 以下是基本代码:
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
public class ValidateURL {
public static void main(String[] args) {
String urlString = "https://www.google.com";
// Make sure "http://" or "https://" is located
// at the beginning of the supplied URL.
if (urlString.matches("((http)[s]?(://).*)")) {
try {
final URL url = new URL(urlString);
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
int responseCode = huc.getResponseCode();
if (responseCode != 200) {
System.out.println("There was a problem connecting to:\n\n" +
urlString + "\n\nResponse Code: [" + responseCode + "]");
}
System.out.println("The supplied URL is GOOD!");
}
catch (UnknownHostException | MalformedURLException ex) {
System.out.println("Either the supplied URL is good or\n" +
"there is No Network Connection!\n" + urlString);
}
catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
}
嗯,在我看来,验证URL的最好方法就是实际尝试。有很多方法可以搞乱URL,然后是http://或https://之类的东西 无论如何,如果您想实际测试URL,以确保其真正有效且实际在线,这里有一个替代方案。是的,我知道这要慢得多,但至少你肯定知道这是好的 以下是基本代码:
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
public class ValidateURL {
public static void main(String[] args) {
String urlString = "https://www.google.com";
// Make sure "http://" or "https://" is located
// at the beginning of the supplied URL.
if (urlString.matches("((http)[s]?(://).*)")) {
try {
final URL url = new URL(urlString);
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
int responseCode = huc.getResponseCode();
if (responseCode != 200) {
System.out.println("There was a problem connecting to:\n\n" +
urlString + "\n\nResponse Code: [" + responseCode + "]");
}
System.out.println("The supplied URL is GOOD!");
}
catch (UnknownHostException | MalformedURLException ex) {
System.out.println("Either the supplied URL is good or\n" +
"there is No Network Connection!\n" + urlString);
}
catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
}
有很多方法。请发布您的尝试,以便您可以获得更好的帮助。我唯一认为的是使用endswith,但它不会有效…为什么使用endswith?它不是更愿意开始吗?有很多方法。请发布您的尝试,以便您可以获得更好的帮助。我唯一认为的是使用endswith,但它不会有效…为什么使用endswith?它不是更愿意开始吗?