需求是这样
我们需要发送一个post请求向服务器要参数。要求是发送的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); }