Java (Patterns.WEB_URL.matcher(URL).matches()工作不正常
我想检查页面是否存在。我使用了以下代码:Java (Patterns.WEB_URL.matcher(URL).matches()工作不正常,java,android,Java,Android,我想检查页面是否存在。我使用了以下代码: url = "https://muslimmemo.com/page/" + ++page; if (Patterns.WEB_URL.matcher(url).matches()) { Log.d("response","the url exists. page is " + page); Title title = new Title(
url = "https://muslimmemo.com/page/" + ++page;
if (Patterns.WEB_URL.matcher(url).matches())
{
Log.d("response","the url exists. page is " + page);
Title title = new Title(getApplicationContext());
title.execute();
}
else
{
Log.d("response","the url does not exist.");
}
在上面的代码中,尽管https://muslimmemo.com/page/7
不存在。页面不断增加且永不停止。如何解决此问题
Patterns.WEB_URL.matcher(URL.matches())
仅检查url是否具有有效语法,即类似于:000:/some的url。url
将无效
若要检查服务器上是否存在url,可以发送一个简单的GET请求,如下所示:
public boolean exists(String url){
HttpURLConnection huc = ( HttpURLConnection ) url.openConnection ();
huc.setRequestMethod ("GET"); //OR huc.setRequestMethod ("HEAD");
huc.connect () ;
int code = huc.getResponseCode() ;
if(code==200)
return true;
else
return false;
}
请不要忘记像其他任何网络操作一样在后台线程中发送它。您似乎误解了
matches()
方法。它只检测url
是否为有效格式。它无法检测url
是否存在。是这样吗?我有多傻?有什么帮助吗?