Delete request with Volley header and parameters

Hi, I want to send a request to the server using Volley on the headers and body parameters. but I can not send the request successfully

What i 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 a 400 response code error. Please let me know if anyone can help me .. what am I doing wrong. Thanks

here are the screenshots for Delete Api that I tested, and its work is excellent.

I need to send this data to the server

And here is the response form server

+5
source share
1 answer

UPDATE:

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


Your application received error code 400 because the data body was not sent with a 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 that the DELETE query ignores the body data. There is a workaround, that is, you create the CustomHurlStack class (copy the entire contents of the HurlStack above), only with a modification:

  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 only works for API21 + (I have not tested API20). From API19-, java.net.ProtocolException: DELETE does not support writing will be reset.

P / S: add useLibrary 'org.apache.http.legacy' to your build.gradle file if your application is compileSdkVersion 23 and you get an error when creating the CustomHurlStack class.

Hope this helps!

+5
source

Source: https://habr.com/ru/post/1235321/


All Articles