I need to send via volley a http request with both authentification header and Json object in body. Bu I did not found a request for this in volley.
I found GsonRequest and JsonObjectRequest. GsonRequest int method, String url, Class clazz, Map headers, Listener listener, ErrorListener errorListener, Gson useGson)
JsonObjectRequest (int method, java.lang.String url, JSONObject jsonRequest, Response.Listener listener, Response.ErrorListener errorListener)
Any idea what to do ?
In your Request class, override getHeaders()
to send custom Headers
To send parameters in request body you need to override either getParams() or getBody() method of the request classes
described here:
Asynchronous HTTP Requests in Android Using Volley
try this code
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> params = new HashMap<String, String>();
String creds = String.format("%s:%s","USERNAME","PASSWORD");
String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT);
params.put("Authorization", auth);
return params;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With