Search code examples
javaandroidretrofitretrofit2

Retrofit Android - How to save Response API Json into String-Array


Can anybody help me?

My response API :

{
    "status": true,
    "pesan": "Berhasil",
    "username": "user",
    "aktif_sampai": "2024-01-31",
    "is_mobile": "1",
    "listitem": [
        {
            "itemcode": "item00001"
        },
        {
            "itemcode": "item00002"
        }
    ]
}

I have created Retrofit ApiService class for the connection and have created the interface.

But I don't know how to make a model to store itemcode data into string-array into usable variable

Thanks for your help

store itemcode data into string-array into usable variable


Solution

  • You can not make it with only one model class. Because listitem array accepts objects not strings. So, you need to create listitem objects which includes itemcode string. Here is how you achieve it:

    data class User(
        val aktif_sampai: String,
        val is_mobile: String,
        val listitem: List<Listitem>,
        val pesan: String,
        val status: Boolean,
        val username: String
    )
    

    And class for the listitem:

    data class Listitem(
        val itemcode: String
    )
    

    Now you can create a list with items of listitem.