"HttpDeleteWithBody" is not deleted from the database

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 ...

Figure 1

What am I doing wrong?

EDIT

I tried using cURL and this is the result:

Figure 2

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.

+2
json android
May 2 '15 at 9:23 PM
source share
2 answers

Finally, I solved the problem, what I did was change the HttpDelete method on my API , and instead of sending JSON , I send parameters (like HttpGet ) and now my code is like:

  boolean resul = true; HttpClient httpClient = new DefaultHttpClient(); String id____USER = params[0]; String id____PROD = params[1]; HttpDelete del = new HttpDelete(getResources().getString(R.string.IPAPI) + "produsuaris/produsuari?idProd=" + Integer.parseInt(id____PROD)+"&idUs="+Integer.parseInt(id____USER)); del.setHeader("content-type", "application/json"); try { 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); resul = false; } return resul; 

This is what I call it AsyncMethod

  TareaWSInsertar tarea = new TareaWSInsertar(); tarea.execute(String.valueOf(IDUSU),String.valueOf(IDPROD)); 

This work is for me, I know that this is not the best solution, but I have little time, I also tried three solutions, and no one worked.

Feel free to post the correct answer if you know what I'm doing wrong.

0
May 05 '15 at 03:50
source
— -

This seems to be a server-side problem, to verify this, follow these steps: 1) Add Ion to your rich.

compile 'com.koushikdutta.ion:ion:+'

2) To execute your request use the following snipp:

 JSONObject jsonObject = new JSONObject(); try{ for(BasicNameValuePair aNameValue : getParameters()){ jsonObject.put(aNameValue.getName(), aNameValue.getValue()); Log.d("TEST","parameter "+aNameValue.getName()+": "+aNameValue.getValue()); } jsonObject.put("time_zone", Util.timeZone()); Log.d("TEST","parameter time_zone:"+Util.timeZone()); }catch(Exception e){ // } Log.d("CURL", "curl -X DELETE -d '"+jsonObject.toString()+"' "+getUrl()); Builders.Any.F builder = Ion.with(getContext()) .load(HttpDelete.METHOD_NAME, getUrl()) .setTimeout(BuildConfig.HttpClientMaxTimeout).setStringBody(jsonObject.toString()); String response = builder.asString().get(); Util.checkThreadUiException(); Log.d("TEST","-->"+ response); 

There is no science of rockets, this is the code that I used in the application, in this method I got the parameters to send as json, as a collection of BasicNameValuePair . You can change this and configure json. I am 100% sure that this request will fail because it is a server problem.

UPDATE

 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(); } 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(getContext()) .load(HttpDelete.METHOD_NAME, url) .setTimeout(BuildConfig.HttpClientMaxTimeout).setStringBody(usuari.toString()); String response = builder.asString().get(); Log.d("TEST","Req response -->"+ response) 

UPDATE

Try this, run this query through curl and let me know the result:

curl --http1.0 -X DELETE -d '{"idusuari":121,"idProducte":15}' 192.168.1.46/ServicioWebRest/api/produsuaris/produsuari

By doing this, you say to curl, to send a request via http 1.0, chunked responses are only supported by http 1.1, if there is an error in the chunk encoding, this should tell you.

Also take a look at this issue , which I have long introduced Ion. I think the problem I was having at that time and your current problems are the same, maybe some of the tips will help. Special part about addHeader("Connection", "close") .

It will look like this:

  Builders.Any.F builder = Ion.with(getContext()) .addHeader("Connection", "close") .load(HttpDelete.METHOD_NAME, getUrl()) .setTimeout(BuildConfig.HttpClientMaxTimeout).setStringBody(jsonObject.toString()); 
+1
May 03 '15 at 19:04
source



All Articles