Send post request using Volley and receive in PHP

Had a lot of problems myself, try this ! 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> responseListener, ErrorListener errorListener) { super(Method.GET, url, errorListener); this.listener = responseListener; this.params = params; } public CustomRequest(int method, String url,Map<String, String> params, Listener<JSONObject> reponseListener, ErrorListener errorListener) { super(method, … Read more

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

Android set up Volley to use from cache

Please note that if the web service supports caching output, you don’t have to use CacheRequest below, because Volley will automatically cache. For your issue, I use some codes inside parseCacheHeaders (and refering to @oleksandr_yefremov‘s codes). The following code I have tested. Of course, can use for JsonArrayRequest also. Hope this help! JsonObjectRequest jsonObjectRequest = … Read more

Android Volley – BasicNetwork.performRequest: Unexpected response code 400

One way of doing this without changing Volley‘s source code is to check for the response data in the VolleyError and parse it your self. As of f605da3 commit, Volley throws a ServerError exception that contains the raw network response. So you can do something similar to this in your error listener: /* import com.android.volley.toolbox.HttpHeaderParser; … Read more

How can I return value from function onResponse of Volley?

You want to use callback interfaces like so: public void getString(final VolleyCallback callback) { StringRequest strReq = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { @Override public void onResponse(String response) { … // (optionally) some manipulation of the response callback.onSuccess(response); } }… }} Where the callback is defined as public interface VolleyCallback{ void onSuccess(String result); } Example … 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

How to send a POST request using volley with string body?

You can refer to the following code (of course you can customize to get more details of the network response): try { RequestQueue requestQueue = Volley.newRequestQueue(this); String URL = “http://…”; JSONObject jsonBody = new JSONObject(); jsonBody.put(“Title”, “Android Volley Demo”); jsonBody.put(“Author”, “BNK”); final String requestBody = jsonBody.toString(); StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() { … Read more

Change Volley timeout duration

See Request.setRetryPolicy() and the constructor for DefaultRetryPolicy, e.g. JsonObjectRequest myRequest = new JsonObjectRequest(Method.GET, url, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Log.d(TAG, response.toString()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.d(TAG, “Error: ” + error.getMessage()); } }); myRequest.setRetryPolicy(new DefaultRetryPolicy( MY_SOCKET_TIMEOUT_MS, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

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