0

I am trying to pass a string array to a PHP script as POST data in an Android app but i have an issue.

this is my client side code:

ArrayList<NameValuePair> querySend = new ArrayList<NameValuePair>();
for (Stop s : stops) {
                querySend.add(new BasicNameValuePair("stop_data[]", s.getNumber()));
            }
HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://www.myURL/myPHPScript.php");
                httppost.setEntity(new UrlEncodedFormEntity(querySend));
                HttpResponse response = httpclient.execute(httppost);

and this is my PHP script code:

foreach($_GET['stop_data'] as $stop) {
  // do something
}

If i run my java code i will get this response error from php script:

Warning: Invalid argument supplied for foreach() in /web/htdocs/www.myURL/myPHPScript.php on line 6
null

otherwise if i will put the request on my browser manully in this way:

http://www.myURL/myPHPScript.php?stop_data[]=768&stop_data[]=1283

i will give the correct answer!

if i try the same code, but if i POST not an array but a single string passing and receiving stop_data variables without [] it works! I can't understand where is the mistake!! It is possible that the [] in BasicNameValuePair String name are doing some codification mistaken? I need to pass an array, how i have to do?

1
  • Why don't you create a JSON and send to PHP? Commented Nov 17, 2013 at 14:49

1 Answer 1

1

you use POST method and in php code you should use $_POST

try this

foreach($_POST['stop_data'] as $stop) {
  // do something
}

this way is using GET method

http://www.myURL/myPHPScript.php?stop_data[]=768&stop_data[]=1283
Sign up to request clarification or add additional context in comments.

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.