0

I'm trying to write my network request to use Retrofit 2.6.0 and coroutines suspend fun. But i keep getting null object. This is the first time am trying retrofit 2.6 and coroutines

here is my sample code

Data class

data class ProjectList (val data: List<Project>)

Sample JSON Object

{
    "data": [
        {
            "project_id": "10824",
            "project_name": "Bendor Project",
            "project_number": "P010824",
            "content_items": [
                {
                    "content_id": "235",
                    "content_name": "Longonot Project",
                    "content_description": "Valves Example ",
                    "content_date_updated": "2019-08-31 12:29:00",
                    "project_id": "10824",
                    "media_items": []


Network Interface

    suspend fun getProjects(@Query("mode") mode: String): ProjectList

Retrofit Client

class RetrofitClient{


    private val gson = GsonBuilder()
        .setLenient()
        .create()


    private fun retrofit(): Retrofit = Retrofit.Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(GsonConverterFactory.create(gson))
        .build()

    val retrofitService: ProjectAPI by lazy {
        retrofit().create(ProjectAPI::class.java)
    }

Repository

class ProjectRepository {

    private val client: ProjectAPI = RetrofitClient().retrofitService

    suspend fun getProjectData(mode : String) : ProjectList = client.getProjects(mode)

}

livedata in ViewModel

val request : LiveData<ProjectList> = liveData(Dispatchers.IO){
            val response = repository.getProjectData(SOURCE_MODE)
            Log.e(TAG, "${response.data}")
            emit(response)
        }

I keep getting null response. Where am i not getting it right?

Kennan Obura
  • 315
  • 1
  • 4
  • 14
  • Looks like you are doing everything correctly. Can you change the type to `Response` and debug the request to see if the request matches what you are intended to send? – Saeed Entezari Sep 01 '19 at 07:12
  • I just replaced this ```val request : LiveData = liveData(Dispatchers.IO){ val response = repository.getProjectData(SOURCE_MODE) Log.e(TAG, "${response.data}") emit(response) }``` TO ``` viewModelScope.launch(Dispatchers.IO){ val response = repository.getProjectData("main") Log.e(TAG, "${response.data}") liveProjectData.postValue(response.data) }``` Trying to figure out the diffrence – Kennan Obura Sep 01 '19 at 07:19
  • share Project class first – Zafer Celaloglu Sep 01 '19 at 12:26
  • u're probably doing something wrong while parsing – Zafer Celaloglu Sep 01 '19 at 12:27
  • my another view is u don't need to emit values, just call suspend func. and then create LiveData in your ViewModel and last call livedata.post() in order to be able to assign network response to your LiveData – Zafer Celaloglu Sep 01 '19 at 12:28
  • @ZaferCelaloglu I had it working already with similar idea of your last comment. Thanks – Kennan Obura Sep 01 '19 at 13:50
  • then let me convert my comments to answers, please approve it :) – Zafer Celaloglu Sep 01 '19 at 13:55
  • @ZaferCelaloglu sure – Kennan Obura Sep 01 '19 at 13:57

1 Answers1

3

you don't need to emit values, just call your suspend func in coroutine scope and then create a new LiveData in your ViewModel and lastly call liveData.post() in order to be able to assign the response of network request to your LiveData object.

Zafer Celaloglu
  • 1,438
  • 1
  • 17
  • 28