Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete Request With header and Parametes Volley

Hi i want to Send Delete Request to server using Volley along Headers and body parameters. but i am not able to send request successfully

What i have tried

JSONObject jsonbObjj = new JSONObject();
try {
    jsonbObjj.put("nombre", Integer.parseInt(no_of_addition
            .getText().toString()));
    jsonbObjj.put("cru", crue);
    jsonbObjj.put("annee", 2010);
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
VolleyRequest mVolleyRequest = new VolleyRequest(
        Method.DELETE, url, jsonbObjj,

        new Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject jsonObject) {
                // TODO Auto-generated method stub

                if (pDialog != null) {
                    pDialog.dismiss();
                }
                Log.e("Server Response", "response = "
                        + jsonObject.toString());
            }

        }, new ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError arg0) {
                // TODO Auto-generated method stub
                if (pDialog != null) {
                    pDialog.dismiss();
                }
                Log.e("Error Response",
                        "Error " + arg0.getMessage());
                Log.e("Error Response",
                        "Error = " + arg0.getCause());

            }
        }, mUserSession.getUserEmail(), mUserSession
                .getUserPassword(), false);

ApplicationController.getInstance().addToRequestQueue(
        mVolleyRequest, "deleteRequest");

and here is my VolleyRequest request class

public class VolleyRequest extends JsonObjectRequest {

    String email, pass;
    boolean saveCookeis;

    public VolleyRequest(int method, String url, JSONObject jsonRequest,
            Listener<JSONObject> listener, ErrorListener errorListener,
            String email, String pass, boolean saveCookie) {
        super(method, url, jsonRequest, listener, errorListener);
        // TODO Auto-generated constructor stub
        this.email = email;
        this.pass = pass;
        this.saveCookeis = saveCookie;
    }

    public VolleyRequest(int method, String url, JSONObject jsonRequest,
            Listener<JSONObject> listener, ErrorListener errorListener) {
        super(Method.POST, url, jsonRequest, listener, errorListener);
        // TODO Auto-generated constructor stub

    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        // TODO Auto-generated method stub
            HashMap<String, String> params = new HashMap<String, String>();

            String auth = "";
            try {
                auth = android.util.Base64.encodeToString(
                        (this.email + ":" + this.pass).getBytes("UTF-8"),
                        android.util.Base64.DEFAULT);
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            params.put("Authorization", auth);
            return params;
    }

    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
        // TODO Auto-generated method stub

        if (saveCookeis) {
            try {
                String jsonString = new String(response.data,
                        HttpHeaderParser.parseCharset(response.headers));

                ApplicationController.getInstance().checkSessionCookie(
                        response.headers);

                return Response.success(new JSONObject(jsonString),
                        HttpHeaderParser.parseCacheHeaders(response));

            } catch (UnsupportedEncodingException e) {
                return Response.error(new ParseError(e));
            } catch (JSONException je) {
                return Response.error(new ParseError(je));
            }
        }
        return super.parseNetworkResponse(response);

    }

}

When i tried this code i get 400 response code error Please let me know if anyone can help me.. that what i am doing wrong. Thanks

here the screen shots for Delete Api which i tested and its working fine.

I need to send this Data to server

And here is the response form server

like image 398
Mustanser Iqbal Avatar asked Nov 05 '15 19:11

Mustanser Iqbal


1 Answers

UPDATE:

I have posted my working sample project to GitHub to fix java.net.ProtocolException: DELETE does not support writing, please take a look.


Your app got 400 error code because the data body has not been sent with DELETE request.

Inside HurlStack.java, you will find the following:

            case Method.DELETE:
                connection.setRequestMethod("DELETE");
                break;
            case Method.POST:
                connection.setRequestMethod("POST");
                addBodyIfExists(connection, request);
                break;

So we can see DELETE request ignores body data. There's a workaround, that is you create a CustomHurlStack class (copy all content of HurlStack above), with only modification as the following:

            case Request.Method.DELETE:
                connection.setRequestMethod("DELETE");
                addBodyIfExists(connection, request);
                break;

Then, in your activity, call:

CustomHurlStack customHurlStack = new CustomHurlStack();
RequestQueue queue = Volley.newRequestQueue(this, customHurlStack);

Please note that this workaround works only for API21+ (API20 I have not tested). From API19-, java.net.ProtocolException: DELETE does not support writing will be thrown.

P/S: add useLibrary 'org.apache.http.legacy' inside your build.gradle file if your app compileSdkVersion 23 and you get error when create CustomHurlStack class.

Hope this helps!

like image 127
BNK Avatar answered Sep 30 '22 16:09

BNK