I am using the Android Volley library to send a POST request. And for the POST request
Content-Type:application/json Header Content-Type:application/jsonJson String Message Json String
But no matter what I do to change the Volley Request Header , it is always set to Content-Type:text/html . And that gives me 400 Bad Request . Here is my class for POST request on Volley
public class GsonRequest<T> extends Request<T> { private final Class<T> clazz; private final Map<String, String> headers; private final Listener<T> listener; private Map<String, String> postParams; private String postString = null; public GsonRequest(int method, String url, Class<T> clazz, Map<String, String> headers, Map<String, Object> params, Listener<T> listener, ErrorListener errorListener) { super(method, url, errorListener); this.clazz = clazz; this.headers = headers; this.listener = listener; if (method == Method.POST && params != null && params.size() > 0) { setRetryPolicy(new DefaultRetryPolicy(12000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); postString = new GsonBuilder().create().toJson(params); } } @Override public Map<String, String> getHeaders() throws AuthFailureError { return headers != null ? headers : super.getHeaders(); } @Override public byte[] getBody() throws AuthFailureError { return postString != null ? postString.getBytes(Charset .forName("UTF-8")) : super.getBody(); } @Override public String getBodyContentType() { return postString !=null?"application/json; charset=utf-8":super.getBodyContentType(); } @Override protected void deliverResponse(T response) { listener.onResponse(response); } @Override protected Response<T> parseNetworkResponse(NetworkResponse response) { try { String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); Log.i("response", json); T responses = new GsonBuilder().create().fromJson(json, clazz); return Response.success(responses, HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (JsonSyntaxException e) { return Response.error(new ParseError(e)); } catch (Exception e) { return Response.error(new ParseError(e)); } } }
Any idea what I'm doing wrong. I checked with normal HttpPost and it works from there, but when using Volley my POST Header never changes.
android android-volley
laaptu Jun 03 '14 at 18:39 2014-06-03 18:39
source share