代码之家  ›  专栏  ›  技术社区  ›  Sandeep Parish

添加带有截击请求和响应的自定义头为“data”:“此字段是必需的”

  •  -1
  • Sandeep Parish  · 技术社区  · 6 年前

    我刚开始使用定制请求截击请求这里是我用来发送定制头的代码的和平。标题必须是 headers.put(“Accept”,“application/json”); headers.put(“内容类型”,“应用程序/json”);

    邮递员行数据 row data in postman Image

    和 标题 Headers Image

    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"
        }
    

    如果我这边有什么问题,请告诉我。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Jatin Sahgal    6 年前
      StringRequest stringRequest = new StringRequest(Request.Method.POST,
            WebConstants.BASE_URL + method,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
    
                }
            }) {
    
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<>();
            headers.put("Accept", "application/json");
            headers.put("Content-Type", "application/json; charset=utf-8");
            return headers;
        }
        @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;
            }
    };
    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);