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.
params.put("Authorization", "Basic " + auth);
– Pernodcase Method.DELETE: connection.setRequestMethod("DELETE"); break; case Method.POST: connection.setRequestMethod("POST"); addBodyIfExists(connection, request); break;
. Read some solution here #19051206 – Pernod