在齐发请求中发送表格数据

时间:2018-09-20 16:30:19

标签: android django-rest-framework android-volley form-data

邮递员的回复是这张图片:

This is the postman request out of which i a, getting the perfect response

这是我用来在发布请求中发送数据的代码。尽管我从中得到了400条响应代码。

            StringRequest stringRequest = new StringRequest(Request.Method.POST,
                    API.ADD_PAYMENT,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            progressDialog.dismiss();
                            onBackPressed();
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            progressDialog.dismiss();
                            Toast.makeText(AddPaymentActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }){
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> params = new HashMap<>();
                    params.put("amount", "123");
                    params.put("description", "Not Paid");
                    params.put("customer", "1");
                    return params;
                }

                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String, String> headers = new HashMap<>();
                    headers.put("Content-Type", "application/json; charset=UTF-8");
                    headers.put("Authorization", "token 0ee1248c5a84e8b1e36a8a15da48c0bb7580926c");
                    return headers;
                }
            };
            RequestQueue requestQueue = Volley.newRequestQueue(AddPaymentActivity.this);
            requestQueue.add(stringRequest);

1 个答案:

答案 0 :(得分:0)

首先,当您将JsonObjectRequest用作服务器的交谈语言时,为什么不使用StringRequest而不是application/json?其次,您是否尝试过将参数传递给其他请求构造函数,如下所示?

Map<String, Object> params = new HashMap<>();
params.put("amount", 123);
params.put("description", "Not Paid");
params.put("customer", 1);
JsonObjectRequest jor = new JsonObjectRequest(Request.Method.POST, API.ADD_PAYMENT, new JSONObject(params), new Response.Listener<JSONObject>() {
...

传递标头是必不可少的,因此其余代码保持原样。

相关问题