如何在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;
}

最佳答案

这是因为在默认情况下,Volley不会发送Body进行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";
    }
}

关于java - 我们可以使用 Volley 库在删除请求中发送带有x-www-url-form-encoded的正文吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43338627/

10-10 23:47