How to send java string array to php? using volley (Android)

I am trying to send following data to my php but program crashes if I put more than one s1 variable.

Java code:

//my java code goes here @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> params = new HashMap<>(); params.put("s1",max[0]); params.put("s1",max[1]); params.put("s1",max[3]); return params; }

PHP code:

//my php code goes here <?php $con = mysqli_connect('localhost','myuser','mypass'); mysqli_select_db($con,'mydb'); $checked = $_POST["s1"]; $SQL = "INSERT INTO TABLE VALUES ('$checked[0]','$checked[1]','$checked[2]')"; mysqli_query($con,$SQL); ?>


Map can only store one key and one value, duplicate keys are not allowed so in short you are just sending a single value and trying to fetch multiple values using index (which does not exists) hence the exception

Solution : Either use different keys and fetch values using those keys on server or send the whole array

To send whole array , simply Create JSONObject or JSONArray request instead of String


There is my example:

public void todo(String value, String value2) { StringRequest stringRequest = new StringRequest(Request.Method.POST, "https://adress.com/script.php", new Response.Listener<String>() { @Override public void onResponse(String response) { //response Log.d("Response", response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { error.printStackTrace(); } } ){ @Override protected Map<String, String> getParams() { Map<String, String> params = new HashMap<String, String>(); params.put("s1", value1); params.put("s2", value2); return params; } }; MySingleton.getInstance(this).addToRequestque(stringRequest); }

MySingleton.java :

public class MySingleton { private static MySingleton mInstance; private RequestQueue requestQueue; private static Context mCtx; private MySingleton(Context context) { mCtx = context; requestQueue = getRequestQueue(); } public RequestQueue getRequestQueue() { if(requestQueue==null) { requestQueue = Volley.newRequestQueue(mCtx.getApplicationContext()); } return requestQueue; } public static synchronized MySingleton getInstance(Context context) { if (mInstance==null) { mInstance = new MySingleton(context); } return mInstance; } public <T>void addToRequestque(Request<T> request) { requestQueue.add(request); } }


I assume this is generating a query string like


This will result in each value of s1 overwriting the last and s1 will contain 'C', not an array.

If you want s1 to be an array you need to use s1[] as the label, so it will generate a query string like


So then your Java would be:

params.put("s1[]",max[0]); params.put("s1[]",max[1]); params.put("s1[]",max[3]);

BUT, and this is REALLY IMPORTANT, you are not escaping the posted values before you put them opening yourself up to SQL injection attacks if your PHP page is in any way open to the public (and even if it isn't you should still guard against this) Have a look at http://php.net/manual/en/mysqli.real-escape-string.php


  • How to Cache Images Dynamically-Generated with PHP?
  • passing parameter to server in ExtJs
  • Merge list of objects into consistent list based on common matching attribute in Python
  • Read a file in “chunks” using PHP
  • Insert records if not exist SQL Server 2005
  • Leaflet z-index
  • retrieving data from url in iphone
  • auth.provider is not set to 'password' when user signs-in with email and password
  • Unique Permutations - with exceptions
  • How can I extend PHP DOMElement?
  • Julia: How to give multiple workers access to functions that are 'include(…)' into a modul
  • Cloud Code function running twice
  • Find group of records that match multiple values
  • JPA flush vs commit
  • 550 Access denied - Invalid HELO name
  • How do I signal completion of my dataflow?
  • Convert Type Decimal to Hex (string) in .NET 3.5
  • Array with custom indexes in Ionic2
  • Parsing a CSV string while ignoring commas inside the individual columns
  • Avoid links criss cross / overlap in d3.js using force layout
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Android screen density dpi vs ppi
  • PHP - How to update data to MySQL when click a radio button
  • Bug in WPF DataGrid
  • Check if a string to interpolate provides expected placeholders
  • Join two tables and save into third-sql
  • How to handle AllServersUnavailable Exception
  • htaccess rewriting URLs with multiple forward slashes
  • Javascript + PHP Encryption with pidCrypt
  • Redux, normalised entities and lodash merge
  • Do create extension work in single-user mode in postgres?
  • Delete MySQLi record without showing the id in the URL
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Unanticipated behavior
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Comma separated Values
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Trying to get generic when generic is not available
  • java string with new operator and a literal