资源试用版Android Studio [英] Try-with-Resources Android Studio
问题描述
我有一个项目的项目语言级别" = 6.0.我尝试将其更改为7.0,但不会更改,我相信这是因为我选择了"Minimum required SDK"作为API 10,如果要更改它,则需要选择API19.这是需要修改的代码使用语言级别"等于7.0
I have a project that have a "Project language level" = 6.0. I try to change this to 7.0 but this not changes, I believe that is because I selected the "Minimum required SDK" as API 10 and if I want to change it I need to choose as API 19. This is the code that needs to use "Language level" equals to 7.0
URL url= new URL(strURL);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestProperty("Content-Type",
"application/json; charset=utf-8");
connection.setDoOutput(true);
connection.setRequestMethod("POST");
try (OutputStreamWriter out = new OutputStreamWriter(
connection.getOutputStream(), "UTF-8")) {
out.write(requestContent.toString());
}
StringBuilder responseBuilder = new StringBuilder();
try (BufferedReader in = new BufferedReader(new InputStreamReader(
connection.getInputStream(), "UTF-8"))) {
String buffer;
while ((buffer = in.readLine()) != null) {
responseBuilder.append(buffer);
}
}
return new JSONObject(responseBuilder.toString());
推荐答案
您怀疑,仅当您的minSdkVersion设置为19或更高时,才支持try-with-resources.
As you suspected, try-with-resources is only supported if your minSdkVersion is set to 19 or higher.
2014年3月添加了对Java 7语言功能的支持(请参见此处).但是,try-with-resources需要对运行时进行更新(因此也需要对核心Android OS进行更新),并且直到发布API 19才进行这些更改.
Support for Java 7 language features was added in March of 2014 (see here). However, try-with-resources requires updates to the runtime (and thus updates to the core Android OS), and those changes were not made until API 19 was released.
这篇关于资源试用版Android Studio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!