By Mustanser Iqbal


2015-11-05 19:51:55 8 Comments

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

1 comments

@BNK 2015-11-06 07:37:25

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!

@Mustanser Iqbal 2015-11-08 05:00:10

can you please tell how can i create newRequestQueue method like here stackoverflow.com/a/30748487/3593066http://stackoverflow.com‌​/a/…. m little confused please explain because i am getting this error java.net.ProtocolException: DELETE does not support writing

@BNK 2015-11-08 05:49:20

If you have control over server-side app (web service), I suggest you change so that DELETE request uses URL parameters instead of body params

@Mustanser Iqbal 2015-11-08 06:44:52

that is the main problem... i dont have access to web service

@BNK 2015-11-09 02:31:37

I uploaded my new sample project to github.com/ngocchung/DeleteRequest, please take a look. However, because the last success run got the response I/onResponse: {"reste":0}, so other requests later will get response code 400 because there's no item to delete :). Actually, they will get { "error": "bad request", "message": "suppression impossible : pas assez de bouteilles correspondantes en cave" }

@Mustanser Iqbal 2015-11-09 09:30:54

Thanks man its working perfectly. and thanks one again for your time. (y)

@BNK 2015-11-09 10:04:51

Glad it could help, happy coding :)

@Mustanser Iqbal 2015-11-09 11:56:18

can you give me some idea how to achieve this animation gettipsi.com/static/landing/landing2/images/… (scanning animation) currently m doing this with the help of animation list defined in anim.

@BNK 2015-11-10 04:47:40

Sorry, I am not familiar with animation in Android :)

@Mustanser Iqbal 2015-12-08 10:46:28

i need a little help. please

@BNK 2015-12-08 13:51:20

Yes, however I am on mobile, so I don't know can help you much or not

@Mustanser Iqbal 2015-12-08 17:41:12

actuall i am using volley in an other project to send JsonObject. but i dont know what this error mean.. can you tell me what that mean {{{12-08 23:06:44.546 29293-29293/com.pilot E/onErrorResponse: org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONObject}}} i can show code how i am trying if you like... thanks

@Mustanser Iqbal 2015-12-08 18:53:06

Thanks Brother for your consideration. i have solved it. actually i parse the network response with UTF-8 and it work. Thanku

@BNK 2015-12-08 23:49:13

Ok Bro :), sometimes if the server responses compressed (zip) data, you should decompress it at parseNetworkResponse too.

@Mustanser Iqbal 2015-12-09 04:31:57

Thanks, i don't why client don't access to web api. they give the link and said request on this URL. that's it create problem sometimes.. anyway thanks

@Mustanser Iqbal 2015-12-13 18:17:41

Brother do you have any experience with retrofit? I need a little help in this please.

@BNK 2015-12-14 01:06:06

Not much, I have tried it only 2-3 times before, however, if you have any issue, pls create a new question, IMO, many people in SO can help :)

@Mustanser Iqbal 2016-03-30 12:44:11

please have a look at this question stackoverflow.com/q/36309092/3593066 Thanks

@BNK 2016-03-30 13:06:25

Please read stackoverflow.com/questions/32240177/… to see if it can help or not.

@BNK 2016-03-30 13:33:40

Or try my sample project at github.com/ngocchung/MultipartOkHttp

@Mustanser Iqbal 2016-03-30 13:34:55

is this code is uploading videos more than 2 mb?

@BNK 2016-03-30 14:45:21

You can try, if have any error, please tell me

@Mustanser Iqbal 2016-03-30 16:01:07

i am getting socket timeOut exception. i am updating my today's question. what i have trying to do. please check.

@Kevin Flachsmann 2017-03-13 22:47:13

HttpClient is removed since SKD 23. Any workaround for that solution, or considering updating your git?

@BNK 2017-03-14 01:09:58

@KevinFlachsmann pls read stackoverflow.com/questions/32472263/…

@Abdul Waheed 2017-06-13 09:55:30

it was helpful me rather making custom hurl stack i appended params in url

@Anshu P 2017-08-09 04:51:17

@BNK Hi i checked your answer and sample , it does not work for me , it is giving me same Error DELETE does not support Writing Protocol exception.

Related Questions

Sponsored Content

15 Answered Questions

[SOLVED] How to Delete using INNER JOIN with SQL Server?

10 Answered Questions

3 Answered Questions

1 Answered Questions

13 Answered Questions

[SOLVED] How to set custom header in Volley Request

1 Answered Questions

Acceleromter service does not work on Android TV Box

1 Answered Questions

[SOLVED] Adding an array in params.put to volley request in android

2 Answered Questions

[SOLVED] How to serially get JSONObject from a txt file

  • 2015-05-28 12:45:57
  • Rashedul
  • 108 View
  • -1 Score
  • 2 Answer
  • Tags:   android json

0 Answered Questions

NanoHTTPD. Cache InputStream to file and continue streaming

Sponsored Content