🐛 Fixes ktor fetch not working

This commit is contained in:
Daniel Svitan 2025-05-17 11:15:40 +02:00
parent e4a2fdfd59
commit 3f5886ef4a
4 changed files with 32 additions and 10 deletions

View File

@ -52,7 +52,9 @@ dependencies {
implementation(libs.material)
implementation(libs.androidx.activity)
implementation(libs.ktor.client.core)
implementation(libs.ktor.client.cio)
implementation(libs.ktor.client.android)
implementation(libs.ktor.client.content.negotiation)
implementation(libs.ktor.serialization.kotlinx.json)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"

View File

@ -34,6 +34,10 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import dev.svitan.antifed.ui.theme.AntiFedTheme
import io.ktor.client.HttpClient
import io.ktor.client.engine.android.Android
import io.ktor.client.request.get
import io.ktor.http.HttpStatusCode
import kotlinx.coroutines.delay
@OptIn(ExperimentalMaterial3Api::class)
@ -45,8 +49,9 @@ class AuthActivity : ComponentActivity() {
AntiFedTheme {
var serverUrl by remember { mutableStateOf("") }
var checkingUrl by remember { mutableStateOf(false) }
var isServerUrlOk by remember { mutableStateOf(false) }
fun isServerUrlOkay(): Boolean {
fun serverUrlMatches(): Boolean {
var re = Regex(
"(?:http[s]?:\\/\\/.)?(?:www\\.)?[-a-zA-Z0-9@%._\\+~#=]{2,256}\\.[a-z]{2,6}\\b(?:[-a-zA-Z0-9@:%_\\+.~#?&\\/\\/=]*)",
RegexOption.DOT_MATCHES_ALL
@ -55,15 +60,23 @@ class AuthActivity : ComponentActivity() {
}
LaunchedEffect(serverUrl) {
if (serverUrl.isBlank()) return@LaunchedEffect
if (!isServerUrlOkay()) return@LaunchedEffect
if (serverUrl.isBlank()) {
isServerUrlOk = false
return@LaunchedEffect
}
if (!serverUrlMatches()) {
isServerUrlOk = false
return@LaunchedEffect
}
checkingUrl = true
delay(1000)
// val client = HttpClient(CIO)
// val response = client.get(serverUrl)
// println(response.status)
checkingUrl = true
val client = HttpClient(Android)
val response = client.get(serverUrl)
checkingUrl = false
isServerUrlOk = response.status == HttpStatusCode.OK
}
Scaffold(
@ -103,7 +116,7 @@ class AuthActivity : ComponentActivity() {
onValueChange = { serverUrl = it },
label = { Text(stringResource(R.string.server_url)) },
singleLine = true,
isError = !isServerUrlOkay(),
isError = !isServerUrlOk,
trailingIcon = {
if (checkingUrl)
CircularProgressIndicator(

View File

@ -13,6 +13,9 @@ material = "1.10.0"
activity = "1.10.1"
ktorClientCore = "3.1.3"
ktorClientCio = "3.1.3"
ktorClientAndroid = "3.1.3"
ktorClientContentNegotiation = "3.1.3"
ktorSerializationKotlinxJson = "3.1.3"
[libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version = "1.16.0" }
@ -26,7 +29,7 @@ androidx-ui = { group = "androidx.compose.ui", name = "ui" }
androidx-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" }
androidx-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" }
androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest", version = "1.8.1" }
androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
@ -34,6 +37,9 @@ material = { group = "com.google.android.material", name = "material", version =
androidx-activity = { group = "androidx.activity", name = "activity", version.ref = "activity" }
ktor-client-core = { group = "io.ktor", name = "ktor-client-core", version.ref = "ktorClientCore" }
ktor-client-cio = { group = "io.ktor", name = "ktor-client-cio", version.ref = "ktorClientCio" }
ktor-client-android = { group = "io.ktor", name = "ktor-client-android", version.ref = "ktorClientAndroid" }
ktor-client-content-negotiation = { group = "io.ktor", name = "ktor-client-content-negotiation", version.ref = "ktorClientContentNegotiation" }
ktor-serialization-kotlinx-json = { group = "io.ktor", name = "ktor-serialization-kotlinx-json", version.ref = "ktorSerializationKotlinxJson" }
[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }