Volley - Sending a POST request using JSONArrayRequest

They're probably going to add it later, but in the meanwhile you can add the wanted constructor yourself:

public JsonArrayRequest(int method, String url, JSONObject jsonRequest,
        Listener<JSONArray> listener, ErrorListener errorListener) {
    super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), 
        listener, errorListener);
}

This isn't tested, though I see no reason this shouldn't work since the implementation details are in the super class: JsonRequest.

Try it and see if it works.

EDIT:

I called it! It took them almost two years after I answered this but the Volley team added this constructor on March 19, 2015 to the repo. Guess what? This is the EXACT syntax.


I was lazy and did not build the Volley library myself (just used the .jar), hence have no source code...so in the anonymous new JSONArrayRequest I added these functions:

            // NO CONSTRUCTOR AVAILABLE FOR POST AND PARAMS FOR JSONARRAY!
            // overridden the necessary functions for this
            @Override
            public byte[] getBody() {
                try {
                    return paramsArray.toString().getBytes("utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            public int getMethod() {
                return Method.POST;
            }

This code will make what you want

    Volley.newRequestQueue(context).add(
            new JsonRequest<JSONArray>(Request.Method.POST, "url/", null,
                    new Response.Listener<JSONArray>() {
                        @Override
                        public void onResponse(JSONArray response) {

                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {

                        }
                    }) {
                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("param1", "one");
                    params.put("param2", "two");
                    return params;
                }

                @Override
                protected Response<JSONArray> parseNetworkResponse(
                        NetworkResponse response) {
                    try {
                        String jsonString = new String(response.data,
                                HttpHeaderParser
                                        .parseCharset(response.headers));
                        return Response.success(new JSONArray(jsonString),
                                HttpHeaderParser
                                        .parseCacheHeaders(response));
                    } catch (UnsupportedEncodingException e) {
                        return Response.error(new ParseError(e));
                    } catch (JSONException je) {
                        return Response.error(new ParseError(je));
                    }
                }
            });

The best and easy way to send parameter request and return custom response according to parameter using JSONarray Request is to add get paramater value in the URL itself.

String URL ="http://mentormentee.gear.host/android_api/Message.aspx?key="+keyvalue;

where keyvalue parameter value and add this URL in the JsonArrayRequest URL simple.

    JsonArrayRequest searchMsg= new JsonArrayRequest(URL, new Response.Listener<JSONArray>() {

        @Override
        public void onResponse(JSONArray response) {
            Log.d(TAG, response.toString());


            // Parsing json
            for (int i = 0; i < response.length(); i++) {
                try {

                    JSONObject obj = response.getJSONObject(i);
                    Message msg = new Message();
                    msg.setMessageThread(obj.getString("msgThread"));
                    msg.setUserName(obj.getString("Username"));
                    msg.setDate(obj.getString("msgDate"));

                    // adding movie to movies array
                    MessageList.add(msg);

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            // notifying list adapter about data changes
            // so that it renders the list view with updated data
            adapter.notifyDataSetChanged();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
           // hidePDialog();

        }
    });
    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(searchMsg);
}

May be your problem is solved but I hope this will be helpful for other users. What I did was, I created a new custom class by extending it.. here is the code.

public class CustomJsonRequest extends Request {

Map<String, String> params;       
private Response.Listener listener; 

public CustomJsonRequest(int requestMethod, String url, Map<String, String> params,
                      Response.Listener responseListener, Response.ErrorListener errorListener) {

    super(requestMethod, url, errorListener);
    this.params = params;
    this.listener = responseListener;
}

@Override
protected void deliverResponse(Object response) {
    listener.onResponse(response); 

}

@Override
public Map<String, String> getParams() throws AuthFailureError {
         return params;
}

@Override
protected Response parseNetworkResponse(NetworkResponse response) {
    try {
        String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
        return Response.success(new JSONObject(jsonString),
        HttpHeaderParser.parseCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
    } catch (JSONException je) {
        return Response.error(new ParseError(je));
    }
}

}

you can use this class instead of JsonArrayRequest or JSonObjectRequest. And this also solve the problem of php not being able to catch post parameter in $_POST