1

I have the following POST request that's written by using Java 11's HttpClient. But Android Studio does not recognize the libraries.

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'
    }

And in Android Studio Project Structure the JDK path is set to my default JDK C:\Program Files\Java\jdk-11.0.8 also in modules:

enter image description here

What am i doing wrong? What can i do to implement a Http method with Java 11 HttpClient?

4
  • Under project structure in the menu on the left you should have module settings as well as project settings. If you click on "projects" you should see which JDK you're using. If you click on "Modules" there should be a "language level" at the top of that settings window, indicating what Java version the module is written to support. Commented Dec 18, 2020 at 21:24
  • imgur.com/a/QXwg1zk you mean, here? It's also set to Java 11. Commented Dec 18, 2020 at 21:27
  • 3
    Increasing the language level is of no use in this case as the used classes like java.net.http.HttpClient are simply not available on Android (see available java.net classes). The only chance would be to extract the necessary classes from JRE and add them to your project as library. Commented Dec 21, 2020 at 12:40
  • I didn't know it, thank you so much! Commented Dec 21, 2020 at 21:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.