【问题标题】:Android Volley Request with Body带有正文的 Android Volley 请求
【发布时间】:2015-11-28 04:54:35
【问题描述】:

是否可以使用 DELETE 方法在 StringRequest 的正文中发送简单的文本?

我找不到任何有人在请求正文中添加内容的示例... 这是我的要求,我想在正文中添加“{'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;
        }
    };

【问题讨论】:

  • 编写自己的请求和override getBody()getBodyContentType() 方法。
  • 如果你的服务器端是 Asp.Net WebAPI, IMO,你应该阅读this questionanother question。也许其他网络服务是一样的
  • 感谢您的两个回答! @dieter_h 你能发布一个快速的代码示例吗?
  • 现在我在工作。稍后我会发布完整的答案。
  • 我发布了一个答案,但这只是我已经做过的答案的副本:stackoverflow.com/questions/23220695/…(如果您使用我在那里编写的代码,您将能够管理您的问题)

标签: android request android-volley delete-method


【解决方案1】:

这是因为 Volley 默认情况下不会发送 Body 进行 DELETE。仅适用于 POST、PUT 和 PATCH。不幸的是,至少可以说

这里列出了一个解决方法:Volley - how to send DELETE request parameters?

【讨论】:

    【解决方案2】:

    试试这个:

    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 是你的 json String

    【讨论】:

    • 感谢您的回答,但正文仍被忽略。如果我在 Postman 中使用 DELETE,一切似乎都正常,所以我的服务器很好。关于如何让 Volley 发送身体的任何其他想法?
    • 我没有听说任何其他方法。
    【解决方案3】:
    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);
    

    【讨论】:

    • 当您分享代码 sn-p 作为答案时,请尝试解释一下。
    猜你喜欢
    • 2016-09-24
    • 1970-01-01
    • 2020-12-13
    • 2018-03-11
    • 2021-05-24
    • 2021-08-11
    • 1970-01-01
    • 2019-12-13
    • 2020-01-02
    相关资源
    最近更新 更多