Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java (Patterns.WEB_URL.matcher(URL).matches()工作不正常_Java_Android - Fatal编程技术网

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
是否存在。是这样吗?我有多傻?有什么帮助吗?