2

I want to pass two values(can be array or jsonobject in curly braces only) to a php webservice by okhttp in android and it is like:

  • userid="1" & productid={"1","2","3"}.

I know how to pass userid but I don't know how to pass productid values to url with the userid by okhttp. If it is a duplicate kindly give me the link I have tried to search it but did not find any relevant answer. Please help. Thanks in advance.

Chirag Gohil
  • 352
  • 1
  • 2
  • 14
  • See this http://stackoverflow.com/q/33307395/6848782 You can pass each element of array following this answer – Roger RV May 07 '17 at 14:50
  • you can convert that json object to string and then pass – Uday Naidu May 07 '17 at 14:51
  • @RogerRV Thanks for replying....I have checked the post but It will post parms as productid1="1", productid2="2", productid3="3"...but I want all of them in a single parameter like productid={"1","2","3"} and this can be anything array, arraylist, hashmap, jsonobject... – Chirag Gohil May 07 '17 at 15:04
  • @UdayNaidu can you elaborate your answer please...any code.. – Chirag Gohil May 07 '17 at 15:05
  • @ChiragGohil But if you added like key productid[0] and value 1 as the example you will receive it as an only variable in php. And if it's another type before passing you can transform your data into Array then make a loop to adding all elements in post in each array elements format – Roger RV May 07 '17 at 15:07
  • @ChiragGohil Try the answer I make – Roger RV May 07 '17 at 15:26

1 Answers1

2
    //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;
 }
?>
Roger RV
  • 132
  • 3
  • 15