volley post客户端以json数据上传服务器

封装的volley请求类

public class VolleyUtil { private static RequestQueue requestQueue ; private static String host = Config. BASE_URL ; public static RequestQueue getRequestQueue(Context context) { if ( requestQueue == null) { requestQueue = Volley. newRequestQueue(context) ; } return requestQueue ; }

    public static void postJSON(Context context, String url, JSONObject jsonObject, Response.Listener listener, Response.ErrorListener errorListener) {
        getRequestQueue(context);
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, host+url, jsonObject, listener, errorListener) {

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

        };

        requestQueue.add(request);
    }

}


客户端的请求方式

Map, String> userMap = new HashMap<>();
userMap.put("password", pws);
userMap.put("email", name);

String url = "wms/user-authentication/login";

JSONObject jsonObject = new JSONObject(userMap);

VolleyUtil.postJSON(this, url, jsonObject, new Response.Listener() {
    @Override
    public void onResponse(Object o) {
        Log.e("111111", "response -> " + o.toString());
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError volleyError) {
        Log.e("111111", volleyError.getMessage(), volleyError);
    }
});



你可能感兴趣的:(volley post客户端以json数据上传服务器)