0

I am stuck at the conversion of JSON response into LiveData. This can be possible using Room. But I am not using Room in my app.

private fun fetchFromNetwork(dbSource: LiveData<T>) {
    //here 'result' is MediatorLiveData
    result.addSource(dbSource) { newData -> result.setValue(Resource.loading(newData)) }
     createCall().enqueue(object : Callback<V> {
         override fun onResponse(call: Call<V>, response: Response<V>) {
             result.removeSource(dbSource)

          //   response.body() is JSON response from server and need tobe convert into LiveData type

             result.addSource(convertedLiveData) { newData ->
                 if (null != newData)
                     result.value = Resource.success(newData)
             }
         }

         override fun onFailure(call: Call<V>, t: Throwable) {
             result.removeSource(dbSource)
             result.addSource(dbSource) { newData ->
                 result.setValue(
                     Resource.error(
                         getCustomErrorMessage(t),
                         newData
                     )
                 )
             }
         }
     })
 }
Kalu Khan Luhar
  • 1,044
  • 1
  • 22
  • 35
  • 1
    You cannot convert from JSON to `LiveData`; all you can do is to convert from JSON to an instance, and then set the value of a `LiveData` with said instance. And there is no way to do any of that with Room, so I am not sure why you even mention it. Please take a look at [Gson](https://github.com/google/gson), which is a library that you can use to deserialise JSON data. – Julio E. Rodríguez Cabañas May 10 '19 at 10:02
  • Hi @JulioE.RodríguezCabañas , thanks for the reply. I mentioned 'Room' here because being new to 'Android architecture components' because I have gone through several examples where we can get data in LiveData type from 'Dao'. Would you please provide code snippet demonstrating this using Gson? – Kalu Khan Luhar May 10 '19 at 10:27
  • I don't know exactly what you are trying to deserialise from JSON, but Gson has a lot of documentation and is very easy to use. – Julio E. Rodríguez Cabañas May 10 '19 at 10:31

1 Answers1

0

I have converted the server's response into MutableLiveData as below in code:

private fun fetchFromNetwork(dbSource: LiveData<T>) {
     result.addSource(dbSource) { newData -> result.setValue(Resource.loading(newData)) }
     createCall().enqueue(object : Callback<V> {
         override fun onResponse(call: Call<V>, response: Response<V>) {
             result.removeSource(dbSource)
            // here converting server response in to MutableLiveData
             val converted: MutableLiveData<T> = MutableLiveData()
             converted.value = response.body() as T
             result.addSource(converted) { newData ->
                 if (null != newData)
                     result.value = Resource.success(newData)
             }
         }

         override fun onFailure(call: Call<V>, t: Throwable) {
             result.removeSource(dbSource)
             result.addSource(dbSource) { newData ->
                 result.setValue(
                     Resource.error(
                         getCustomErrorMessage(t),
                         newData
                     )
                 )
             }
         }
     })
 }
Kalu Khan Luhar
  • 1,044
  • 1
  • 22
  • 35