Home » Php » How to build a complex retrofit call that use Associative Array as Parameter?

How to build a complex retrofit call that use Associative Array as Parameter?

Posted by: admin September 25, 2018 Leave a comment

Questions:

Im building an Android App that make some calls to Magento API 2.2, that is developed with PHP and supports associative arrays. (Like $age[‘Peter’] = “35”)

The problem is some of the endpoints receive an associative array as parameter. For example, to get products :

GET http://<magento_host>/index.php/rest/V1/products?
searchCriteria[filter_groups][0][filters][0][field]=name&
searchCriteria[filter_groups][0][filters][0][value]=%25Leggings%25&
searchCriteria[filter_groups][0][filters][0][condition_type]=like&
searchCriteria[filter_groups][0][filters][1][field]=name&
searchCriteria[filter_groups][0][filters][1][value]=%25Parachute%25&
searchCriteria[filter_groups][0][filters][1][condition_type]=like

But i have no clue in how to make this in Android ! I’m used to send simple arrays as parameter in retrofit, but this one is too complex.

interface MagentoProductApi {
    @GET(MAGENTO_V1_PREFIX + "/products")
    fun products(@Query params:): Single<MagentoProductsReceiver>   
}

At this moment, i’m trying to build an Equivalent JSON for this parameter, so then i can make a couple of POJO’s to send. Unfortunately, i’m not getting the right format. See below what i got so far:

{  
   "searchCriteria":[  
      "filterGroups":[  
         {  
            "filters":[  
               {  
                  "field":"sku",
                  "value":"WSH%31%",
                  "condition_type":"like"
               },
               {  
                  "field":"sku",
                  "value":"WSH%31%",
                  "condition_type":"like"
               },
               {  
                  "field":"sku",
                  "value":"WSH%31%",
                  "condition_type":"like"
               }
            ]
         }
      ]
   ]
}

or something like :

{  
   "searchCriteria":{  
      "filterGroups":{  
         "0":{  
            "filters”:[  
               "0":{  
                  "field":"sku",
                  "value":"WSH%31%",
                  "condition_type":"like"
               },
                 “1”:{  
                  "field":"sku",
                  "value":"WSH%31%",
                  "condition_type":"like"
               },
            ]
         }
      }
   }
}

Any help ?

Answers: