直接在自己的类中引入如下工具类,将需要判断的url作为构造函数的参数传递进去即可,如果是有效的url会抛出异常,如果是可用的url则会创建对象成功,并且可以通过对象获取到此url中相关的信息,比如协议头,端口,ip地址等
package test;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static android.util.Patterns.GOOD_IRI_CHAR;
/**
* Created by fuzhongbin on 2018/2/7.
*/
public class judgeUrl {
private String mScheme;
private String mHost;
private int mPort;
private String mPath;
private String mAuthInfo;
static final int MATCH_GROUP_SCHEME = 1;
static final int MATCH_GROUP_AUTHORITY = 2;
static final int MATCH_GROUP_HOST = 3;
static final int MATCH_GROUP_PORT = 4;
static final int MATCH_GROUP_PATH = 5;
/* ENRICO: imported the ParseExeption here */
public static class ParseException extends RuntimeException {
public String response;
ParseException(String response) {
this.response = response;
}
}
//根据正则表达式进行判断
static Pattern sAddressPattern = Pattern.compile(
/* scheme */ "(?:(http|https|file)\\:\\/\\/)?" +
/* authority */ "(?:([-A-Za-z0-9$_.+!*'(),;?&=]+(?:\\:[-A-Za-z0-9$_.+!*'(),;?&=]+)?)@)?" +
/* host */ "([" + GOOD_IRI_CHAR + "%_-][" + GOOD_IRI_CHAR + "%_\\.-]*|\\[[0-9a-fA-F:\\.]+\\])?" +
/* port */ "(?:\\:([0-9]*))?" +
/* path */ "(\\/?[^#]*)?" +
/* anchor */ ".*", Pattern.CASE_INSENSITIVE);
/** parses given uriString. */
//用法:将需要判断的url传入,new WebAddress(address) 如果抛出异常,则会出现问题
public judgeUrl(String address) throws ParseException {
if (address == null) {
throw new NullPointerException();
}
// android.util.Log.d(LOGTAG, "WebAddress: " + address);
mScheme = "";
mHost = "";
mPort = -1;
mPath = "/";
mAuthInfo = "";
Matcher m = sAddressPattern.matcher(address);
String t;
if (m.matches()) {
t = m.group(MATCH_GROUP_SCHEME);
if (t != null) mScheme = t.toLowerCase(Locale.getDefault());
t = m.group(MATCH_GROUP_AUTHORITY);
if (t != null) mAuthInfo = t;
t = m.group(MATCH_GROUP_HOST);
if (t != null) mHost = t;
t = m.group(MATCH_GROUP_PORT);
if (t != null && t.length() > 0) {
// The ':' character is not returned by the regex.
try {
mPort = Integer.parseInt(t);
} catch (NumberFormatException ex) {
throw new ParseException("Bad port");
}
}
t = m.group(MATCH_GROUP_PATH);
if (t != null && t.length() > 0) {
/* handle busted myspace frontpage redirect with
missing initial "/" */
if (t.charAt(0) == '/') {
mPath = t;
} else {
mPath = "/" + t;
}
}
} else {
// nothing found... outa here
throw new ParseException("Bad address");
}
/* Get port from scheme or scheme from port, if necessary and
possible */
if (mPort == 443 && mScheme.equals("")) {
mScheme = "https";
} else if (mPort == -1) {
if (mScheme.equals("https"))
mPort = 443;
else
mPort = 80; // default
}
if (mScheme.equals("")) mScheme = "http";
}
@Override
public String toString() {
String port = "";
if ((mPort != 443 && mScheme.equals("https")) ||
(mPort != 80 && mScheme.equals("http"))) {
port = ":" + Integer.toString(mPort);
}
String authInfo = "";
if (mAuthInfo.length() > 0) {
authInfo = mAuthInfo + "@";
}
return mScheme + "://" + authInfo + mHost + port + mPath;
}
public void setScheme(String scheme) {
mScheme = scheme;
}
public String getScheme() {
return mScheme;
}
public void setHost(String host) {
mHost = host;
}
public String getHost() {
return mHost;
}
public void setPort(int port) {
mPort = port;
}
public int getPort() {
return mPort;
}
public void setPath(String path) {
mPath = path;
}
public String getPath() {
return mPath;
}
public void setAuthInfo(String authInfo) {
mAuthInfo = authInfo;
}
public String getAuthInfo() {
return mAuthInfo;
}
}
希望对大家有用