Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing ArrayList to PHP using Android Asynchronous Http Client Issue

Im trying to send ArrayList to PHP ($_POST) and use it as an array. I use Android Asynchronous Http Client http://loopj.com/android-async-http/

My code

ArrayList<String> array = new ArrayList<String>();
array.add("1");
array.add("2");
array.add("3");
RequestParams params = new RequestParams();
params.put("descr_array", array );
AsyncHttpClient client = new AsyncHttpClient();
client.post(StaticValues.SERVER_PAGE, params, new AsyncHttpResponseHandler() { ... }

In PHP

$descrarray[] = $_POST['descr_array'];

shows only one item, as a string.

$result = print_r($_POST);
print(json_encode($result));

Debugging code shows

Array ( [descr_array] => 1 )

There is only one string, how can I solve it?

like image 493
A7madev Avatar asked Feb 14 '26 06:02

A7madev


1 Answers

If you want to get all values write:

RequestParams params = new RequestParams();
params.put("descr_array_1", array.get(0) );
params.put("descr_array_2", array.get(1) );
params.put("descr_array_2", array.get(2) );

But I prefer you to use Gson to convert Array to String and use one pair only:

Gson gson = new Gson(); 
String out = gson.toJson(array);
params.put("descr_array", out ); 
like image 50
Maxim Shoustin Avatar answered Feb 15 '26 20:02

Maxim Shoustin