【发布时间】:2016-02-23 08:21:52
【问题描述】:
在我的应用程序中,我必须使用 JSON req 参数发送 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": "sharma@gmail.com",
"userProfile": null,
"region": null,
"countryCode": "91",
"password": "pass@123",
"places": [],
"trustedNetwork": [],
"profilePic": null,
"fullName": null,
"longitude": 0.0,
"latitude": 0.0
}
【问题讨论】:
-
你能更准确地描述一下这个问题吗?您可以发布您向 Postman 提出的请求吗?
标签: android android-volley androidhttpclient