我们可以使用齐射库在删除请求中使用x-www-url-form-encodede发送正文吗?

时间:2017-04-11 06:48:02

标签: java android android-volley

如何在android中x-www-form-urlencoded发送数据以传递删除请求?

@Override
public byte[] getBody()  {

    Map<String, String> params = new HashMap<>();
    params.put("is_admin","1");
    params.put("client_dbname",sessionManager.clientdbname());
    params.put("user_id" ,"1");

    //yeah, I copied this from the base method.
    if (params !=null)
    {
        try {
            para = params.toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    return para;
}

1 个答案:

答案 0 :(得分:1)

这是因为Volley默认情况下不会发送Body for DELETE。仅适用于POST,PUT和PATCH。

将此第三方用于删除请求

https://github.com/ngocchung/DeleteRequest

尝试此类删除请求:

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";
    }
}