I'm trying to fetch some data with Retrofit on my Android project update this on the ViewModel and my activity with LiveData.
Here is my service Class:
class PaymentService {
private var paymentMethodList = ArrayList<PaymentMethodModel>()
private val paymentMethodListLiveData = MutableLiveData<List<PaymentMethodModel>>()
init {
paymentMethodListLiveData.value = paymentMethodList
}
fun fetchPaymentMethods() {
val retrofit = Retrofit.Builder()
.baseUrl(SERVICE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
val service = retrofit.create(PaymentClient::class.java)
val jsonCall = service.getListOfPaymentMethods()
jsonCall.enqueue(object : Callback<List<PaymentMethodModel>> {
override fun onResponse(call: Call<List<PaymentMethodModel>>, response: Response<List<PaymentMethodModel>>) {
paymentMethodList = (response.body() as ArrayList<PaymentMethodModel>?)!!
}
override fun onFailure(call: Call<List<PaymentMethodModel>>, t: Throwable) {
//TODO
}
})
}
And here is where I'm trying to listen to the changes on the list:
goToNextButton.setOnClickListener {
paymentMethods = PaymentMethodSelectionViewModel().getAllPaymentMethods()
paymentMethods!!.observe(viewLifecycleOwner, Observer {
Log.e("", "")
})
}
The problem is that so far I'm getting the list only the first time with 0 elements and this observer method is not getting called after the rest call is made and the list updated.
Edit
class PaymentRepository {
private val paymentService = PaymentService()
fun getPaymentMethods(): LiveData<List<PaymentMethodModel>> {
paymentService.fetchPaymentMethods()
return paymentService.getPaymentMethods()
}
}
class PaymentMethodSelectionViewModel: ViewModel() {
private val paymentRepository = PaymentRepository()
private val paymentMethods = paymentRepository.getPaymentMethods()
fun getAllPaymentMethods(): LiveData<List<PaymentMethodModel>> {
paymentRepository.getPaymentMethods()
return paymentMethods
}
}
As @tyczj says in the comment, every time you use a LiveData, you have to decide when all the observers receive an update notification.
You can do this notification by calling post function of your paymentMethodListLiveData object. This is the correct way to use LiveData in Java .
In Kotlin I think you have to add something like this on your onResponse method:
paymentMethodListLiveData.value = paymentMethodList;
to implicitly call the post method and trigger methods in your observe function.
Hope this help or give you some hints.
Cheers
Change your request into viewmodel
class PaymentMethodSelectionViewModel: ViewModel() {
//Data
var paymentMethodList = MutableLiveData<List<PaymentMethodModel>>()
fun getAllPayments(){
val retrofit = Retrofit.Builder()
.baseUrl(SERVICE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
val service = retrofit.create(PaymentClient::class.java)
val jsonCall = service.getListOfPaymentMethods()
jsonCall.enqueue(object : Callback<List<PaymentMethodModel>> {
override fun onResponse(call: Call<List<PaymentMethodModel>>, response: Response<List<PaymentMethodModel>>) {
var data: List<PaymentMethodModel> = (response.body() as ArrayList<PaymentMethodModel>?)!!
paymentMethodList.value=data
}
override fun onFailure(call: Call<List<PaymentMethodModel>>, t: Throwable) {
//TODO
}
})
}
}
in your view (activity) use
//load
paymentMethodSelectionViewModel.getAllPayments();
//Observers
paymentMethodSelectionViewModel.paymentMethodList.observe(this,
Observer { list ->
// your code
})
I recommend you use retrofit 2 with corutines or RXJAVA2, check this tutorial
https://medium.com/@amtechnovation/android-architecture-component-mvvm-part-1-a2e7cff07a76
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.