[英]Can't import java.net.http into my Android project
While trying to use GET request in my project, I stumbled a problem: I can't import java.net.http.在我的项目中尝试使用 GET 请求时,我遇到了一个问题:我无法导入 java.net.http。
Here's part of my code (just an example, that I'm trying to reproduce):这是我的部分代码(只是一个例子,我正在尝试重现):
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
fun String.utf8(): String = URLEncoder.encode(this, "UTF-8")
fun main() {
val params = mapOf("name" to "John Doe", "occupation" to "gardener")
val urlParams = params.map {(k, v) -> "${(k.utf8())}=${v.utf8()}"}
.joinToString("&")
val client = HttpClient.newBuilder().build();
val request = HttpRequest.newBuilder()
.uri(URI.create("https://httpbin.org/get?${urlParams}"))
.build();
val response = client.send(request, HttpResponse.BodyHandlers.ofString());
println(response.body())
}
Build output:构建 output:
e: C:\Users\BJayD\AndroidStudioProjects\Viva_La_Resistance_Radio\VLR-Radio\app\src\main\java\com\example\viva_la_resistance_radio\Info.kt: (16, 17): Unresolved reference: http
e: C:\Users\BJayD\AndroidStudioProjects\Viva_La_Resistance_Radio\VLR-Radio\app\src\main\java\com\example\viva_la_resistance_radio\Info.kt: (17, 17): Unresolved reference: http
e: C:\Users\BJayD\AndroidStudioProjects\Viva_La_Resistance_Radio\VLR-Radio\app\src\main\java\com\example\viva_la_resistance_radio\Info.kt: (18, 17): Unresolved reference: http
e: C:\Users\BJayD\AndroidStudioProjects\Viva_La_Resistance_Radio\VLR-Radio\app\src\main\java\com\example\viva_la_resistance_radio\Info.kt: (29, 18): Unresolved reference: HttpClient
e: C:\Users\BJayD\AndroidStudioProjects\Viva_La_Resistance_Radio\VLR-Radio\app\src\main\java\com\example\viva_la_resistance_radio\Info.kt: (30, 19): Unresolved reference: HttpRequest
e: C:\Users\BJayD\AndroidStudioProjects\Viva_La_Resistance_Radio\VLR-Radio\app\src\main\java\com\example\viva_la_resistance_radio\Info.kt: (34, 41): Unresolved reference: HttpResponse
e: C:\Users\BJayD\AndroidStudioProjects\Viva_La_Resistance_Radio\VLR-Radio\app\src\main\java\com\example\viva_la_resistance_radio\Info.kt: (129, 23): Unresolved reference: HttpRequest
Here's my buld.gradle, since I found that the problem is usually with incompatibility between Java 8 and 11:这是我的 buld.gradle,因为我发现问题通常与 Java 8 和 11 之间的不兼容有关:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0'
}
}
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
namespace 'com.example.viva_la_resistance_radio'
compileSdk 33
defaultConfig {
applicationId "com.example.viva_la_resistance_radio"
minSdk 21
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = '11'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.appcompat:appcompat:1.6.0'
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
I updated Java already, changed settings, but nothing helps.我已经更新了 Java,更改了设置,但没有任何帮助。 I think I've made a mistake somewhere else, but can't find anything.
我想我在其他地方犯了一个错误,但找不到任何东西。
The classes you are trying to use are not part of the Android platform.您尝试使用的类不是 Android 平台的一部分。 These are Java SE classes that were introduced in Java 11.
这些是在 Java 11 中引入的 Java SE 类。
Unless the answers to How can I use JDK 11 HTTP packages in Android are out of date, there are no ports of the java.net.http.*
classes to Android (yet).除非Android 中 How can I use JDK 11 HTTP packages 的答案已过时,否则
java.net.http.*
类没有端口到 Android(目前)。
I recommend that you use one of the 3rd-party libraries listed in Make an HTTP request with android .我建议您使用使用android 发出 HTTP 请求中列出的第 3 方库之一。 The respective documentation will include examples for you to follow... if that is what you need.
相应的文档将包含供您遵循的示例……如果您需要的话。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.