快速的网址检查

时间:2018-07-26 13:40:59

标签: java android performance url

因此,我创建了一个android应用程序,该应用程序需要非常快地检查远程服务器上是否存在文件,因为在应用程序可用之前,我必须测试〜1000个链接。

我目前正在调用一个函数,该函数返回有效的URL,否则返回null

public String CheckUrl(String url) {
  try {
    URL urll = new URL(url);
    HttpURLConnection huc = (HttpURLConnection) urll.openConnection();

    huc.setRequestMethod("GET");  //OR  huc.setRequestMethod ("HEAD");
    huc.connect();
    int code = huc.getResponseCode();
    System.out.println(code);

    if (code == 200) {
      return url;
    } else {
      return null;
    }
  } catch (Exception e) {
    return null;
  }
}     

我这样使用它:

for (Element episode : episodes) {
  globalEpisodeCounter++;
  localEpisodeCounter++;

  MLP_Episode currentEpisode = new MLP_Episode();

  Elements links = episode.getElementsByTag("a");
  Element linkObj = links.get(0);
  Element thumObj = linkObj.getElementsByTag("img").get(0);
  Element titleObj = linkObj.getElementsByTag("b").get(0);

  int notRealsead = episode.getElementsByClass("btn btn-sm btn-error").size();
  Boolean epReleased = false;
  if (notRealsead == 0) {
    epReleased = true;
  }

  currentEpisode.url = "https://www.newlunarrepublic.fr" + linkObj.attributes().get("href");
  currentEpisode.thumbUrl = "https://www.newlunarrepublic.fr" + thumObj.attributes().get("src");
  currentEpisode.title = titleObj.text();
  currentEpisode.released = epReleased;
  currentEpisode.id_local = localEpisodeCounter;
  currentEpisode.id_global = globalEpisodeCounter;
  currentEpisode.in_season_num = seasonCounter;

  if (epReleased) {
    currentEpisode.url_vo_1080p = CheckUrl(
        "---------/NLR-1080p-" + addZero(seasonCounter) + "x" + addZero(localEpisodeCounter) + ".webm");

  }

  epList.add(currentEpisode);
}                        

在搜索的最后,搜索线程调用一个函数来更新UI

但是该功能的缺点是每秒1-2个链接的速度非常慢,在应用可用之前等待15分钟便会发送

1 个答案:

答案 0 :(得分:-1)

所以答案是在单独的线程中运行检查:

Thread thread = new Thread() {
    @Override
    public void run() {
        try {
            currentEpisode.url_vo_1080p = CheckUrl("------------/NLR-1080p-"+addZero(seasonCounter2)+"x"+addZero(localEpisodeCounter2)+".webm");
        }
        catch (Exception e) {}
    }
};

thread.start();
相关问题