//Convert your object to an array
FormBody.Builder formBuilder = new FormBody.Builder();
for(int x=0; x<array.length; x++)
{
String val = (String) array[x];
formBuilder.add("productid["+x+"]",val);
}
RequestBody body = formBuilder.build();
Request request = new Request.Builder()
.url(YOUR_URL)
.post(body)
.build();
Note: if you want custom index not numeric arrays, in php arrays can be indexed by non-numeric index so you can loop any type of variable with a non-numeric index
- Option 1 (with examples following the previus note):
FormBody.Builder formBuilder = new FormBody.Builder();
//Check if object is HashMap
if(object instanceof HashMap)
{
for(Entry<String, String> e : m.entrySet()) {
String key = e.getKey();
String val = e.getValue();
formBuilder.add("productid["+key+"]",val);
}
} else if(object instanceof Array) {
for(int x=0; x<array.length; x++)
{
String val = (String) array[x];
formBuilder.add("productid["+x+"]",val);
}
}
RequestBody body = formBuilder.build();
Request request = new Request.Builder()
.url(YOUR_URL)
.post(body)
.build();
- Option 2 (Bad practice for me)
Considering that you only want to pass only one param and .add() method only can pass String, you can convert all objects to an Array and then to String. String to 1 dimensional array:
<?php
$productidstring = $_POST['productid'];
$productid = StringToArray($productidstring);
function StringToArray($productidstring)
{
$productidstringtmp = str_replace("[","",$productidstring);
$productidstringtmp = str_replace("]","",$productidstring);
$productidarray = explode(",",$productidstringtmp);
return $productidarray;
}
?>