Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将java字符串数组发送到php?使用截击(Android)_Php_Android_Arrays - Fatal编程技术网

如何将java字符串数组发送到php?使用截击(Android)

如何将java字符串数组发送到php?使用截击(Android),php,android,arrays,Php,Android,Arrays,我试图将以下数据发送到我的php,但如果我放入多个s1变量,程序就会崩溃 Java代码: //my java code goes here @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> params = new HashMap<>(); pa

我试图将以下数据发送到我的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;
    }
//我的java代码在这里
@凌驾
受保护的映射getParams()引发AuthFailureError{
Map params=新的HashMap();
参数put(“s1”,最大值[0]);
参数put(“s1”,最大值[1]);
参数put(“s1”,最大值[3]);
返回参数;
}
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代码在这里

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

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


要发送整个数组,只需创建
JSONObject
JSONArray
请求,而不是字符串映射只能存储一个键和一个值,不允许重复键,因此简而言之,您只需发送一个值,并尝试使用索引(不存在)获取多个值,因此出现异常

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

要发送整个数组,只需创建
JSONObject
JSONArray
请求,而不是字符串,下面是我的示例:

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);
    }
public void todo(字符串值,字符串值2)
{
StringRequest StringRequest=新的StringRequest(Request.Method.POST)https://adress.com/script.php,新的响应。侦听器(){
@凌驾
公共void onResponse(字符串响应){
//回应
Log.d(“响应”,响应);
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
错误。printStackTrace();
}
}
){
@凌驾
受保护的映射getParams()
{
Map params=新的HashMap();
参数put(“s1”,值1);
参数put(“s2”,值2);
返回参数;
}
};
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);
    }
}
公共类MySingleton{
私有静态MySingleton-mInstance;
私有请求队列请求队列;
私有静态上下文mCtx;
private MySingleton(上下文)
{
mCtx=上下文;
requestQueue=getRequestQueue();
}
公共请求队列getRequestQueue()
{
if(requestQueue==null)
{
requestQueue=Volley.newRequestQueue(mCtx.getApplicationContext());
}
返回请求队列;
}
公共静态同步MySingleton getInstance(上下文)
{
if(minInstance==null)
{
MinInstance=新的MySingleton(上下文);
}
回报率;
}
public void addToRequestque(请求)
{
添加(请求);
}
}
下面是我的例子:

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);
    }
public void todo(字符串值,字符串值2)
{
StringRequest StringRequest=新的StringRequest(Request.Method.POST)https://adress.com/script.php,新的响应。侦听器(){
@凌驾
公共void onResponse(字符串响应){
//回应
Log.d(“响应”,响应);
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
错误。printStackTrace();
}
}
){
@凌驾
受保护的映射getParams()
{
Map params=新的HashMap();
参数put(“s1”,值1);
参数put(“s2”,值2);
返回参数;
}
};
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);
    }
}
公共类MySingleton{
私有静态MySingleton-mInstance;
私有请求队列请求队列;
私有静态上下文mCtx;
private MySingleton(上下文)
{
mCtx=上下文;
requestQueue=getRequestQueue();
}
公共请求队列getRequestQueue()
{
if(requestQueue==null)
{
requestQueue=Volley.newRequestQueue(mCtx.getApplicationContext());
}
返回请求队列;
}
公共静态同步MySingleton getInstance(上下文)
{
if(minInstance==null)
{
MinInstance=新的MySingleton(上下文);
}
回报率;
}
public void addToRequestque(请求)
{
添加(请求);
}
}

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

?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页面以任何方式向公众开放(即使不是这样,您也应该对此加以防范),那么在发布这些值之前,您不能逃避这些值。请看一下

我想这会生成一个类似

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

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

如果希望s1成为数组,则需要使用s1[]作为标签,这样它将生成一个查询字符串