-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,024
  • 1
  • 13
  • 40
Skizo-ozᴉʞS
  • 18,460
  • 17
  • 76
  • 134
  • 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
  • 4,031
  • 3
  • 36
  • 60
  • So I can't do it by the way I was doing? – Skizo-ozᴉʞS Apr 29 '15 at 17:55
  • It's the same that you were doing.. The only thing that you have to do is change the plain and simple `HttpDelete` for this `HttpDeleteWithBody` doing so would allow you to set the json body through the `setEntity` method. – 4gus71n Apr 29 '15 at 18:53
  • what's the first thing? I mean, I created the class, and then when shall I've to create the JSON? – Skizo-ozᴉʞS May 01 '15 at 19:01