How to send json array as post request in volley?
I used below code to post JSONArray to volley. You have to use JsonArrayRequest and pass the JSON Array directly without adding it to any JSONObject. Also keep in mind to Override the "parseNetworkResponse" method to convert the response to JSONArray again as The ResponseListner for JsonArrayRequest expects a type of JSONArray
String URL = "www.myposturl.com/data";
RequestQueue queue = Volley.newRequestQueue(this);
//Create json array for filter
JSONArray array = new JSONArray();
//Create json objects for two filter Ids
JSONObject jsonParam = new JSONObject();
JSONObject jsonParam1 = new JSONObject();
try {
//Add string params
jsonParam.put("NAME", "XXXXXXXXXXXXXX");
jsonParam.put("USERNAME", "XXXXXXXXXXXXXX");
jsonParam.put("PASSWORD", "XXXXXXXXXXXX");
jsonParam1.put("NAME", "XXXXXXXXXXXXXX");
jsonParam1.put("USERNAME", "XXXXXXXXXXXXXX");
jsonParam1.put("PASSWORD", "XXXXXXXXXXXX");
} catch (JSONException e) {
e.printStackTrace();
}
array.put(jsonParam);
array.put(jsonParam1);
JsonArrayRequest request_json = new JsonArrayRequest(Request.Method.POST, URL, array,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//Get Final response
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
VolleyLog.e("Error: ", volleyError.getMessage());
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
// Add headers
return headers;
}
//Important part to convert response to JSON Array Again
@Override
protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
String responseString;
JSONArray array = new JSONArray();
if (response != null) {
try {
responseString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
JSONObject obj = new JSONObject(responseString);
(array).put(obj);
} catch (Exception ex) {
}
}
//return array;
return Response.success(array, HttpHeaderParser.parseCacheHeaders(response));
}
};
queue.add(request_json);
If you are having a problem in calling the API then 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.
Implementation will be similar for JSONArray. Instead of JsonObjectRequest use JsonArrayRequest and send jArray instead of jObject.
For creating json array just do a little tweak
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 are converting Json array to string