Я только что начал с залпового запроса с пользовательским запросом. Здесь приведен код, который я использую для отправки пользовательского заголовка.заголовки должны быть headers.put («Принять», «Приложение / JSON»);headers.put ("Content-Type", "application / json");
Когда я нажимаю на этот URL с почтальоном, он работает нормально.
данные строкив почтальоне ![row data in postman Image](https://i.stack.imgur.com/dDP8f.png)
и заголовках ![Headers Image](https://i.stack.imgur.com/DCVdu.png)
StringRequest stringRequest = new StringRequest(Request.Method.POST,
WebConstants.BASE_URL + method,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (dialog != null)
dialog.dismiss();
if (response != null) {
dialog.dismiss();
Log.e("res", response);
// eventHandler.onResponse("" + method, response);
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (dialog != null)
dialog.dismiss();
eventHandler.onResponse("" + method, "");
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap headers = new HashMap();
headers.put("Accept", "application/json");
headers.put("Content-Type", "application/json");
return headers != null ? headers : super.getHeaders();
}
@Override
public byte[] getBody() {
String str = "{\"firstname\":\"Test\", \"lastname\":\"User\", \"gender\":\"male\", \"country\":\"US\", \"zipcode\":\"H1A NFG\", \"email\":\"testuseef434532r@mail.com\", \"password\":\"123456\", \"referral_code\":\"\", \"requested_plan\":\"\"}";
return str.getBytes();
}
/* Tried with this
@Override
public byte[] getBody() {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("firstname","Sandeep");
jsonObject.put("lastname","Parish");
jsonObject.put("gender","M");
jsonObject.put("country","US");
jsonObject.put("zipcode","H1A NFG");
jsonObject.put("email","usermail@localmail.com");
jsonObject.put("password","123456");
jsonObject.put("referral_code","123456");
jsonObject.put("requested_plan","123456");
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject.toString().getBytes();
}
//Tried with map
@Override
protected Map<String, String> getParams() {
Map<String, String> map = new HashMap<>();
map.put("firstname", "sandeep");
map.put("lastname", "sada");
map.put("gender", "d");
map.put("country", "sdfasiio");
map.put("zipcode", "test");
map.put("email", "testmail@mail.com");
map.put("password", "0");
map.put("referral_code", "0");
map.put("requested_plan", "0");
Log.e("Map", map.toString());
return map;
}
*/
@Override
public String getBodyContentType() {
return "application/json";
}
};
stringRequest.setShouldCache(false);
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue requestQueue = Volley.newRequestQueue(context);
requestQueue.add(stringRequest);
, пожалуйста, помогите мне выяснить, в чем проблемаэтот код я получаю ответ с этим кодом.
E/res: {
"success": false,
"data": "This field is required"
}
если какая-либо проблема на моей стороне, пожалуйста, дайте мне знать.