简体   繁体   中英

send an Array from Android to PHP Script over HTTP and get the data

I would like to send an integer array with 30 values Integer[] temp = new Integer[30] over HTTP (POST) to PHP an get the data in PHP again. How can I do that? I know how to send a single value, like a string, but not how to send an array.

One way is to serialize the array in the way the php serialize command does it. After receiving the string value(by the method you currently use), you can use the unserialize command to get the array in your php code.

Here is a litle php example to demonstrate the workflow.

$arr = array(1,2,3,4,5,6);

$stringArr = serialize($arr);
echo $stringArr; 
echo "<br/>";

$arr2 = unserialize($stringArr);

if ($arr === $arr2)
{
  echo "arrays are equal";
}

The output of the script is:

a:6:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:4;i:4;i:5;i:5;i:6;}
arrays are equal

The main difficulty is to construct the resulting string for complex structures (in your case, it is pretty straight forward for an array of integers). This fact results in the second approach.

One can use a serialization API or another notation than used by the php example. As stated by the others, JSON is one of the widespread notations. PHP also provides a possibility to serialize and unserialize json objects. Simply use json_decode and look at the example in the manual.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM