Skip to content

Commit

Permalink
Merge pull request square#3089 from square/jwilson.0107.crawler_usabi…
Browse files Browse the repository at this point in the history
…lity

Make the crawler a little better.
  • Loading branch information
swankjesse authored Jan 7, 2017
2 parents de8699b + ca76167 commit b336a03
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion samples/crawler/src/main/java/okhttp3/sample/Crawler.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,15 @@ private void drainQueue() throws Exception {
continue;
}

Thread currentThread = Thread.currentThread();
String originalName = currentThread.getName();
currentThread.setName("Crawler " + url.toString());
try {
fetch(url);
} catch (IOException e) {
System.out.printf("XXX: %s %s%n", url, e);
} finally {
currentThread.setName(originalName);
}
}
}
Expand Down Expand Up @@ -114,7 +119,8 @@ public void fetch(HttpUrl url) throws IOException {
for (Element element : document.select("a[href]")) {
String href = element.attr("href");
HttpUrl link = response.request().url().resolve(href);
if (link != null) queue.add(link);
if (link == null) continue; // URL is either invalid or its scheme isn't http/https.
queue.add(link.newBuilder().fragment(null).build());
}
}

Expand Down

0 comments on commit b336a03

Please sign in to comment.