I try to call the API by sending JSON to remove the product from my database; however, it does not delete anything.
JSON answer is "true" and it does not give me any error; even so, when I make a request in my database, the product is still there.
I created a class called HttpDeleteWithBody that looks like this:
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 on my doInBackGround my Fragment , I do this:
boolean resul = true; 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(); } try { HttpEntity entity = new StringEntity(usuari.toString()); HttpClient httpClient = new DefaultHttpClient(); HttpDeleteWithBody httpDeleteWithBody = new HttpDeleteWithBody(getResources().getString(R.string.IPAPI) + "produsuaris/produsuari"); httpDeleteWithBody.setEntity(entity); HttpResponse response = httpClient.execute(httpDeleteWithBody); Log.d("Response ---------->", response.getStatusLine().toString()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } catch (Exception ex) { Log.e("ServicioRest", "Error!", ex); } return resul;
Also, I tried to do this:
HttpDeleteWithBody delete = new HttpDeleteWithBody(getResources().getString(R.string.IPAPI) + "produsuaris/produsuari"); StringEntity se = new StringEntity(usuari.toString(), HTTP.UTF_8); se.setContentType("application/json"); delete.setEntity(se);
however it does not work ... the log says:
D / Response ---------->: HTTP / 1.1 200 OK
This is how I call the method:
JSONObject deleteproduct = new JSONObject(); try { deleteproduct.put("idProducte", String.valueOf(IDPROD)); deleteproduct.put("idusuari", String.valueOf(IDUSU)); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } Log.i("Json test per afegir prod --> ", deleteproduct.toString()); TareaWSInsertar tarea = new TareaWSInsertar(); tarea.execute(String.valueOf(IDPROD), String.valueOf(IDUSU));
I added a plugin called " PostMan " to my Google Chrome and when I try to do it this way it is removed correctly ...

What am I doing wrong?
EDIT
I tried using cURL and this is the result:

It returns me false when I put the same JSON as PostMan ; however, if I put the same JSON on PostMan , it works fine.
EDIT 2
I implemented the ion library and I did it like:
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(); } try { String url = getResources().getString(R.string.IPAPI) + "produsuaris/produsuari"; Log.d("CURL", "curl -X DELETE -d '" + usuari.toString() + "' " + url); Builders.Any.F builder = Ion.with(getActivity().getApplicationContext()) .load(HttpDelete.METHOD_NAME, url) .setTimeout(15000).setStringBody(usuari.toString()); String response = builder.toString(); Log.d("TEST", "Req response -->" + response); } catch (Exception ex){ resul = false; }
And he still returns that everything is in order and not delete anything.