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?

miador
  • 358
  • 1
  • 5
  • 20
  • 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. – TheFunk Dec 18 '20 at 21:24
  • https://imgur.com/a/QXwg1zk you mean, here? It's also set to Java 11. – miador Dec 18 '20 at 21:27
  • 2
    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](https://developer.android.com/reference/java/net/package-summary)). The only chance would be to extract the necessary classes from JRE and add them to your project as library. – Robert Dec 21 '20 at 12:40
  • I didn't know it, thank you so much! – miador Dec 21 '20 at 21:31

0 Answers0