为我的 ViewModel class 和 Retrofit class 创建一个 LiveData class
Creating a LiveData class for my ViewModel class and Retrofit class
我正在努力为我的 ViewModel
class 和我的 Retrofit
class 创建一个 LiveData
class API 来电。
ViewModel
class:
class ApiViewModel : ViewModel() {
private lateinit var status: String
@NonNull
fun getApiResults(param1: String, param2: String, param3: String) {
viewModelScope.launch {
try {
val listResult = ApiCalls.retrofitService.ApiCallsService(param1, param2, param3)
} catch (e: Exception) {
status = "Failure: ${e.message}"
}
}
}
}
Retrofit
class:
private const val BASE_URL = "https://sampleapiurl.com"
private val moshi = Moshi.Builder().addLast(KotlinJsonAdapterFactory()).build()
private val client = OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.build()
private val retrofit = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create(moshi))
.baseUrl(BASE_URL)
.client(client)
.build()
interface ApiCallsService {
@GET(".")
suspend fun apiCalls(
@Query("param1") param1: String,
@Query("param2") param2: String,
@Query("param3") param3: String
): Call<CustomDataClass>
}
object ApiCalls {
val retrofitService: ApiCallsService by lazy { retrofit.create(ApiCallsService::class.java) }
}
现在,在 ViewModel
class 的 try catch
语句中,我想将 listResult
的结果传递给 LiveData
class.这样在我的 onViewCreated
片段中,我可以做这样的事情来检索数据:
val apiViewModel = ViewModelProvider(this).get(ApiViewModel::class.java)
apiViewModel.getApiResults("val1", "val2", "val3").observe(...
我的 LiveData
class 应该如何实现?
您可能需要创建 LiveData 来观察您想要的任何值。在您的情况下,您似乎需要观察 status
和 listResult
的值。因此,我建议您在 ApiViewModel
中创建 LiveData,即
val statusLiveData = MutableLiveData<String>()
val listResultLiveData = MutableLiveData<YourListType>()
和post在你的getApiResults()
中的值(修改你的getApiResults()
)
@NonNull
fun getApiResults(param1: String, param2: String, param3: String) {
viewModelScope.launch {
try {
listResultLiveData.postValue(ApiCalls.retrofitService.ApiCallsService(param1, param2, param3))
} catch (e: Exception) {
statusLiveData.postValue("Failure: ${e.message}")
}
}
}
并将其添加到 Fragment
中的 onViewCreated()
apiViewModel.statusLiveData.observe(viewLifecycleOwner, Observer {
// TODO: What to do with the status
})
apiViewModel.listResultLiveData.observe(viewLifecycleOwner, Observer {
// TODO: What to do on get list result
})
我正在努力为我的 ViewModel
class 和我的 Retrofit
class 创建一个 LiveData
class API 来电。
ViewModel
class:
class ApiViewModel : ViewModel() {
private lateinit var status: String
@NonNull
fun getApiResults(param1: String, param2: String, param3: String) {
viewModelScope.launch {
try {
val listResult = ApiCalls.retrofitService.ApiCallsService(param1, param2, param3)
} catch (e: Exception) {
status = "Failure: ${e.message}"
}
}
}
}
Retrofit
class:
private const val BASE_URL = "https://sampleapiurl.com"
private val moshi = Moshi.Builder().addLast(KotlinJsonAdapterFactory()).build()
private val client = OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.build()
private val retrofit = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create(moshi))
.baseUrl(BASE_URL)
.client(client)
.build()
interface ApiCallsService {
@GET(".")
suspend fun apiCalls(
@Query("param1") param1: String,
@Query("param2") param2: String,
@Query("param3") param3: String
): Call<CustomDataClass>
}
object ApiCalls {
val retrofitService: ApiCallsService by lazy { retrofit.create(ApiCallsService::class.java) }
}
现在,在 ViewModel
class 的 try catch
语句中,我想将 listResult
的结果传递给 LiveData
class.这样在我的 onViewCreated
片段中,我可以做这样的事情来检索数据:
val apiViewModel = ViewModelProvider(this).get(ApiViewModel::class.java)
apiViewModel.getApiResults("val1", "val2", "val3").observe(...
我的 LiveData
class 应该如何实现?
您可能需要创建 LiveData 来观察您想要的任何值。在您的情况下,您似乎需要观察 status
和 listResult
的值。因此,我建议您在 ApiViewModel
中创建 LiveData,即
val statusLiveData = MutableLiveData<String>()
val listResultLiveData = MutableLiveData<YourListType>()
和post在你的getApiResults()
中的值(修改你的getApiResults()
)
@NonNull
fun getApiResults(param1: String, param2: String, param3: String) {
viewModelScope.launch {
try {
listResultLiveData.postValue(ApiCalls.retrofitService.ApiCallsService(param1, param2, param3))
} catch (e: Exception) {
statusLiveData.postValue("Failure: ${e.message}")
}
}
}
并将其添加到 Fragment
中的onViewCreated()
apiViewModel.statusLiveData.observe(viewLifecycleOwner, Observer {
// TODO: What to do with the status
})
apiViewModel.listResultLiveData.observe(viewLifecycleOwner, Observer {
// TODO: What to do on get list result
})