问题描述:

I want to make a server POST request, sending a JSON payload, and recieving a string response in return. I've looked everywhere and there doesn't seem to be an easy way to do this using the Volley library, without implementing a custom request (which I am completely lost when trying to implement it). What is the `correct' way to perform this POST request?

网友答案:
    final JSONObject body = new JSONObject();
    try {
        // Todo: populate JSON body
    } catch (JSONException e) {
        e.printStackTrace();
    }

    StringRequest stringRequest = new StringRequest(Request.Method.POST, "http://...",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }
    ) {
        @Override
        public byte[] getBody() throws AuthFailureError {
            return body.toString().getBytes();
        }

        @Override
        public String getBodyContentType() {
            return "application/json";
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
网友答案:

Volley have JsonObjectRequest and JsonArrayRequest.

 /**
     * Making json object request
     * */
    public void makeJsonObjReq(String url) {

        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
                url,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d(TAG, response.toString());
//                      msgResponse.setText(response.toString());

                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());

            }
        }) {

            /**
             * Passing some request headers
             * */
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("Content-Type", "application/json");
                return headers;
            }

            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("name", "[email protected]");
                return params;
            }

        };

        // Adding request to request queue
        ApplicationController.getInstance().addToRequestQueue(jsonObjReq,
                tag_json_obj);

        // Cancelling request
        // ApplicationController.getInstance().getRequestQueue().cancelAll(tag_json_obj);
    }



 /**
     * Making json array request
     * */
    public void makeJsonArryReq(String url, final int _type) {

        JsonArrayRequest req = new JsonArrayRequest(url,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());
                        apiTaskListener.onTaskComplete("error",_type);


                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                apiTaskListener.onTaskComplete("error",_type);

            }
        });

        // Adding request to request queue
        ApplicationController.getInstance().addToRequestQueue(req,
                tag_json_arry);

        // Cancelling request
        // ApplicationController.getInstance().getRequestQueue().cancelAll(tag_json_arry);
    }
相关阅读:
Top