Warum sind die Gerätedaten leer oder werden in meinen Methoden getDevice und getListDevice nicht gefunden?Android

Forum für diejenigen, die für Android programmieren
Guest
 Warum sind die Gerätedaten leer oder werden in meinen Methoden getDevice und getListDevice nicht gefunden?

Post by Guest »

Code: Select all

private val _getListTrustedDeviceState: MutableStateFlow =
MutableStateFlow(BaseUIState.Idle)
val getListTrustedDeviceState: StateFlow =
_getListTrustedDeviceState.asStateFlow()

private val _getTrustedDeviceByDeviceKey: MutableStateFlow =
MutableStateFlow(BaseUIState.Idle)
val getTrustedDeviceByDeviceKey: StateFlow =
_getTrustedDeviceByDeviceKey.asStateFlow()

fun getListTrustedDevice() {
viewModelScope.launch {
_getListTrustedDeviceState.value = BaseUIState.Loading

val accessToken = SharedPreferencesUtils().getDataFromSharedPreferences(
context, SharedPrefKey.USER_SECTION
)?.accessToken
if (accessToken.isNullOrBlank()) {
_getListTrustedDeviceState.value = BaseUIState.Error("Access token is missing")
return@launch
}
userRepository.getListTrustedDevices(accessToken).onSuccess { devices ->
_getListTrustedDeviceState.value = BaseUIState.Success(devices)
LogUtil.d("getListTrustedDevice: ${devices.toJson()}")
}.onFailure { error ->
_getListTrustedDeviceState.value = BaseUIState.Error(error.message ?: "An unknown error occurred")
}
}
}

fun getTrustedDeviceByDeviceKey(){
viewModelScope.launch {
_getTrustedDeviceByDeviceKey.value = BaseUIState.Loading

val accessToken = SharedPreferencesUtils().getDataFromSharedPreferences(
context, SharedPrefKey.USER_SECTION
)?.accessToken
val deviceKey = extractDataFromToken(accessToken.toString(), "device_key")
LogUtil.d("deviceKey: $deviceKey")
if (accessToken.isNullOrBlank()) {
_getTrustedDeviceByDeviceKey.value = BaseUIState.Error("Access token is missing")
return@launch
}
userRepository.getDevicesByKey(accessToken,deviceKey.toString()).onSuccess { devices ->
_getTrustedDeviceByDeviceKey.value = BaseUIState.Success(devices)
LogUtil.d("getTrustedDeviceByDeviceKey: ${devices.toJson()}")
}.onFailure { error ->
_getTrustedDeviceByDeviceKey.value = BaseUIState.Error(error.message ?: "An unknown error occurred")

}
}
}

private fun extractDataFromToken(token: String, key: String): String? {
try {
val payloadPart = token.split(".")[1]
val decodedPayload = String(Base64.decode(payloadPart, Base64.URL_SAFE))
val payloadJson = JSONObject(decodedPayload)
return payloadJson.optString(key)
} catch (e: Exception) {
e.printStackTrace()
}
return null
}
Ich arbeite an einer Android-App und verwende Kotlin mit einem Repository-Muster. Ich habe zwei Funktionen, getListTrustedDevice und getTrustedDeviceByDeviceKey, die eine API aufrufen, um eine Liste vertrauenswürdiger Geräte und Gerätedetails nach Geräteschlüssel abzurufen.
Das Problem ist, dass ich beim Aufruf dieser Methoden eine leere Meldung erhalte Antworten oder keine Gerätedaten, obwohl ich erwarte, dass Geräte vorhanden sind. Ich frage mich, ob das Problem mit dem Geräteschlüssel oder einem anderen Teil der Anfrage zusammenhängen könnte.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post