Search code examples
androidjsonandroid-studiokotlinretrofit

how call data from json like this with Retrofit library


i tried this :
interface MYAPI {
 @GET("get-languages")
 fun getdata() : Call<List<Data.Language>>
 } 
this is my api service   

{ "message": "success", "data": { "language": [ {"id": 5, "name": "English", "icon": "19638193-en.png" }, { "id": 6,"name": "turkish","icon": "19638199-tr.png"} ] } }


Solution

  • {
    
       "message":"success",
    
       "data":{
    
          "language":[]
    
       }
    
    }
    

    Share your response model but you will need the "data" attribute that is a language model. If you use directly the language model then won´t works.

    So a possible data could be:

    data class LanguageResponse(val id: Int, val name: String, val icon: String)
    data class LanguagesResponse(val language: List<LanguageResponse>)
    data class DataLanguageResponse(val data: LanguagesResponse)
    

    And your call:

    interface MYAPI {
     @GET("get-languages")
     fun getdata() : Call<DataLanguageResponse>
    }