简体   繁体   中英

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.

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:

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:

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. 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. These are Java SE classes that were introduced in Java 11.

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).

I recommend that you use one of the 3rd-party libraries listed in Make an HTTP request with android . The respective documentation will include examples for you to follow... if that is what you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM