Skip to content

Commit 6cddc89

Browse files
authored
Close all Response in samples (square#3574)
1 parent 33d31d0 commit 6cddc89

File tree

2 files changed

+40
-40
lines changed

2 files changed

+40
-40
lines changed

samples/crawler/src/main/java/okhttp3/sample/Crawler.java

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -95,32 +95,33 @@ public void fetch(HttpUrl url) throws IOException {
9595
Request request = new Request.Builder()
9696
.url(url)
9797
.build();
98-
Response response = client.newCall(request).execute();
99-
String responseSource = response.networkResponse() != null
100-
? ("(network: " + response.networkResponse().code() + " over " + response.protocol() + ")")
101-
: "(cache)";
102-
int responseCode = response.code();
103-
104-
System.out.printf("%03d: %s %s%n", responseCode, url, responseSource);
105-
106-
String contentType = response.header("Content-Type");
107-
if (responseCode != 200 || contentType == null) {
108-
response.body().close();
109-
return;
110-
}
98+
try (Response response = client.newCall(request).execute()) {
99+
String responseSource = response.networkResponse() != null ? ("(network: "
100+
+ response.networkResponse().code()
101+
+ " over "
102+
+ response.protocol()
103+
+ ")") : "(cache)";
104+
int responseCode = response.code();
105+
106+
System.out.printf("%03d: %s %s%n", responseCode, url, responseSource);
107+
108+
String contentType = response.header("Content-Type");
109+
if (responseCode != 200 || contentType == null) {
110+
return;
111+
}
111112

112-
MediaType mediaType = MediaType.parse(contentType);
113-
if (mediaType == null || !mediaType.subtype().equalsIgnoreCase("html")) {
114-
response.body().close();
115-
return;
116-
}
113+
MediaType mediaType = MediaType.parse(contentType);
114+
if (mediaType == null || !mediaType.subtype().equalsIgnoreCase("html")) {
115+
return;
116+
}
117117

118-
Document document = Jsoup.parse(response.body().string(), url.toString());
119-
for (Element element : document.select("a[href]")) {
120-
String href = element.attr("href");
121-
HttpUrl link = response.request().url().resolve(href);
122-
if (link == null) continue; // URL is either invalid or its scheme isn't http/https.
123-
queue.add(link.newBuilder().fragment(null).build());
118+
Document document = Jsoup.parse(response.body().string(), url.toString());
119+
for (Element element : document.select("a[href]")) {
120+
String href = element.attr("href");
121+
HttpUrl link = response.request().url().resolve(href);
122+
if (link == null) continue; // URL is either invalid or its scheme isn't http/https.
123+
queue.add(link.newBuilder().fragment(null).build());
124+
}
124125
}
125126
}
126127

samples/simple-client/src/main/java/okhttp3/sample/OkHttpContributors.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,22 @@ public static void main(String... args) throws Exception {
3131
.build();
3232

3333
// Execute the request and retrieve the response.
34-
Response response = client.newCall(request).execute();
35-
36-
// Deserialize HTTP response to concrete type.
37-
ResponseBody body = response.body();
38-
List<Contributor> contributors = CONTRIBUTORS_JSON_ADAPTER.fromJson(body.source());
39-
body.close();
40-
41-
// Sort list by the most contributions.
42-
Collections.sort(contributors, new Comparator<Contributor>() {
43-
@Override public int compare(Contributor c1, Contributor c2) {
44-
return c2.contributions - c1.contributions;
34+
try (Response response = client.newCall(request).execute()) {
35+
// Deserialize HTTP response to concrete type.
36+
ResponseBody body = response.body();
37+
List<Contributor> contributors = CONTRIBUTORS_JSON_ADAPTER.fromJson(body.source());
38+
39+
// Sort list by the most contributions.
40+
Collections.sort(contributors, new Comparator<Contributor>() {
41+
@Override public int compare(Contributor c1, Contributor c2) {
42+
return c2.contributions - c1.contributions;
43+
}
44+
});
45+
46+
// Output list of contributors.
47+
for (Contributor contributor : contributors) {
48+
System.out.println(contributor.login + ": " + contributor.contributions);
4549
}
46-
});
47-
48-
// Output list of contributors.
49-
for (Contributor contributor : contributors) {
50-
System.out.println(contributor.login + ": " + contributor.contributions);
5150
}
5251
}
5352

0 commit comments

Comments
 (0)