示例代码

try (CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;) {

URIBuilder builder = new URIBuilder(url); // 创建uri
if (param != null) {
for (String key : param.keySet()) {
builder.addParameter(key, param.get(key));
}
}

URI uri = builder.build();

HttpGet httpGet = new HttpGet(uri); // 创建http GET请求

response = httpclient.execute(httpGet); // 执行请求
} catch (Exception e) {
log.info("exception: " + e);
}

出现错误提示:Cannot assign a value to final variable 'response'

Java - try-with-resources_get请求

说明 try() 里的是final变量,不能改变,仅此标记。