In my application I have to send POST request with JSON req param, I tried to create request with Postman Rest Client and it is working fine but not working with below code.
In Postman req parameter sent as raw data, But I am not sure how to send it with Volley request.
public Request getHTTPPostReqResponse(String URL, Class mClass, final Map<String, String> params, final String contentType, final String body) {
mResponseListener.requestStarted();
Request mRequest = new GsonRequest(Request.Method.POST, URL, mClass, new Response.Listener<Object>() {
@Override
public void onResponse(Object response) {
mResponseListener.requestCompleted(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mResponseListener.requestEndedWithError(error);
}
}) {
@Override
protected Map<String, String> getParams() {
return params;
}
@Override
public byte[] getBody() throws AuthFailureError {
if (TextUtils.isEmpty(body)){
return super.getBody();
}else {
return body.getBytes();
}
}
@Override
public String getBodyContentType() {
return contentType;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Content-Type", contentType);
return params;
}
};
mRequest.setRetryPolicy(new DefaultRetryPolicy(
MY_SOCKET_TIMEOUT_MS,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
return mRequest;
}
Request Parameter:
{
"nodeId": null,
"userId": null,
"mobileNumber": "993000008",
"emailId": "sharma@gmail.com",
"userProfile": null,
"region": null,
"countryCode": "91",
"password": "pass@123",
"places": [],
"trustedNetwork": [],
"profilePic": null,
"fullName": null,
"longitude": 0.0,
"latitude": 0.0
}
This is the working code, you can try. we can use JsonObjectRequest for raw data. Here i have just showing how to make requestObject that is added in queue
hope this isn't too late.
Have you tried a different type of request, like String or JsonObject? And a different syntax for the params?
e.g.
Also, have a look at this SO question. Hope any of this helps.