1

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());
user3145102
  • 43
  • 2
  • 8

1 Answers1

8

As you suspected, try-with-resources is only supported if your minSdkVersion is set to 19 or higher.

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.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120