StringBuilder result = new StringBuilder();
                HttpServletResponse resp = (HttpServletResponse)response;
                int status = 0;
                BufferedReader rd=null;
                CloseableHttpResponse closeableResponse = callApi(request);
                status= closeableResponse.getStatusLine().getStatusCode();
                logger.info("Response Code : {}", status);
                try(
                        rd = new BufferedReader(
                                        new InputStreamReader(closeableResponse.getEntity().getContent()))
                        ){

                        String line = "";
                        while ((line = rd.readLine()) != null) {
                                result.append(line);
                        }

                } finally{
                        if (rd!=null) rd.close();
                }


我在下面的行中得到预期的标识符:

rd =新的BufferedReader(
                                        新的InputStreamReader(closeableResponse.getEntity()。getContent()))
                        )

有什么想法吗?我已经定义了所有变量。为什么会出现此错误?

最佳答案

使用try-with-resources要求您在try部分中声明变量。

像这样:

try(BufferedReader rd = getBufferedReader()){
    dfadl
}catch(IOException e){
    dsfsd
}

关于java - Java 8:预期<identifier>,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56697304/

10-12 22:27