Send A Json Array Post Request With Android Using Volley
Solution 1:
Try this:
JSONArray jsonArray=newJSONArray();
JSONObject jsonObj=newJSONObject();
try {
jsonObj.put("filterName","EmailId");
jsonObj.put("FilterValue",// user email);
} catch (JSONException e) {
e.printStackTrace();
}
jsonArray.put(jsonObj);
Add it to your params like this:
postParam.put("FilterData",jsonArray);
Solution 2:
Following three steps should make it work for old Volley libraries lacking this suport.
Prepare payload and post:
JSONArray payloadItems = new JSONArray();
JSONObject payloadItem1=new JSONObject(); //set properties on item1 payloadItem1.put('prop1',"val11");
payloadItems.put(payloadItem1);
JSONObject payloadItem2=new JSONObject(); //set properties on item1 payloadItem2.put('prop1',"val12");
payloadItems.put(payloadItem1);
JsonArrayRequest request;
request = new JsonArrayRequest(Request.Method.POST,url,payloadItems, new Response.Listener() { @SuppressWarnings("unchecked") @Override public void onResponse(JSONArray response) { //your logic to handle response } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { //your logic to handle error } }) {
publicMap<String, String> getHeaders() throws AuthFailureError { Map<String,String> params = newHashMap<String, String>(); /* This is very important to pass along */ params.put("Content-Type","application/json"); //other headers if anyreturn params; } };
[If Needed] Add this constructor in Class- JsonArrayRequest in Volley package if not already there
publicJsonArrayRequest(int method, String url, JSONArray jsonArray, Listener<JSONArray> listener, ErrorListener errorListener) { super(method, url, (jsonArray == null) ? null : jsonArray.toString(), listener, errorListener);
}
[If Needed] Override this method if not yet implemented to support JSONArray response from server.
@OverrideprotectedResponse<JSONArray> parseNetworkResponse(NetworkResponse response) { String responseString; JSONArray array = newJSONArray(); if (response != null) { try { responseString = newString(response.data, HttpHeaderParser.parseCharset(response.headers)); JSONObject obj = newJSONObject(responseString); (array).put(obj); } catch (Exception ex) { } } //return array;returnResponse.success(array, HttpHeaderParser.parseCacheHeaders(response)); }
Post a Comment for "Send A Json Array Post Request With Android Using Volley"