需求是这样

我们需要发送一个post请求向服务器要参数。要求是发送的post参数也要是json格式。

简单一点的是这样的:

volley用法之 以post方式发送 json 参数

如果要发送的是这样简单的json格式,我们可以简单的使用map来实现:

RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());

        Map<String, String> merchant = new HashMap<String, String>();
        merchant.put("id", "id");
        merchant.put("ncode", "ncode");
        merchant.put("tradingName", "tradingName");

        Log.d("map", map.toString());
        JSONObject jsonObject = new JSONObject(merchant);

        Log.e(TAG, "getdata: " + jsonObject.toString());

        JsonRequest<JSONObject> jsonRequest = new JsonObjectRequest(Request.Method.POST, "", jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d(TAG, "response -> " + response.toString());
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, error.getMessage(), error);
            }
        }) {

            @Override
            public Map<String, String> getHeaders() {
                HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("Accept", "application/json");
                headers.put("Content-Type", "application/json; charset=UTF-8");

                return headers;
            }
        };
        requestQueue.add(jsonRequest);


    }
View Code

相关文章:

  • 2022-12-23
  • 2022-01-22
  • 2022-12-23
  • 2022-02-07
  • 2021-12-25
  • 2021-08-25
  • 2022-01-18
  • 2022-12-23
猜你喜欢
  • 2021-12-16
  • 2022-12-23
  • 2022-12-23
  • 2021-08-13
  • 2018-10-24
  • 2022-01-07
相关资源
相似解决方案