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