4

I am trying to fire a DELETE request using HttpUrlConnection in android,I am setting the setRequestmethod as DELETE and getting a 200 as response code. The Item is not getting deleted. The async I am using is below.

private class DeleteTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
        String result = null;
        URL url = null;
        try {
            url = new URL(urls[0]);
            Log.i("URL to access :", urls[0]);
        } catch (MalformedURLException exception) {
            exception.printStackTrace();
        }
        HttpURLConnection httpURLConnection = null;
        try {
            httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("DELETE");
            httpURLConnection.setRequestProperty("charset", "utf-8");
            httpURLConnection.setUseCaches(false);
            System.out.println("ResponseCode: "+httpURLConnection.getResponseCode());
            if(httpURLConnection.getResponseCode() == 204){
                Log.d(TAG,"Deleted");
            }
        } catch (IOException exception) {
            exception.printStackTrace();
        } finally {
            if (httpURLConnection != null) {
                httpURLConnection.disconnect();
            }
        }
        return null;
    }
    }
}

It looks like the setRequestMethod() is not working and its taking the Request as a GET and giving me a 200 !!

I tested this in postman(a chrome extension) and it was working fine , If it was a backend issue then from postman also it should fail.
okHttp: I was trying to make this work on okHttp also for that

Request request = new Request.Builder().url(url.toString()).patch(body).build();

How will I make up this for delete, because delete request dosent have a body

Volly: I've tried out the google volly library too..

RequestQueue requestQueue = Volley.newRequestQueue(TimerSummary.this);
    StringRequest request = new StringRequest(Request.Method.DELETE, uri, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d(TAG,"Response: "+response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d(TAG,"Error: "+error);
        }
    });
    requestQueue.add(request);

This also returns like GET request, I am getting the item as json which was supposed to be deleted.

any suggestions are appreciated. Thanks in advance..

P-RAD
  • 1,293
  • 2
  • 15
  • 36
  • 1
    to me it sounds like a Backend's issue – Blackbelt Jun 12 '15 at 12:39
  • @Blackbelt Thanks for the response , I tested this in postman(a chrome extension) and it was working fine, If it was a backend issue then from postman also it should fail right? – P-RAD Jun 12 '15 at 12:43
  • @Dev If it works with postman use a proxy like Burp or Filler and redirect your traffic thought that proxy (the one from Chrome and your Java traffic). Then compare both requests. – Robert Jun 12 '15 at 13:37
  • @Robert I dont know much about proxies .. but I'll try for sure – P-RAD Jun 12 '15 at 14:00
  • @Dev If it is not HTTPS you can also use Wireshark – Robert Jun 12 '15 at 14:13
  • @Robert its not .. If you dont mind , can you tell how I should test this with proxies? what am I looking for ?, Why can't I think this is a code mistake and move on to okhttp or volly? – P-RAD Jun 12 '15 at 14:17
  • @Robert Got the answer , The URL was wrong !!! before appending the params before "?" I forgot the "/" . That was the only issue ..Thanks for your time – P-RAD Jun 15 '15 at 09:08
  • I just test a restful DELETE method, and I got 204 back as response code. So you probably want to check again with your backend guys – Blackbelt Jun 15 '15 at 11:27
  • @Blackbelt exactly ! It was actually my mistake ,but still not sure that why it is taking as GET when I miss the "/" !! anyway the problem was the "/". – P-RAD Jun 15 '15 at 12:37
  • no it is not /. the only conversion done is from get to post you have setDoOutput flag set to true – Blackbelt Jun 15 '15 at 12:50
  • @Dev, you should answer your own question with what you discovered – Blackbelt Jun 16 '15 at 14:20

1 Answers1

0

It was actually a typo!!! I am an idiot!Both of my methods work fine ,but I am Now using Google's volly library for network related things.

I was missing a "/" before the "?" before appending parameters with the URL

P-RAD
  • 1,293
  • 2
  • 15
  • 36