在我的应用程序中,我必须使用 JSON req param 发送 POST 请求,我尝试使用 Postman Rest Client 创建请求,它工作正常,但无法使用下面的代码。
在作为原始数据发送的 Postman req 参数中,但我不确定如何使用 Volley 请求发送它。
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;
}
请求参数:
{
"nodeId": null,
"userId": null,
"mobileNumber": "993000008",
"emailId": "[email protected]",
"userProfile": null,
"region": null,
"countryCode": "91",
"password": "pass@123",
"places": [],
"trustedNetwork": [],
"profilePic": null,
"fullName": null,
"longitude": 0.0,
"latitude": 0.0
}