试用资源Android Studio

时间:2014-06-30 20:38:36

标签: java android android-studio

我的项目的“项目语言级别”= 6.0。我尝试将此更改为7.0,但这不会改变,我相信这是因为我选择了“最低要求的SDK”作为API 10,如果我想更改它,我需要选择API 19.这是需要的代码使用“语言水平”等于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());

1 个答案:

答案 0 :(得分:8)

如您所料,只有当您的minSdkVersion设置为19或更高时,才支持try-with-resources。

2014年3月增加了对Java 7语言功能的支持(see here)。但是,try-with-resources需要更新运行时(以及更新到核心Android OS),并且在API 19发布之前不会进行这些更改。