HttpDelete在android上使用setEntity的参数?

时间:2015-11-25 08:16:56

标签: java android http-delete

我尝试删除一个参数:

private class SendfeedbackDeleteStudio extends AsyncTask<String, Void, String> {
    private static final String LOG_TAG = "DeleteStudio";
    Bundle extras = getIntent().getExtras();
    final String token= extras.getString("TOKEN");
    @Override
    protected String doInBackground(String... params) {
        String venid = params[0];
        Utils.log("venid: " + venid);
        final String url_delete_studio = Constant.URI_BASE_FAVOURITE;
        String contentType;
        contentType = "application/x-www-form-urlencoded";
        // do above Server call here
        List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(1);
        nameValuePair.add(new BasicNameValuePair("vendor_id", venid));
        try
        {
            HttpClient httpClient = new DefaultHttpClient();
            HttpDelete httpDelete = new HttpDelete(url_delete_studio);
            httpDelete.setHeader("Content-Type", contentType);
            httpDelete.setHeader("Authorization", "Bearer " + token);
            httpDelete.setHeader("Accept", "application/json");
            httpDelete.setHeader("Accept-Charset", "utf-8");
            httpDelete.setEntity(new UrlEncodedFormEntity(nameValuePair));
            HttpResponse response = httpClient.execute(httpDelete);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                // EntityUtils to get the reponse content
                String content =  EntityUtils.toString(entity);
                Utils.log("daftar content: " + content);
                JSONObject hasiljson = new JSONObject(content);
                Utils.log("hasiljson object: " + hasiljson);
                String success = hasiljson.getString("success");
                Utils.log("success: " + success);
            }
            // writing response to log
            Log.d("Http Response:", response.toString());
        }
        catch (Exception e)
        {
            Log.e(LOG_TAG, String.format("Error during delete: %s", e.getMessage()));
        }
        return "processing";
    }

    @Override
    protected void onPostExecute(String message) {
        //process message
        clickFavourites();
    }
}

但它在httpDelete.setEntity(新的UrlEncodedFormEntity(nameValuePair));上变红了,它似乎无法识别我发送到删除的参数。如何删除venid参数?

1 个答案:

答案 0 :(得分:1)

HTTP DELETE就像GET变种一样,所以它不会接受任何输入。

如果您希望使用正文提供删除,则可能需要考虑将POST用于接受正文的位置。

或者您可以使用此

import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import java.net.URI;
import org.apache.http.annotation.NotThreadSafe;

@NotThreadSafe
class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {
    public static final String METHOD_NAME = "DELETE";
    public String getMethod() { return METHOD_NAME; }

    public HttpDeleteWithBody(final String uri) {
        super();
        setURI(URI.create(uri));
    }
    public HttpDeleteWithBody(final URI uri) {
        super();
        setURI(uri);
    }
    public HttpDeleteWithBody() { super(); }
}

来自here