feat: Introduce Network Module and Basic Network Communication

- Implement `NetworkModule` for managing HTTP client with JSON content negotiation.
- Add `kotlinx-serialization-json` and `ktor-client` dependencies for network requests and serialization.
- Implement basic network communication in `Greeting` class to retrieve and display time data from a remote API.
- Add `NetworkResponse` and `TimeResponse` data classes for handling the response from the remote API.
- Refactor the `greet` method in `Greeting` class to integrate network request.
This commit is contained in:
grtsinry43 2025-04-23 09:26:24 +08:00
parent cfa6f13cdd
commit 8acd72bd9e
Signed by: grtsinry43
GPG Key ID: F3305FB3A978C934
4 changed files with 64 additions and 2 deletions

View File

@ -15,6 +15,8 @@ compose-multiplatform = "1.7.3"
junit = "4.13.2"
kotlin = "2.1.10"
kotlinx-coroutines = "1.10.1"
kotlinxSerializationJson = "1.6.2"
ktorClientCore = "2.3.8"
[libraries]
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
@ -30,6 +32,12 @@ androidx-activity-compose = { module = "androidx.activity:activity-compose", ver
androidx-lifecycle-viewmodel = { group = "org.jetbrains.androidx.lifecycle", name = "lifecycle-viewmodel", version.ref = "androidx-lifecycle" }
androidx-lifecycle-runtime-compose = { group = "org.jetbrains.androidx.lifecycle", name = "lifecycle-runtime-compose", version.ref = "androidx-lifecycle" }
kotlinx-coroutines-swing = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-swing", version.ref = "kotlinx-coroutines" }
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinxSerializationJson" }
ktor-client-android = { module = "io.ktor:ktor-client-android", version.ref = "ktorClientCore" }
ktor-client-content-negotiation = { module = "io.ktor:ktor-client-content-negotiation", version.ref = "ktorClientCore" }
ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktorClientCore" }
ktor-client-ios = { module = "io.ktor:ktor-client-ios", version.ref = "ktorClientCore" }
ktor-serialization-kotlinx-json = { module = "io.ktor:ktor-serialization-kotlinx-json", version.ref = "ktorClientCore" }
[plugins]
androidApplication = { id = "com.android.application", version.ref = "agp" }

View File

@ -30,6 +30,16 @@ kotlin {
sourceSets {
commonMain.dependencies {
// put your Multiplatform dependencies here
implementation(libs.ktor.client.core)
implementation(libs.ktor.client.content.negotiation)
implementation(libs.ktor.serialization.kotlinx.json)
implementation(libs.kotlinx.serialization.json)
}
androidMain.dependencies {
implementation(libs.ktor.client.android)
}
iosMain.dependencies {
implementation(libs.ktor.client.ios)
}
}
}

View File

@ -1,9 +1,36 @@
package com.grtsinry43.activityanalyzer
import io.ktor.client.call.body
import io.ktor.client.request.get
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
class Greeting {
private val platform = getPlatform()
fun greet(): String {
return "Hello, ${platform.name}!"
// val response: NetworkResponse =
// NetworkModule.httpClient.get("https://wlbzx.grtsinry43.com/api/time").body()
// println("response is : $response")
return "Hello, ${platform.name}! "
// +"Current time is ${response.data.time}"
}
}
}
@Serializable
data class NetworkResponse(
@SerialName("code")
val code: Int,
@SerialName("message")
val message: String,
@SerialName("data")
val data: TimeResponse
)
@Serializable
data class TimeResponse(
@SerialName("time")
val time: String,
@SerialName("count")
val count: Int
)

View File

@ -0,0 +1,17 @@
package com.grtsinry43.activityanalyzer
import io.ktor.client.HttpClient
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.serialization.kotlinx.json.json
import kotlinx.serialization.json.Json
object NetworkModule {
val httpClient = HttpClient {
install(ContentNegotiation) {
json(Json {
prettyPrint = true
isLenient = true
})
}
}
}