0

My put method is working fine, but just changing the put request to Delete then its not working,, I tried even by sending its header. but still not working. I even tried Json object to set the parameter. Thanks in advance.

StringRequest stringRequest = new StringRequest(Request.Method.DELETE, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("blalala", response);
                String qtyReserved1 = response.toString();
                   Toast.makeText(mContext, "ok" + qtyReserved1, Toast.LENGTH_SHORT).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(mContext, "not ok" + username + Integer.toString(inventoryId), Toast.LENGTH_SHORT).show();
            }
        })
        {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("screen_name", username);
                params.put("inventory_id", Integer.toString(inventoryId));
                params.put("pending", "true");
                return params;
            }
            @Override
            public String getBodyContentType() {
                return "application/json";
            }
        };
        MySingleton.mySingletonInstance(mContext.getApplicationContext()).addToRequestque(stringRequest);
  • "its not working " . What do you mean. Are you getting any error? or your server not getting your request? – Vinayak B Aug 16 '17 at 11:52
  • Server is not getting the request, i guess – Irufaan Ali Aug 16 '17 at 12:49
  • Which is your server. Is it PHP, .Net or Java – Vinayak B Aug 16 '17 at 12:52
  • its php server.. is there any mistake in my code,, really appreciate – Irufaan Ali Aug 16 '17 at 12:57
  • try this answer: https://stackoverflow.com/questions/1402229/why-does-my-web-server-software-disallow-put-and-delete-requests?answertab=votes#tab-top – Vinayak B Aug 16 '17 at 13:21
  • put, get and post request work perfectly,, but delete request is troubling me, i wonder others also have hard time on this delete request? im using volley library, – Irufaan Ali Aug 16 '17 at 14:14
  • Is my code correct, pls tell me if i have – Irufaan Ali Aug 16 '17 at 14:15
  • actually this may be a server issue. not sure.. have you tested on postman..is it giving you any response? or error 403? just test this API with postman. if there is no response then it will be a server error – Vinayak B Aug 16 '17 at 14:36
  • i have already tested in postman. it works – Irufaan Ali Aug 16 '17 at 15:52
  • is the responce comming in onErrorResponse().? if it so, please provide error.printStackTrace(); and error.networkResponse.statusCode; – Vinayak B Aug 16 '17 at 16:01
  • It showing com.android.volley.ServerError Also BasicNetwork.performRequest : Unexpected response code 400 for http://192.168.4.31/api/canteen/cart – Irufaan Ali Aug 17 '17 at 03:19
  • Its a bad request. problem is from your side. In postman you will find a tab called "code". select it and find the code for java. then try to add that in your project or post here. If you post here, then I will write volley request for you – Vinayak B Aug 17 '17 at 03:43
  • OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.parse("application/json"); RequestBody body = RequestBody.create(mediaType, "{\n\t\"screen_name\": \"mariyam.shimaanath\", \n\t\"inventory_id\" : \"19\",\n\t\"pending\" : \"true\"\n}"); Request request = new Request.Builder() .url("http://192.168.4.31/api/canteen/cart") .delete(body) .addHeader("content-type", "application/json") .addHeader("cache-control", "no-cache") .addHeader("postman-token", "397cd246-7ec7-582f-bb72-f49f16272a79") .build(); Response response = client.newCall(request).execute(); – Irufaan Ali Aug 17 '17 at 04:09
  • How do i post screenshot,, ? – Irufaan Ali Aug 17 '17 at 04:11
  • I found the problem. Actually volley don't send body if you using DELETE. thats why its not working. there is a solution https://stackoverflow.com/questions/22803766/volley-how-to-send-delete-request-parameters?answertab=votes#tab-top .. If this not work try to do this using other library like retrofit, loopj or OkHttp – Vinayak B Aug 17 '17 at 05:43
  • Thanks for your time and help , Bless you – Irufaan Ali Aug 17 '17 at 05:58
  • cant i do this using volley library with less code.. I hate to write whole lot of code just do one task,, i only need delete. – Irufaan Ali Aug 17 '17 at 06:03
  • In that case try my answer below. Using loopj is easier than volley. – Vinayak B Aug 17 '17 at 06:10

2 Answers2

0

Volley library don't send body when you using DELETE method. You can try other library. I am providing you an example by using loopj library

Add dependency in your gradle

 compile 'com.loopj.android:android-async-http:1.4.9'

Request your web Api

  RequestParams requestParams = new RequestParams();
    requestParams.put("screen_name", "mariyam.shimaanath");
    requestParams.put("inventory_id", 19);
    requestParams.put("pending", true);

    String url="192.168.4.31/api/canteen/cart";

    new AsyncHttpClient().delete(url, requestParams, new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
          String rs = new String(responseBody);

           // do whatever you want

        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

        }
    });
Vinayak B
  • 4,430
  • 4
  • 28
  • 58
0

After lots of research, thank God I'm managed to solve the problem,, Not exactly solved the problem had in Delete request in Volley. But, I had to changed the method i need to request from server side. I changed the request to post method, I know it might not be good practice and solution, but now i managed to delete from sever. thats what i need.