-1

错误:(226、34)java:找不到符号符号:变量 BodyPublisher 位置:类 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);
}

模块信息.java:

module MyProject {

    requires jdk.incubator.httpclient;

}

我已经在 Linux 上使用 OpenJDK 10 和 Oracle JDK 10 进行了尝试。

4

1 回答 1

0

您可以检查模块的目标字节码版本(File > Settings > Build, Execution, Deployment > Compiler> Java Compiler),它应该设置为 10。您还可以在File > Project Settings下检查项目 SDK 和语言级别设置> 项目文件 > 项目设置 > 模块

于 2018-04-24T09:48:52.333 回答