Request Delete using Volley Android
Asked Answered
C

1

5

I try to send a DELETE Request using Volley to a REST Api with parameters like X-user-email and X-User-token. I have tried almost everything on Internet but still I am getting a 400 error Message.

When i use the POST Request its work, so i think its the same for the DELETE, i just changed:

(Request.Method.POST, url, params, new Response.Listener<JSONObject>() {...}

by

(Request.Method.DELETE, url, params, new Response.Listener<JSONObject>() {...}

There is my following code:

MainActivity.java

disconnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {;
                JSONObject params = new JSONObject();
                try {
                    params.put("X-User-Email", "[email protected]");
                    params.put("X-User-Token", "QwerTyuI789IOP");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                NetworkManager.getInstance(v.getContext()).delete("http://10.0.2.2:3000/users/sign_out", params, new NetworkManager.NetworkManagerListener<String>()  {
                    @Override
                    public void getResult(String response)
                    {
                        if (!response.isEmpty())
                        {
                            result.setText(response.toString());
                            Log.e("Response", response.toString() );
                        }
                    }
                });
            }
        });

NetworkManager.java

public void delete(String url, JSONObject params, final NetworkManagerListener<String> listener) {
        JsonObjectRequest request = new JsonObjectRequest
                (Request.Method.DELETE, url, params, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d(TAG + ": ", "delete onResponse : " + response.toString());
                        if (null != response.toString())
                            listener.getResult(response.toString());
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        if (null != error.networkResponse) {
                            Log.d(TAG + ": ", "delete Error Response code: " + error.networkResponse.statusCode);
                            listener.getResult(error.toString());
                        }
                    }
                }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("Accept", "application/json");
                params.put("Content-type", "application/json");
                return params;
            }
        };
        ;
        requestQueue.add(request);
    }

Do you know how to fix that? thank you to read me.

Colvert answered 12/4, 2016 at 2:9 Comment(0)
F
7

Because DELETE request ignores body data, then you should create a CustomHurlStack class.

Please read more my answer at the following SO question:

Delete Request With header and Parametes Volley

Or try my GitHub sample project. Hope it helps!

Fierce answered 12/4, 2016 at 2:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.