3

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?

2
  • 1
    use json to get the values in php. Commented Sep 30, 2013 at 12:50
  • Name your field descr_array[] instead of desc_array. The extra [] is a signal to PHP to treat multiple of the same input name as an array, rather than only using the last value. Commented Sep 30, 2013 at 13:06

3 Answers 3

3

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 ); 
Sign up to request clarification or add additional context in comments.

Comments

0

This is what worked for me:

RequestParams params = new RequestParams();

for (String val : array) {
    params.add("descr_array[]", val);
}

It doesn't require you to add extra Headers or import other libraries and works for both JSON and XML.


The important thing to note here is the use of add instead of put. See the difference between the two.

Comments

0

This worked for me...

Map<String, String> aMap = new HashMap<String, String>();
aMap.put("f_name" ,"");
aMap.put("l_name" ,"");
aMap.put("email" ,edtEmail.getText().toString());
RequestParams params = new RequestParams();
params.put("user",aMap);

AsyncHttpClient client = new AsyncHttpClient();
client.post(url, params, n.....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.