如何在Android中使用Volley方法POST数组,此处最后一个索引值在数据库中发布

时间:2018-06-10 15:14:27

标签: java android arrays android-volley

这是Volley功能。我使用了数组,并且有多个值,例如arra [] = {1,2,4,5}我希望使用parameter.put(“Data”,array [])发送此数组,这将如何发生?

public void sendData(){
        requestQueue = Volley.newRequestQueue(getApplicationContext());

        StringRequest request = new StringRequest(Request.Method.POST, insertUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                System.out.println(response.toString());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {

                Map<String,String> parameters  = new HashMap<String, String>();
                    for(int i =0;i<array.length;i++) {
                        String A= array[i].toString();
                        parameters.put("Generated",A);
                    }

                return parameters;
            }
        };
        requestQueue.add(request);
    }

1 个答案:

答案 0 :(得分:0)

您可能需要将gson转换为json。

添加您的图书馆。

compile 'com.squareup.retrofit2:converter-gson:2.2.0'

然后

Gson gson = new GsonBuilder()
            .excludeFieldsWithoutExposeAnnotation()
            .setDateFormat("yyyy-MM-dd HH:mm:ss").create();

parameters.put("Generated",gson.toJson(array));