How to send json array as post request in volley?

后端 未结 6 1442
孤城傲影
孤城傲影 2021-01-04 08:56

I am using volley for json parsing. I want to send some data using POST to server side. I am trying to send .Now can any one tell me that how can i send filter array to serv

6条回答
  •  南方客
    南方客 (楼主)
    2021-01-04 09:38

    Hope this helps you.

        //Create Main jSon object
        JSONObject jsonParams = new JSONObject();
    
        try {
            //Add string params
            jsonParams.put("typeName", "MANUFACTURER");
            jsonParams.put("typeId", "22");
            jsonParams.put("cityId", "308");
            jsonParams.put("sortBy", "productname");
            jsonParams.put("sortOrder", "desc");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //Create json array for filter
        JSONArray array=new JSONArray();
    
        //Create json objects for two filter Ids
        JSONObject jsonParam1 =new JSONObject();
        JSONObject jsonParam2 =new JSONObject();
    
        try {
    
            jsonParam1.put("filterId","101");
            jsonParam1.put("typeName","CAT_ID");
    
            jsonParam2.put("filterId","102");
            jsonParam2.put("typeName","CAT_ID");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    
        //Add the filter Id object to array
        array.put(jsonParam1);
        array.put(jsonParam2);
    
        //Add array to main json object
        try {
            jsonParams.put("filter",array);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    

    For more information on how to create json object check this link

    Android JSONObject : add Array to the put method

    EDIT:

    In case of more data it is better to use Gson convertor

    http://www.vogella.com/tutorials/JavaLibrary-Gson/article.html

    Also for creating pojo classes use this

    http://www.jsonschema2pojo.org/

提交回复
热议问题