Studio Master
Studio Master

Reputation: 15

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);
    ?>

Upvotes: 0

Views: 479

Answers (3)

james-geldart
james-geldart

Reputation: 749

I assume this is generating a query string like

?s1=A&s1=B&s1=C

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

?s1[]=A&s1[]=B&s1[]=C

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

Upvotes: 0

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

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

Upvotes: 4

Peter Valek
Peter Valek

Reputation: 87

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);
    }
}

Upvotes: 0

Related Questions