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 … Read more

Send POST request with JSON data using Volley

JsonObjectRequest actually accepts JSONObject as body. From this blog article, final String url = “some/url”; final JSONObject jsonBody = new JSONObject(“{\”type\”:\”example\”}”); new JsonObjectRequest(url, jsonBody, new Response.Listener<JSONObject>() { … }); Here is the source code and JavaDoc (@param jsonRequest): /** * Creates a new request. * @param method the HTTP method to use * @param url … Read more

How to send a “multipart/form-data” POST in Android with Volley

I might be wrong on this but I think you need to implement your own com.android.volley.toolbox.HttpStack for this because the default ones (HurlStack if version > Gingerbread or HttpClientStack) don’t deal with multipart/form-data. Edit: And indeed I was wrong. I was able to do it using MultipartEntity in Request like this: public class MultipartRequest extends … Read more

How to set custom header in Volley Request

The accepted answer with getParams() is for setting POST body data, but the question in the title asked how to set HTTP headers like User-Agent. As CommonsWare said, you override getHeaders(). Here’s some sample code which sets the User-Agent to ‘Nintendo Gameboy’ and Accept-Language to ‘fr’: public void requestWithSomeHttpHeaders() { RequestQueue queue = Volley.newRequestQueue(this); String … Read more

Working POST Multipart Request with Volley and without HttpEntity

I rewrite your code @RacZo and @BNK more modular and easy to use like VolleyMultipartRequest multipartRequest = new VolleyMultipartRequest(Request.Method.POST, url, new Response.Listener<NetworkResponse>() { @Override public void onResponse(NetworkResponse response) { String resultResponse = new String(response.data); // parse success output } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { error.printStackTrace(); } }) { @Override protected … Read more

Can I do a synchronous request with volley?

It looks like it is possible with Volley’s RequestFuture class. For example, to create a synchronous JSON HTTP GET request, you can do the following: RequestFuture<JSONObject> future = RequestFuture.newFuture(); JsonObjectRequest request = new JsonObjectRequest(URL, new JSONObject(), future, future); requestQueue.add(request); try { JSONObject response = future.get(); // this will block } catch (InterruptedException e) { // … Read more

java.lang.IllegalStateException: Fragment not attached to Activity

This error happens due to the combined effect of two factors: The HTTP request, when complete, invokes either onResponse() or onError() (which work on the main thread) without knowing whether the Activity is still in the foreground or not. If the Activity is gone (the user navigated elsewhere), getActivity() returns null. The Volley Response is … Read more