如何在齐射库中传递参数中的数组

时间:2022-08-27 20:18:19

Have servers template which recives an information

拥有收集信息的服务器模板

['param_id' => 12, 'count' => 4, 'car' => ['mazda 3 bk', '1.6', '2010']]

list

 list.add(ed_car.getText().toString());
                            list.add(ed_motor.getText().toString());
                            list.add(ed_year.getText().toString());

volley passing

@Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String, String> parameters = new HashMap<String, String>();
                        parameters.put("param_id", id_item);
                        parameters.put("count", spinner_count.getSelectedItem().toString());

                        for(int i=0; i < list.size(); i++)
                        {
                            parameters.put("car",list.get(i));///////???
                        }
                        return parameters;
                    }

How correctly pass a list according to their places in the receiving template

如何根据接收模板中的位置正确传递列表

2 个解决方案

#1


2  

Use this:

for (int i = 0; i < list.size(); i++)
{
    parameters.put("car["+i+"]", list.get(i));
}

#2


1  

parameters.put("car",list.get(i)); will over write the previous values with new one

parameters.put( “汽车”,list.get(I));将用新的值重写先前的值

Solution :

   ['mazda 3 bk', '1.6', '2010']
// ^                           ^ json array

so create json array and use it's string representation

所以创建json数组并使用它的字符串表示

Map<String, String> parameters = new HashMap<String, String>();
parameters.put("param_id", id_item);
parameters.put("count", spinner_count.getSelectedItem().toString());

JSONArray car = new JSONArray();
for(int i=0; i < list.size(); i++)
{
    car.put(list.get(i));
    // create array and add items into that 
}
parameters.put("car",car.toString());
return parameters;

Take look at toString() which will return the desired format

看一下toString(),它将返回所需的格式

#1


2  

Use this:

for (int i = 0; i < list.size(); i++)
{
    parameters.put("car["+i+"]", list.get(i));
}

#2


1  

parameters.put("car",list.get(i)); will over write the previous values with new one

parameters.put( “汽车”,list.get(I));将用新的值重写先前的值

Solution :

   ['mazda 3 bk', '1.6', '2010']
// ^                           ^ json array

so create json array and use it's string representation

所以创建json数组并使用它的字符串表示

Map<String, String> parameters = new HashMap<String, String>();
parameters.put("param_id", id_item);
parameters.put("count", spinner_count.getSelectedItem().toString());

JSONArray car = new JSONArray();
for(int i=0; i < list.size(); i++)
{
    car.put(list.get(i));
    // create array and add items into that 
}
parameters.put("car",car.toString());
return parameters;

Take look at toString() which will return the desired format

看一下toString(),它将返回所需的格式