我正在尝试使用Volley向我的服务器发送一个DELETE类型的请求,并为请求添加参数.到目前为止我还没有这样做.
创建自定义请求并覆盖getParams()方法并没有帮助我,因为这个方法没有被调用为DELETE类型请求.
如何在Volley中的DELETE请求中添加参数?
解决方法
同样的问题在这里,但我找到了解决方案
问题是在com.android.volley.toolBox.HttpClientStack.java中实现createHttpRequest方法,只有在请求方法为POST,PUT或PATCH时才会添加该主体.
/** * Creates the appropriate subclass of HttpUriRequest for passed in request. */ @SuppressWarnings("deprecation") /* protected */ static HttpUriRequest createHttpRequest(Request<?> request,Map<String,String> additionalHeaders) throws AuthFailureError { switch (request.getMethod()) { case Method.DEPRECATED_GET_OR_POST: { // This is the deprecated way that needs to be handled for backwards compatibility. // If the request's post body is null,then the assumption is that the request is // GET. Otherwise,it is assumed that the request is a POST. byte[] postBody = request.getPostBody(); if (postBody != null) { HttpPost postRequest = new HttpPost(request.getUrl()); postRequest.addHeader(HEADER_CONTENT_TYPE,request.getPostBodyContentType()); HttpEntity entity; entity = new ByteArrayEntity(postBody); postRequest.setEntity(entity); return postRequest; } else { return new HttpGet(request.getUrl()); } } case Method.GET: return new HttpGet(request.getUrl()); case Method.DELETE: return new HttpDelete(request.getUrl()); case Method.POST: { HttpPost postRequest = new HttpPost(request.getUrl()); postRequest.addHeader(HEADER_CONTENT_TYPE,request.getBodyContentType()); setEntityIfNonEmptyBody(postRequest,request); return postRequest; } case Method.PUT: { HttpPut putRequest = new HttpPut(request.getUrl()); putRequest.addHeader(HEADER_CONTENT_TYPE,request.getBodyContentType()); setEntityIfNonEmptyBody(putRequest,request); return putRequest; } case Method.HEAD: return new HttpHead(request.getUrl()); case Method.OPTIONS: return new HttpOptions(request.getUrl()); case Method.TRACE: return new HttpTrace(request.getUrl()); case Method.PATCH: { HttpPatch patchRequest = new HttpPatch(request.getUrl()); patchRequest.addHeader(HEADER_CONTENT_TYPE,request.getBodyContentType()); setEntityIfNonEmptyBody(patchRequest,request); return patchRequest; } default: throw new IllegalStateException("Unknown request method."); } }
所以你必须使用自己的HttpStack.java实现,或者你重写HttpClientStack类.
首先你应该替换你的初始化RequestQueue
RequestQueue requestQueue = Volley.newRequestQueue(sContext);
至
String userAgent = "volley/0"; try { String packageName = getContext().getPackageName(); PackageInfo info = getContext().getPackageManager().getPackageInfo(packageName,0); userAgent = packageName + "/" + info.versionCode; } catch (PackageManager.NameNotFoundException e) {} HttpStack httpStack = new OwnHttpClientStack(AndroidHttpClient.newInstance(userAgent)); RequestQueue requesQueue = Volley.newRequestQueue(sContext,httpStack);
并编写您自己的HttpClientStack实现,您可以在createHttpRequest()方法中更改“Method.POST:”的大小写.您还必须创建一个像“OwnHttpDelete”这样的对象,作为HttpEntityEnclosingRequestBase的扩展,使用方法setEntityIfNonEmptyBody().
public class OwnHttpClientStack extends com.android.volley.toolBox.HttpClientStack { private final static String HEADER_CONTENT_TYPE = "Content-Type"; public OwnHttpClientStack(HttpClient client) { super(client); } @Override public HttpResponse performRequest(Request<?> request,String> additionalHeaders) throws IOException,AuthFailureError { HttpUriRequest httpRequest = createHttpRequest(request,additionalHeaders); addHeaders(httpRequest,request.getHeaders()); onPrepareRequest(httpRequest); HttpParams httpParams = httpRequest.getParams(); int timeoutMs = request.getTimeoutMs(); // TODO: Reevaluate this connection timeout based on more wide-scale // data collection and possibly different for wifi vs. 3G. HttpConnectionParams.setConnectionTimeout(httpParams,5000); HttpConnectionParams.setSoTimeout(httpParams,timeoutMs); return mClient.execute(httpRequest); } private static void addHeaders(HttpUriRequest httpRequest,String> headers) { for (String key : headers.keySet()) { httpRequest.setHeader(key,headers.get(key)); } } static HttpUriRequest createHttpRequest(Request<?> request,String> additionalHeaders) throws AuthFailureError { switch (request.getMethod()) { case Request.Method.DEPRECATED_GET_OR_POST: { byte[] postBody = request.getPostBody(); if (postBody != null) { HttpPost postRequest = new HttpPost(request.getUrl()); postRequest.addHeader(HEADER_CONTENT_TYPE,request.getPostBodyContentType()); HttpEntity entity; entity = new ByteArrayEntity(postBody); postRequest.setEntity(entity); return postRequest; } else { return new HttpGet(request.getUrl()); } } case Request.Method.GET: return new HttpGet(request.getUrl()); case Request.Method.DELETE: OwnHttpDelete deleteRequest = new OwnHttpDelete(request.getUrl()); deleteRequest.addHeader(HEADER_CONTENT_TYPE,request.getBodyContentType()); setEntityIfNonEmptyBody(deleteRequest,request); return deleteRequest; case Request.Method.POST: { HttpPost postRequest = new HttpPost(request.getUrl()); postRequest.addHeader(HEADER_CONTENT_TYPE,request.getBodyContentType()); setEntityIfNonEmptyBody(postRequest,request); return postRequest; } case Request.Method.PUT: { HttpPut putRequest = new HttpPut(request.getUrl()); putRequest.addHeader(HEADER_CONTENT_TYPE,request.getBodyContentType()); setEntityIfNonEmptyBody(putRequest,request); return putRequest; } case Request.Method.HEAD: return new HttpHead(request.getUrl()); case Request.Method.OPTIONS: return new HttpOptions(request.getUrl()); case Request.Method.TRACE: return new HttpTrace(request.getUrl()); case Request.Method.PATCH: { HttpPatch patchRequest = new HttpPatch(request.getUrl()); patchRequest.addHeader(HEADER_CONTENT_TYPE,request.getBodyContentType()); setEntityIfNonEmptyBody(patchRequest,request); return patchRequest; } default: throw new IllegalStateException("Unknown request method."); } } private static void setEntityIfNonEmptyBody(HttpEntityEnclosingRequestBase httpRequest,Request<?> request) throws AuthFailureError { byte[] body = request.getBody(); if (body != null) { HttpEntity entity = new ByteArrayEntity(body); httpRequest.setEntity(entity); } } private static class OwnHttpDelete extends HttpPost { public static final String METHOD_NAME = "DELETE"; public OwnHttpDelete() { super(); } public OwnHttpDelete(URI uri) { super(uri); } public OwnHttpDelete(String uri) { super(uri); } public String getMethod() { return METHOD_NAME; } } }