Search code examples
jsonkotlinmultidimensional-arrayretrofit2rx-java2

Retrieve MultiDimensional Array JSON in Kotlin Android


I want to get this kind of response (if a user search for specific keyword):

VendListRequest(row=50, page=1, query=[["vendorName", contain, "erlangga"],["status","true"]])

But I cannot get that kind of response. I have data class like this:

data class VendListRequest (
        val row : Int,
        val page : Int,
        val query : List<List<String>>
)

It can only retrieve the response like this:

(row=50, page=1, query=[["vendorName", contain, "erlangga"])

The cases that I want it to be are like these:

  1. When a user doesn't type any keyword on search box, the list will appear and the debug query response will be like this :

    VendListRequest(row=50, page=1, query=[["status", "true"]])

  2. When a user types any keyword on the search box, the list will appear and the debug query response will be like this:

    VendListRequest(row=50, page=1, query=[["vendorName", contain, "erlangga"],["status", "true"]])

here's my code in the kotlin class to retrieve the data:

 override fun getVendorList(row: Int, page: Int, vendorName: String?, query: String?, status: String?, isLoadMore: Boolean): Observable<SearchListResponse<VendorItem>> {
            var searchList: List<List<String>> = emptyList()
            if (!vendorName.isNullOrEmpty() && !query.isNullOrEmpty()) {
                searchList = listOf(status, EQUALS, true)
            }
    //___________________________________________________________________
    }

Solution

  • I finally found the answer. This is what I did :

    1. I added a new parameter inside my repository

    Before :

    fun getVendorList(row: Int, page: Int, vendorName: String?, query: String?, isLoadMore: Boolean) : Observable<Response<VendorItem>>
    

    After :

    fun getVendorList(row: Int, page: Int, vendorName: String?, query: String?, status: String, isLoadMore: Boolean) : Observable<Response<VendorItem>>
    
    1. Create a new List variable "statusList" and add it to the model class, after that, we only need to call the REST API.

    val vendListReq= VendListReq(row, page, searchList + statusList)

    then call it in :

    .concatMap { remRegData.getVendList(it, vendListReq) }