我正在尝试使用HttpClient DELETE方法从列表中删除项目。我想使用请求体发送相关的item_id。我使用以下方式发送数据。
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient = HttpUtils.getNewHttpClient();
HttpDelete httpPostRequest = new HttpDelete(URL);
**httpPostRequest.setHeader("item_id",id);**
httpPostRequest.addHeader("Authorization", getB64Auth(username,password));
httpPostRequest.setHeader("Accept", "application/json");
httpPostRequest.setHeader("Content-type", "application/json");
httpPostRequest.setHeader("Accept-Encoding", "gzip");
但是我无法将项目删除到服务器数据库中。如何在HttpDelete中使用Request body?
答案 0 :(得分:2)
根据HTTP / 1.1规范,您不能发送除POST和PUT之外的任何实体。
使用请求参数或标头属性。您可以使用URI Builder:
URI myURI = android.net.Uri.Builder.path(myPathString).query("item_id=1").build();
答案 1 :(得分:1)
基于here的答案,该答案应该为您提供带有实体字段的DELETE请求。一旦你创建了自己的请求类型,就可以了;
List<NameValuePair> deleteParams = new ArrayList<>();
deleteParams.add(new BasicNameValuePair("item_id", id));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(deleteParams);
HttpDeleteWithBody deleteRequest = new HttpDeleteWithBody(URL);
deleteRequest.addHeader("Authorization", getB64Auth(username,password));
deleteRequest.setHeader("Accept", "application/json");
deleteRequest.setHeader("Content-type", "application/json");
deleteRequest.setHeader("Accept-Encoding", "gzip");
deleteRequest.setEntity(entity);