I did it the following way (the content-type of my request-body was application/x-www-form-urlencoded):
I've commented at appropriate places in the code.
/**
* @param url - endpoint url of the call
* @param requestBody - I'm receiving it in json, without any encoding from respective activities.
* @param listener - StringRequestListener is an Interface I created to handle the results in respective activities
* @param activity - just for the context, skippable.
* @param header - This contains my x-api-key
*/
public void makePostRequest2(String url, final JSONObject requestBody, final StringRequestListener listener,
Activity activity, final Map<String,String> header) {
RequestQueue queue = VolleySingleton.getInstance().getRequestQueue();
/**
* You can skip this network testing.
*/
if(!NetworkTester.isNetworkAvailable()) {
Toast.makeText(MyApplication.getAppContext(),"Network error",Toast.LENGTH_SHORT).show();
return;
}
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
listener.onResponse(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
listener.onError(error);
}
}) {
/**
* Setting the body-content type is the most important part.
* @return
* You don't have to write this method if your body content-type is application/x-www-form-urlencoded and encoding is charset=UTF-8
* Because the base method is does the exact same thing.
*/
// @Override
// public String getBodyContentType() {
// return "application/x-www-form-urlencoded; charset=UTF-8";
// }
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return header;
}
/**
* I have copied the style of this method from its original method from com.Android.Volley.Request
* @return
* @throws AuthFailureError
*/
@Override
public byte[] getBody() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
try {
params.put("grant_type","password");
params.put("username",requestBody.getString("username"));
params.put("password",requestBody.getString("password"));
} catch (JSONException e) {
e.printStackTrace();
}
//yeah, I copied this from the base method.
if (params != null && params.size() > 0) {
return encodeParameters(params, getParamsEncoding());
}
return null;
}
};
queue.add(stringRequest);
}
/**
* This method was private in the com.Android.Volley.Request class. I had to copy it here so as to encode my paramters.
* @param params
* @param paramsEncoding
* @return
*/
private byte[] encodeParameters(Map<String, String> params, String paramsEncoding) {
StringBuilder encodedParams = new StringBuilder();
try {
for (Map.Entry<String, String> entry : params.entrySet()) {
encodedParams.append(URLEncoder.encode(entry.getKey(), paramsEncoding));
encodedParams.append('=');
encodedParams.append(URLEncoder.encode(entry.getValue(), paramsEncoding));
encodedParams.append('&');
}
return encodedParams.toString().getBytes(paramsEncoding);
} catch (UnsupportedEncodingException uee) {
throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee);
}
}