How to send json array as email request in salvo?

I use volley to parse json. I want to send some data using POST to the server. I'm trying to send. Now can someone tell me how can I send the filter to the server?

Below is my snippet code. I also tried Hashmap and Jsonobject . but getting this error.

Error:

 org.json.JSONException: Value at Data of type java.lang.String cannot be converted to JSONObject 

Format

 { "typeName": "MANUFACTURER", "typeId": 22, "cityId": 308, "sortBy": "productname", "sortOrder": "desc", "filter":[ { "filterId":101, "typeName":"CAT_ID", "filterId":102, "typeName":"CAT_ID" } ] } 

For patches with code verification

https://pastebin.com/u5qD8e2j

+5
source share
4 answers

If you are having trouble calling the API, this will help you.

 RequestQueue queue = Volley.newRequestQueue(this); JsonObjectRequest jobReq = new JsonObjectRequest(Request.Method.POST, url, jObject, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject jsonObject) { } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { } }); queue.add(jobReq); 

where jObject is the JSON data you want to send to the server.

The implementation will be similar for JSONArray. Instead of JsonObjectRequest use JsonArrayRequest and send jArray instead of jObject.

To create a json array, just tweak it a bit

 JSONArray array=new JSONArray(); for(int i=0;i<filter_items.size();i++){ JSONObject obj=new JSONObject(); try { obj.put("filterId",filter_items.get(i)); obj.put("typeName","CAT_ID"); } catch (JSONException e) { e.printStackTrace(); } array.put(obj); } 

And finally add json array as below

 jsonParams.put("filter",array); 

In your case, you convert a Json array to a string

+4
source

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 a json object, check this link

Android JSONObject: add Array to put method

EDIT

In case of more data, it is better to use a Gson converter

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

Also to create pojo classes use this

http://www.jsonschema2pojo.org/

+2
source
 { "typeName": "MANUFACTURER", "typeId": 22, "cityId": 308, "sortBy": "productname", "sortOrder": "desc", "filter":[ { "filterId":101, "typeName":"CAT_ID", } { "filterId":102, "typeName":"CAT_ID" } ] } JSONObject object=new JSONObject(); object.put("typeName",""); object.put("typeId",""); object.put("cityId",""); object.put("sortBy",""); object.put("sortOrder",""); JSONArray array=new JSONArray(); JSONObject obj=new JSONObject(); obj.put("filterId",""); obj.put("typeName",""); array.put(obj); object.put("filter",obj.toString()); 

pass a JSONObject for the request. use https://www.androidhive.info/2014/09/android-json-parsing-using-volley/

+1
source

Hi Volley does not support JsonArray request, it is better to use some other libraries ...

0
source

Source: https://habr.com/ru/post/1272124/


All Articles