数组作为Volley POST请求参数 [英] Array as Volley POST request params

查看:170
本文介绍了数组作为Volley POST请求参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用数组作为参数进行齐发POST请求,例如我要POST

I am trying to make a volley POST request with a array as my parameter, for example I want to POST

{"types":[1,2,3]} 

我现在拥有的是一个字符串

what I have now is a string

{"types":"[1,2,3]"}

这是我提出齐射要求的方式:

This is how I made my volley request:

JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = new JSONObject();

    try{
        jsonObject.put("types", list);
        jsonArray.put(jsonObject);
        System.out.println(jsonObject);
    }catch(Exception e){

    }

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject,
            new Response.Listener<JSONObject>(){
                @Override
                public void onResponse(JSONObject response) {
                    Log.e("Response", response.toString());
                }
            },
            new Response.ErrorListener(){
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("Error.Response", error.toString());
                    String json = null;
                    NetworkResponse response = error.networkResponse;
                    if(response != null && response.data != null){
                        switch(response.statusCode){
                            case 400:

                                json = new String(response.data);
                                System.out.println(json);
                                break;
                        }
                    }
                }
            })
    {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<>();
            headers.put("Authorization", Token);
            return headers;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(jsonObjectRequest);

列表是声明为的数组列表:

List is an array list declared as:

List<Integer> list = new ArrayList<Integer>();

我假设jsonObject.put( types,list)会将我的数组变成一个列表,我应该如何解决这个问题?

I am assuming that jsonObject.put("types", list) will turn my array into a list, how should I solve this problem?

推荐答案

而不是 List ,尝试 JSONArray

JSONArray types=new JSONArray();
types.put(1);
types.put(2);
types.put(3);
jsonObject.put("types", types);
jsonArray.put(jsonObject);

这应该可以解决您的问题

This should fix your issue

这篇关于数组作为Volley POST请求参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆