我编写了以下代码,效果很好。
public void callingTheAPI() throws IOException{
BufferedReader bufferedReader= null;
CloseableHttpClient httpclient= null;
CloseableHttpResponse httpResponse= null;
try {
httpclient = HttpClients.createDefault();
HttpGet httpGet= new HttpGet(URL);
httpGet.addHeader("Authorization","Bearer "+getToken());
httpResponse=httpclient.execute(httpGet);
int responseCode= httpResponse.getStatusLine().getStatusCode();
System.out.println(responseCode);
bufferedReader= new BufferedReader(new InputStreamReader (httpResponse.getEntity().getContent()));
StringBuilder stringBuilder= new StringBuilder();
String line;
while((line=bufferedReader.readLine())!=null){
stringBuilder.append(line);
}
System.out.println(stringBuilder.toString());
}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if (bufferedReader != null){
bufferedReader.close();
}
if(httpResponse != null){
httpResponse.close();
}
if (httpclient != null){
httpclient.close();
}
}
}
最近我发现从try-with-resource
开始有一个名为java 7
的新功能,它会在语句正确运行后自动关闭资源。如何将以下代码转换为使用try-with-resource
语句。我做了一点但我对如何添加所有资源感到困惑。这是代码
public void closeResources(){
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet= new HttpGet(URL);
httpGet.addHeader("Authorization","Bearer "+getToken());
CloseableHttpResponse httpResponse=httpclient.execute(httpGet);
int responseCode= httpResponse.getStatusLine().getStatusCode();
System.out.println(responseCode);
try(BufferedReader bufferedReader= new BufferedReader(new InputStreamReader (httpResponse.getEntity().getContent()))){
StringBuilder stringBuilder= new StringBuilder();
String line;
while((line=bufferedReader.readLine())!=null){
stringBuilder.append(line);
}
System.out.println(stringBuilder.toString());
} catch (UnsupportedOperationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
是否可以帮助我转换此代码,以便它使用try-with-resources
语句。提前致谢
答案 0 :(得分:0)
使用“;
”作为分隔符来分隔资源。现在资源将以声明的相反顺序关闭,因此:BufferedReader,
InputStreamReader,CloseableHttpResponse。
您也可以从方法中返回可关闭的对象,如下例所示,这样您就可以在分配给try-catch块之前修改它们。
您可以查看本教程以展开多个try-catch-resources。
public void closeResources(){
try(CloseableHttpResponse httpResponse = createCloseableHttpResponse();
InputStreamReader reader = new InputStreamReader(httpResponse.getEntity().getContent());
BufferedReader bufferedReader= new BufferedReader(reader)){
StringBuilder stringBuilder= new StringBuilder();
String line;
while((line=bufferedReader.readLine())!=null){
stringBuilder.append(line);
}
System.out.println(stringBuilder.toString());
} catch (UnsupportedOperationException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private CloseableHttpResponse createCloseableHttpResponse() throws IOException{
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet= new HttpGet("");
httpGet.addHeader("Authorization","Bearer "+getToken());
return httpclient.execute(httpGet);
}