android java 如何判断一个url地址为有效url

您所在的位置:网站首页 web还是android android java 如何判断一个url地址为有效url

android java 如何判断一个url地址为有效url

2024-07-09 21:13| 来源: 网络整理| 查看: 265

我们在android 和java开发中可能会遇到一个问题,就是判断用户输入的url或者从网页或其他地方复制来的url是否为一个有效的url,下面是一些方法,供大家参考:

在没有encode的情况下可以使用下面方法

URI uriObj = new URI(url);

这个方法来进行判定,如果该方法抛出异常URISyntaxException,那么就说明这个url不是一个正确的url,不过当遇到一个url地址中包含字符串^的时候,也会抛出异常,可以这个含有^的链接却是一个有效的地址,比如百度中的某些地址,因此可以通过下面的代码进行改进:

URL urlObj = new URL(url); URI uriObj = new URI(urlObj.getProtocol(), urlObj.getHost(), urlObj.getPath(), urlObj.getQuery(), null);这两个方法分别会抛出MalformedURLException和URISyntaxException.

不过如果你想对一些schemes进行限制,可以加入以下代码:

private static final String acceptableSchemes[] = { "http:", "https:", "file:" }; private static boolean urlHasAcceptableScheme(String url) { if (url == null) { return false; } for (int i = 0; i < acceptableSchemes.length; i++) { if (url.startsWith(acceptableSchemes[i])) { return true; } } return false; } private String isValidUrl(String incommingString) throws Exception{ String url = ""; URL urlObj = new URL(incommingString); URI uriObj = new URI(urlObj.getProtocol(), urlObj.getHost(), urlObj.getPath(), urlObj.getQuery(), null); String scheme = uriObj.getScheme(); if (!urlHasAcceptableScheme(incommingString)) { if (scheme != null) { throw new URISyntaxException("", ""); } } //此处需要对url进行赋值操作,例如需要加http://等   return url; } 对于android开发,还可以以这样处理:

import android.util.Patterns;//android包下的 if (Patterns.WEB_URL.matcher(searchContent).matches()) { //符合标准 } else{ //不符合标准 }

接下来提供一个完整的封装类,不但提供了对url的校验,还对url的大部分信息进行了封装操作,代码如下:

package 随便写; import static android.util.Patterns.GOOD_IRI_CHAR; import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; public class WebAddress { 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 WebAddress(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; } } 通过以上方法,可以判断出一个url是否为真正的url



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3