-1

Error:(226, 34) java: cannot find symbol symbol: variable BodyPublisher location: class jdk.incubator.http.HttpRequest

private static JSONObject sendRequest(JSONObject json) throws Exception {
    HttpClient client;
    InetSocketAddress proxy = getRandomProxy();

    if(proxy == null) {
        client = HttpClient.newHttpClient();
    } else {
        client = HttpClient.newBuilder().proxy(ProxySelector.of(proxy)).build();
    }

    HttpRequest httpRequest = HttpRequest
            .newBuilder(new URI(BASE_URL))
            .header("Accept", "application/json")
            .header("Content-Type", "application/json")
            .timeout(TIMEOUT_DURATION)
            .POST(HttpRequest.BodyPublisher.fromString(json.toString()))
            .build();

    HttpResponse<String> httpResponse = client.send(httpRequest, HttpResponse.BodyHandler.asString());
    String jsonResponse = httpResponse.body();

    return new JSONObject(jsonResponse);
}

module-info.java:

module MyProject {

    requires jdk.incubator.httpclient;

}

I've tried it with OpenJDK 10 and Oracle JDK 10 on Linux.

Celsius
  • 104
  • 1
  • 9
  • 1
    Works fine for me in a modularised code. Could you share a reproducible instance or further details of how you are executing the code? – Naman Apr 17 '18 at 14:52
  • I'm using IntelliJ Idea 2017.3.2 with JDK 10 (readme: [https://hastebin.com/setuyexiqo.ini]. For example this won't work for me: `System.out.println(HttpRequest.BodyPublisher.fromString("Test").hashCode());` – Celsius Apr 18 '18 at 12:35

1 Answers1

0

You could check the target bytecode version of your module (File > Settings > Build, Execution, Deployment > Compiler> Java Compiler), it should be set to 10. You might also check the project SDK and language level settings under File > Project Settings > Project and File > Project Settings > Modules.