-1

I'm trying to implement the HttpDelete, I've used HttpGet and HttpPost, and worked well.

First think, I saw that on HttpDelete, I can not put del.setEntity(entity); where entity is StringEntity entity = new StringEntity(usuari.toString()); and usuari is the JSON.

Since I can't put entity on my HttpDelete, I tried this:

boolean resul = true;
HttpClient httpClient = new DefaultHttpClient();
HttpDelete del = new HttpDelete(getResources().getString(R.string.IPAPI) + "produsuaris/produsuari");

del.setHeader("content-type", "application/json");
try {
    JSONObject usuari = new JSONObject();
    try {
        usuari.put("idProducte", params[0]);
        usuari.put("idusuari", params[1]);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    HttpResponse resp = httpClient.execute(del);
    String respStr = EntityUtils.toString(resp.getEntity());

    if (!respStr.equals("true")) ;
        resul = false;
} catch (Exception ex) {
    Log.e("ServicioRest", "Error!", ex);
}
return resul;

I don't know how to put the JSONObject usuari on the entity of my HttpDelete. What I'm missing or what I'm doing wrong?

MRS1367
  • 1,053
  • 1
  • 14
  • 40
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
  • I don't wanna sound annoying by why don't you try changing to a more complete http client?. With [Ion](https://github.com/koush/ion) you can do this kind of things in 5,6 lines of code. – 4gus71n Apr 29 '15 at 16:39
  • I've got all the POST and GET with this method... I don't want to change my code, I think with HttpDelete it's possible – Skizo-ozᴉʞS ツ Apr 29 '15 at 16:55
  • possible duplicate of [HttpDelete with body](http://stackoverflow.com/questions/3773338/httpdelete-with-body) – 4gus71n Apr 29 '15 at 17:17

1 Answers1

1

Why dont you try doing this? IDK for certain if It will work, but worth try it.

HttpRequestBase dn = new HttpPost() {
                    @Override
                    public String getMethod() {
                        return HttpDelete.METHOD_NAME;
                    }

                    @Override
                    public HttpEntity getEntity() {
                        return new StringEntity("{json}") //Return you raw body here
                    }
                }

What you want to do is send the json as the raw body the request, right?

Also you could try 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(); }
}

And then:

HttpDeleteWithBody delete = new HttpDeleteWithBody(api_address);

StringEntity se = new StringEntity(json_data, HTTP.UTF_8);
se.setContentType("application/json");

delete.setEntity(se)
4gus71n
  • 3,717
  • 3
  • 39
  • 66