Volley - POST/GET parameters Volley - POST/GET parameters android android

Volley - POST/GET parameters


For the GET parameters there are two alternatives:

First: As suggested in a comment bellow the question you can just use String and replace the parameters placeholders with their values like:

String uri = String.format("http://somesite.com/some_endpoint.php?param1=%1$s&param2=%2$s",                           num1,                           num2);StringRequest myReq = new StringRequest(Method.GET,                                        uri,                                        createMyReqSuccessListener(),                                        createMyReqErrorListener());queue.add(myReq);

where num1 and num2 are String variables that contain your values.

Second: If you are using newer external HttpClient (4.2.x for example) you can use URIBuilder to build your Uri. Advantage is that if your uri string already has parameters in it it will be easier to pass it to the URIBuilder and then use ub.setQuery(URLEncodedUtils.format(getGetParams(), "UTF-8")); to add your additional parameters. That way you will not bother to check if "?" is already added to the uri or to miss some & thus eliminating a source for potential errors.

For the POST parameters probably sometimes will be easier than the accepted answer to do it like:

StringRequest myReq = new StringRequest(Method.POST,                                        "http://somesite.com/some_endpoint.php",                                        createMyReqSuccessListener(),                                        createMyReqErrorListener()) {    protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {        Map<String, String> params = new HashMap<String, String>();        params.put("param1", num1);        params.put("param2", num2);        return params;    };};queue.add(myReq);

e.g. to just override the getParams() method.

You can find a working example (along with many other basic Volley examples) in the Andorid Volley Examples project.


In your Request class (that extends Request), override the getParams() method. You would do the same for headers, just override getHeaders().

If you look at PostWithBody class in TestRequest.java in Volley tests, you'll find an example.It goes something like this

public class LoginRequest extends Request<String> {    // ... other methods go here    private Map<String, String> mParams;    public LoginRequest(String param1, String param2, Listener<String> listener, ErrorListener errorListener) {        super(Method.POST, "http://test.url", errorListener);        mListener = listener;        mParams = new HashMap<String, String>();        mParams.put("paramOne", param1);        mParams.put("paramTwo", param2);    }    @Override    public Map<String, String> getParams() {        return mParams;    }}

Evan Charlton was kind enough to make a quick example project to show us how to use volley.https://github.com/evancharlton/folly/


CustomRequest is a way to solve the Volley's JSONObjectRequest can't post parameters like the StringRequest

here is the helper class which allow to add params:

    import java.io.UnsupportedEncodingException;    import java.util.Map;        import org.json.JSONException;    import org.json.JSONObject;        import com.android.volley.NetworkResponse;    import com.android.volley.ParseError;    import com.android.volley.Request;    import com.android.volley.Response;    import com.android.volley.Response.ErrorListener;    import com.android.volley.Response.Listener;    import com.android.volley.toolbox.HttpHeaderParser;    public class CustomRequest extends Request<JSONObject> {    private Listener<JSONObject> listener;    private Map<String, String> params;    public CustomRequest(String url, Map<String, String> params,            Listener<JSONObject> reponseListener, ErrorListener errorListener) {        super(Method.GET, url, errorListener);        this.listener = reponseListener;        this.params = params;    }    public CustomRequest(int method, String url, Map<String, String> params,            Listener<JSONObject> reponseListener, ErrorListener errorListener) {        super(method, url, errorListener);        this.listener = reponseListener;        this.params = params;    }    protected Map<String, String> getParams()            throws com.android.volley.AuthFailureError {        return params;    };    @Override    protected Response<JSONObject> 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));        }    }    @Override    protected void deliverResponse(JSONObject response) {        // TODO Auto-generated method stub        listener.onResponse(response);    }}

thanks to Greenchiu