I have an App which repeatedly gets some data from an API. That works really good when I have a wifi connection. But with mobile data my request either times out or takes like 20 seconds. Which doesn't make sense because if I just type the API url in the android browser it almost immediately loads up. Also the API is a non https api I don't know if that matters. Any idea what could cause this? My Ktor Client:
object KtorClient {
val ktorClient = HttpClient(Android) {
install(DefaultRequest) {
header("apiKey", "apiKey")
}
install(ContentNegotiation) {
json()
}
defaultRequest {
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
}
}
}
My product repository:
object ProductRepository {
const val URL = "API"
const val LATEST_APK_URI = "$URL/apk"
suspend fun getProducts() = ktorClient.get("$URL/products").body<Map<String, List<ProductItem>>>().map {
it.key to it.value.sortedBy { item -> item.done }
}.toMap()
}
My viewmodel:
class ProductViewModel : ViewModel() {
val productFlow = mutableStateMapOf<String, List<ProductItem>>()
val errorFlow = MutableStateFlow<Error?>(null)
val lastRefreshFlow = MutableStateFlow<DateTimeTz?>(null)
fun refreshProducts(context: Context) {
viewModelScope.launch {
kotlin.runCatching {
ProductRepository.getProducts()
}.onSuccess {
productFlow.clear()
productFlow.putAll(it)
lastRefreshFlow.value = DateTime.nowLocal()
//updateOrder(context)
context.cacheFile.updateCache(productFlow)
}.onFailure {
errorFlow.value = SilentError("Konnte Produkte nicht neu laden", it)
}
}
}
}