简体   繁体   English

如何发送java字符串数组到PHP? 使用凌空(Android)

[英]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. 我试图将以下数据发送到我的php,但是如果我放置多个s1变量,程序将崩溃。

Java code: Java代码:

    //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: PHP代码:

    //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 要发送整个数组,只需创建JSONObjectJSONArray请求而不是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 : 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 我假设这正在生成查询字符串,例如

?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. 这将导致s1的每个值覆盖最后一个值,并且s1将包含“ C”,而不是数组。

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为数组,则需要使用s1 []作为标签,因此它将生成一个查询字符串,例如

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

So then your Java would be: 因此,您的Java将是:

  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 但是,这确实很重要,如果您的PHP页面以任何方式向公众开放,则在您将发布的值开放给SQL注入攻击之前,您不要转义它们(即使不是,您仍然应该警惕)反对)看看http://php.net/manual/en/mysqli.real-escape-string.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM