2

I try to delete a parameter with this :

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();
    }
}

but it get red on httpDelete.setEntity(new UrlEncodedFormEntity(nameValuePair));, it seems it cannot recognize the parameter that I sent to delete. How to delete venid parameter?

1 Answers1

1

HTTP DELETE acts like GET variant so it won't take any inputs.

If you are looking to provide a delete with a body, you might want to consider using a POST to a location that accepts a body.

or you can use this

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(); }
}

which is referred from here

Community
  • 1
  • 1
Niko Adrianus Yuwono
  • 11,012
  • 8
  • 42
  • 64