Android - HttpDelete with JSON -
i'm trying implement httpdelete
, i've used httpget
, httppost
, , worked well.
first think, saw on httpdelete
, can not put del.setentity(entity);
entity stringentity entity = new stringentity(usuari.tostring());
, usuari
json
.
since can't put entity on httpdelete
, 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 put jsonobject usuari
on entity of httpdelete
. i'm missing or i'm doing wrong?
why dont try doing this? idk if work, worth try it.
httprequestbase dn = new httppost() { @override public string getmethod() { return httpdelete.method_name; } @override public httpentity getentity() { return new stringentity("{json}") //return raw body here } }
what want send json raw body request, right?
also 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)
Comments
Post a Comment