简体   繁体   English

Android Volley Request with Body

[英]Android Volley Request with Body

Is it possible to send a simple text in the body of a StringRequest using DELETE-Method?是否可以使用 DELETE-Method 在 StringRequest 的正文中发送简单的文本?

I couldn't find any example where somebody put something in the body of a request... This is my request and I want to add "{'deviceid':'xyz'}" to the body (method is DELETE):我找不到任何某人在请求正文中放入某些内容的示例...这是我的请求,我想在正文中添加“{'deviceid':'xyz'}”(方法为 DELETE):

final StringRequest stringRequest = new StringRequest(method, url + "?token=" + token, new Response.Listener<String>() {
        @Override
        public void onResponse(String jsonResponse) {
            // do something
    }, new Response.ErrorListener() {
            // do something
        }
    }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("api-version", "1");

            return headers;
        }
    };

This because Volley doesn't send the Body for DELETE by default.这是因为默认情况下 Volley 不会发送用于 DELETE 的正文。 Only for POST, PUT and PATCH.仅用于 POST、PUT 和 PATCH。 Unfortunate to say the least不幸的是,至少可以说

There is a workaround for it listed here: Volley - how to send DELETE request parameters?这里列出了一个解决方法: Volley - how to send DELETE request parameters?

Try this:尝试这个:

public class StringJSONBodyReqest extends StringRequest {

    private static final String TAG = StringJSONBodyReqest.class.getName();
    private final String mContent;

    public StringJSONBodyReqest(int method, String url, String content, Response.Listener<String> listener, Response.ErrorListener errorListener) {
        super(method, url, listener, errorListener);
        mContent = content;
    }


    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        HashMap<String, String> headers = new HashMap<String, String>();
        headers.put("api-version", "1");

        return headers;
    }


    @Override
    public byte[] getBody() throws AuthFailureError {

        byte[] body = new byte[0];
        try {
            body = mContent.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            Log.e(TAG, "Unable to gets bytes from JSON", e.fillInStackTrace());
        }
        return body;
    }


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

mContent is your json String mContent是你的 json String

StringRequest stringRequest = new StringRequest(StringRequest.Method.PUT,
            BASE_URL + "/addItem",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.d(TAG, response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                //handle error
                }
            }) {
        @Override
        public byte[] getBody(){
            String jsonString = json to send;
            return jsonString.getBytes();
        }
        @Override
        public String getBodyContentType() {
            return "application/json";
        }
    };
    MyRequestQueue.getInstance().addRequest(stringRequest);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM