【问题标题】:How to send java string array to php? using volley (Android)如何将java字符串数组发送到php?使用凌空(Android)
【发布时间】:2018-08-16 22:17:52
【问题描述】:

我正在尝试将以下数据发送到我的 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);
    ?>

【问题讨论】:

  • 尝试在 PHP 中输​​出 $checked 并查看它究竟得到了什么。如果仍然不确定,请将其放在这里,这将有助于回答您的问题。另外:您的 PHP 代码容易受到 SQL 注入的攻击。查找准备好的语句。

标签: php android arrays


【解决方案1】:

Map 只能存储一个键和一个值,不允许重复键,所以简而言之,您只是发送一个值并尝试使用索引(不存在)获取多个值,因此异常

解决方案:要么使用不同的键并使用服务器上的这些键获取值,要么发送整个数组

要发送整个数组,只需创建 JSONObjectJSONArray 请求而不是字符串

【讨论】:

  • 整个数组在这里不起作用——这些是 GET http 参数。没有内置的方式来发送数组。他可以使它们成为一个逗号分隔字符串的一部分并在服务器端解析它,但要发送一个数组,他需要将它作为一个主体发送。
  • 是的,你是对的,我已经修改了答案,谢谢先生
  • 是的,你是 ryt 我如何将 max[0]、max[1] 和 max[2] 发送到我的 php 的 s1 变量
  • @StudioMaster 你需要创建一个jsonarray请求和this is how you can send your array as jsonarray
【解决方案2】:

这是我的例子:

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

【讨论】:

    【解决方案3】:

    我假设这是生成一个查询字符串,如

    ?s1=A&amp;s1=B&amp;s1=C

    这将导致 s1 的每个值都覆盖最后一个,并且 s1 将包含“C”,而不是数组。

    如果你想让 s1 成为一个数组,你需要使用 s1[] 作为标签,所以它会生成一个类似的查询字符串

    ?s1[]=A&amp;s1[]=B&amp;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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多