Delete Request With header and Parametes Volley

☆樱花仙子☆ 提交于 2019-11-28 01:21:49

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!