英文:
Kotlin HttpClient and HttpRequest unresolved reference errors on Intelij-idea
问题
以下是代码部分的中文翻译:
我在我的 Kotlin 代码中无法正确导入或映射 HttpClient 和 HttpRequest 的引用。我对 Kotlin 还很陌生,所以可能没有正确配置项目。我在 Mac 和 Windows 机器上都尝试过,都遇到了相同的问题。
我的导入语句:
import io.jsonwebtoken.Header
import io.jsonwebtoken.Jwts
import io.jsonwebtoken.io.Decoders
import io.jsonwebtoken.security.Keys
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.security.Key
import java.time.ZoneOffset
import java.time.ZonedDateTime
import java.util.*
import kotlin.collections.HashMap
在主函数中引起问题的代码行:
val client = HttpClient.newBuilder().build();
val request = HttpRequest.newBuilder()
.uri(URI.create("https://openapi.example.com/example/"))
.header("Authorization", "Bearer $jwt")
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(body))
.build();
我收到了未解决的引用错误。
注意:上述翻译中,HTML 实体编码(如"
)已被还原为正常的双引号。
英文:
I can't get references to HttpClient and HttpRequest to import/map properly in my Kotlin code. I'm new to Kotlin, so I might not being configuring the project correctly. I tried this both on Mac and Windows machines, and ran into the same issue.
My Import statements:
import io.jsonwebtoken.Header
import io.jsonwebtoken.Jwts
import io.jsonwebtoken.io.Decoders
import io.jsonwebtoken.security.Keys
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.security.Key
import java.time.ZoneOffset
import java.time.ZonedDateTime
import java.util.*
import kotlin.collections.HashMap
Lines causing issues in inside the main fuction:
val client = HttpClient.newBuilder().build();
val request = HttpRequest.newBuilder()
.uri(URI.create("https://openapi.example.com/example/"))
.header("Authorization", "Bearer $jwt")
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(body))
.build();
I receive unresolved reference errors:
Configuration:
- Intelij-IDEA with Kotlin (latest)
- Amazon Corretto 17
- Gradle 8.0.1
答案1
得分: 1
尝试打开您的 build.gradle
/build.gradle.kts
文件,并将 jvmToolchain
更改为 17。在 IDEA 中重新加载 Gradle。
英文:
The import java.net.http.HttpClient
was added in JDK 11 but your Gradle configuration should still uses a lower language level.
Try to open your build.gradle
/build.gradle.kts
, and change your jvmToolchain
to 17. Reload the Gradle in IDEA.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论