android-studio - 在 Android Kotlin 项目中使用 Java 11 库
问题描述
我有以下使用 Java 11 的 HttpClient 编写的 POST 请求。但 Android Studio 无法识别这些库。
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
class Verification {
fun verify(username: String, signature: String) {
try {
HttpClient.newHttpClient().send(HttpRequest.newBuilder()
.POST(HttpRequest.BodyPublishers.ofString("$username:$signature"))
.uri(URI.create("http://localhost:9080/sse/verify"))
.build(), HttpResponse.BodyHandlers.ofString())
} catch (e: Exception) {
e.printStackTrace()
}
}
}
build.gradle:
android {
compileSdkVersion 29
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "io.example.code"
minSdkVersion 26
targetSdkVersion 29
versionCode 1
versionName "1.0"
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = '11'
}
并且在Android Studio Project Structure
JDK 路径中设置为我的默认 JDKC:\Program Files\Java\jdk-11.0.8
也在模块中:
我究竟做错了什么?如何使用 Java 11 HttpClient 实现 Http 方法?
解决方案
推荐阅读
- assembly - x86 程序集:错误 A2008:语法错误:否定
- python - Flask / Gcloud 错误“内容必须通过 HTTPS 提供”
- c++ - vs 代码无法遵循相关的 gcc 构建错误链接
- node.js - NodeJS-Heroku 加载图片资源失败:服务器响应状态为 502 (Bad Gateway)
- javascript - Find the clients' IP address using an ExpressJS app
- node.js - 为 npx create-react-app 创建文件的问题
- jquery - Kendo-UI Router 无限路由
- spring - PropertyPlaceholderConfigurer 与 xml 配置中的配置文件
- javascript - 如何从多个输入中获取价值并使其成为一个输入?
- homebrew - SwiftGen 在启动时“杀死:9”,因为“代码签名无效”,但前提是通过 Homebrew 安装