Android Volley Header Title Does Not Change

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/json
  • Json 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; /** * Make a GET request and return a parsed object from JSON. * * @param url * URL of the request to make * @param clazz * Relevant class object, for Gson reflection * @param headers * Map of request headers */ 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.

+1
android android-volley
Jun 03 '14 at 18:39
source share
2 answers

Using a network sniffing tool like Wireshark , I found that my error was an HTTP header error. Then using the Chrome DHC plugin

I found that the Header Content-Type must be application/json; charset=utf-8 application/json; charset=utf-8 and I constantly used

  Map<String, String> header = new HashMap<String, String>(); header.put("Content-Type", "application/json"); 

instead

  Map<String, String> header = new HashMap<String, String>(); header.put("Content-Type", "application/json; charset=utf-8"); 

Using the correct Header solved my problem

+7
Jun 03 '14 at 19:05
source share

I found that I had to override getBodyContentType() to get the correct Content-Type header:

  public String getBodyContentType() { return "application/json; charset=utf-8"; } 

Here is my question with more details on this issue:

  • Content-Type Volley Header Does Not Update
+12
Oct 07 '14 at 23:01
source share



All Articles