簡體   English   中英

如何發送java字符串數組到PHP? 使用凌空(Android)

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

我試圖將以下數據發送到我的php,但是如果我放置多個s1變量,程序將崩潰。

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代碼:

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

地圖只能存儲一個鍵和一個值,不允許重復的鍵,因此簡而言之,您只是發送一個值,並嘗試使用索引(不存在)來獲取多個值,因此例外

解決方案:要么使用不同的鍵,然后使用服務器上的那些鍵獲取值,要么發送整個數組

要發送整個數組,只需創建JSONObjectJSONArray請求而不是String

有我的例子:

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

我假設這正在生成查詢字符串,例如

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

這將導致s1的每個值覆蓋最后一個值,並且s1將包含“ C”,而不是數組。

如果希望s1為數組,則需要使用s1 []作為標簽,因此它將生成一個查詢字符串,例如

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

因此,您的Java將是:

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

但是,這確實很重要,如果您的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